如何为 wpf 应用程序设置容器以共享每个图形的依赖项
How can I set up the container for a wpf application to share dependencies per graph
我有一个结构良好的大型 wpf mvvm 应用程序,它使用温莎城堡作为容器。
我的根混合了带有类型化工厂和存储库的操作。
我希望我的所有操作都具有每个图表的共享依赖关系。
如果我的所有操作都只使用类型化工厂来创建视图模型,那么使用 BoundTo < ViewModelBase > ()...
注册存储库会很容易
我目前的解决方案使用
BoundTo< Object >() 有它的缺点,例如,所有根操作共享存储库。
我已经搜索了解决方案,但没有找到任何解决方案。
我更愿意使用温莎城堡作为我的容器。
谁能帮我解决这个问题?
我做的是使用 BoundTo。只需拥有您想要的任何 class 自己的图形实现 ISomeMarkerInterface,它只是一个空接口 class。如果我没记错的话,您需要将 ISomeMarkerInterface 注册为 class.
的附加接口
祝你好运,
马尔维恩.
== 编辑 ==
如果您确实需要这种灵活性,请尝试使用 CustomRootSelector :
using System.Diagnostics;
using System.Linq;
using Castle.MicroKernel;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
namespace ConsoleApplication3
{
public class Root1
{
public Dep1 D1 { get; set; }
public Dep1 D2 { get; set; }
public Dep2 D3 { get; set; }
public Dep2 D4 { get; set; }
}
public class Root2
{
public Dep1 D1 { get; set; }
public Dep1 D2 { get; set; }
public Dep2 D3 { get; set; }
public Dep2 D4 { get; set; }
}
public class Dep1
{
}
public class Dep2
{
}
public class CustomScopeRootSelector
{
private readonly object[] _roots;
public CustomScopeRootSelector(params object[] roots)
{
_roots = roots;
}
public IHandler Selector(IHandler[] resolutionStack)
{
return resolutionStack.FirstOrDefault(h => _roots.Contains(h.ComponentModel.Implementation));
}
}
class Program
{
static void Main()
{
var container = new WindsorContainer();
var rootSelector = new CustomScopeRootSelector(typeof (Root1), typeof (Root2));
container.Register(
Component.For<Root1>().LifestyleTransient(),
Component.For<Root2>().LifestyleTransient(),
Component.For<Dep1>().LifestyleBoundTo(rootSelector.Selector),
Component.For<Dep2>().LifestyleBoundTo(rootSelector.Selector)
);
var r1 = container.Resolve<Root1>();
var r2 = container.Resolve<Root2>();
Debug.Assert(r1.D1 == r1.D2);
Debug.Assert(r1.D1 != r2.D1);
}
}
}
我希望代码解释得足够清楚。如果您有任何问题,请告诉我。
我有一个结构良好的大型 wpf mvvm 应用程序,它使用温莎城堡作为容器。
我的根混合了带有类型化工厂和存储库的操作。 我希望我的所有操作都具有每个图表的共享依赖关系。
如果我的所有操作都只使用类型化工厂来创建视图模型,那么使用 BoundTo < ViewModelBase > ()...
注册存储库会很容易我目前的解决方案使用 BoundTo< Object >() 有它的缺点,例如,所有根操作共享存储库。
我已经搜索了解决方案,但没有找到任何解决方案。 我更愿意使用温莎城堡作为我的容器。 谁能帮我解决这个问题?
我做的是使用 BoundTo。只需拥有您想要的任何 class 自己的图形实现 ISomeMarkerInterface,它只是一个空接口 class。如果我没记错的话,您需要将 ISomeMarkerInterface 注册为 class.
的附加接口祝你好运, 马尔维恩.
== 编辑 ==
如果您确实需要这种灵活性,请尝试使用 CustomRootSelector :
using System.Diagnostics;
using System.Linq;
using Castle.MicroKernel;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
namespace ConsoleApplication3
{
public class Root1
{
public Dep1 D1 { get; set; }
public Dep1 D2 { get; set; }
public Dep2 D3 { get; set; }
public Dep2 D4 { get; set; }
}
public class Root2
{
public Dep1 D1 { get; set; }
public Dep1 D2 { get; set; }
public Dep2 D3 { get; set; }
public Dep2 D4 { get; set; }
}
public class Dep1
{
}
public class Dep2
{
}
public class CustomScopeRootSelector
{
private readonly object[] _roots;
public CustomScopeRootSelector(params object[] roots)
{
_roots = roots;
}
public IHandler Selector(IHandler[] resolutionStack)
{
return resolutionStack.FirstOrDefault(h => _roots.Contains(h.ComponentModel.Implementation));
}
}
class Program
{
static void Main()
{
var container = new WindsorContainer();
var rootSelector = new CustomScopeRootSelector(typeof (Root1), typeof (Root2));
container.Register(
Component.For<Root1>().LifestyleTransient(),
Component.For<Root2>().LifestyleTransient(),
Component.For<Dep1>().LifestyleBoundTo(rootSelector.Selector),
Component.For<Dep2>().LifestyleBoundTo(rootSelector.Selector)
);
var r1 = container.Resolve<Root1>();
var r2 = container.Resolve<Root2>();
Debug.Assert(r1.D1 == r1.D2);
Debug.Assert(r1.D1 != r2.D1);
}
}
}
我希望代码解释得足够清楚。如果您有任何问题,请告诉我。