我在我的项目中使用Castle windsor。我想制作懒惰的解析服务和存储库。示例代码:
public IPersonService PersonService
{
get { return _windsorContainer.Resolve<IPersonService>(); }
set { _windsorContainer.Resolve<IPersonService>(value); }
}
public ICityService CityService
{
get { return _windsorContainer.Resolve<ICityService>(); }
set { _windsorContainer.Resolve<ICityService>(value); }
}
public IPhoneService PhoneService
{
get { return _windsorContainer.Resolve<IPhoneService>(); }
set { _windsorContainer.Resolve<IPhoneService>(value); }
}
问题是:解决后,我需要做什么,发布这些。 TNX
修改
My Roof Class是:
public class PhonebookApplication
{
public WindsorContainer _windsorContainer;
private void InitializeWindsor()
{
_windsorContainer = new WindsorContainer();
_windsorContainer.Install(Configuration.FromXmlFile("Castle.config"));
}
public void Dispose()
{
if (_windsorContainer != null)
{
_windsorContainer.Dispose();
}
}
private static readonly Lazy<PhonebookApplication> s_Current = new Lazy<PhonebookApplication>(() => new PhonebookApplication(), true);
public static PhonebookApplication Current
{
get { return s_Current.Value; }
}
public void ApplicationStart()
{
InitializeWindsor();
}
//Service count increaseble as your have.
public IPersonService PersonService
{
get { return _windsorContainer.Resolve<IPersonService>(); }
set { _windsorContainer.Resolve<IPersonService>(value); }
}
public ICityService CityService
{
get { return _windsorContainer.Resolve<ICityService>(); }
set { _windsorContainer.Resolve<ICityService>(value); }
}
public IPhoneService PhoneService
{
get { return _windsorContainer.Resolve<IPhoneService>(); }
set { _windsorContainer.Resolve<IPhoneService>(value); }
}
}
}
如果我想在HomeController上调用一个方法,我将方法用作PhoneBookApplication.Current.PhoneService.GetAllList()
,然后我想在方法调用完成后调用release current resolved service。如果我将这个类构造函数注入所有工厂类,那么屋顶类依赖于此,因此我不想在任何地方使用ctor注入。
答案 0 :(得分:2)
不要这样做 - 您可以使用Windsors TypedFactory
faclity代替在运行时解析和发布组件。
您只需创建一个界面:
public interface IPersonServiceFactory
{
IPersonService Create();
void Release(IPersonService svc);
}
然后注册AsFactory
container.Register(Component.For<IPersonServiceFactory>().AsFactory());
然后将工厂注入您的班级:
public class SomeClass
{
public SomeClass(IPersonServiceFactory personServiceFactory)
{
IPersonService svc = personServiceFactory.Create();
// Do some work with the service
svc.Blah();
// Now release it
personServiceFactory.Release(svc);
}
}
您需要确保在引导代码中向容器添加了类型TypedFactoryFacility
:
container.AddFacility<TypedFactoryFacility>();
<强>附录:强>
如果需要添加任何特殊分辨率逻辑,还可以为工厂界面创建自定义解析器:
container.Register(Component.For<IPersonServiceFactory>().AsFactory(x => x.SelectedWith<PersonServiceFactoryComponentSelector>()));
不要忘记注册
container.Register(Component.For<PersonServiceFactoryComponentSelector>());
实现应该是DefaultTypedFactoryComponentSelector
的子类,并且可以覆盖解析组件名称的方法
e.g。您可能希望向Create()
工厂方法添加一个参数,以便它使用bool Create(bool useAlternateComponent)
来确定是使用一个组件还是另一个组件:
public class DispatcherFactoryComponentSelector : DefaultTypedFactoryComponentSelector
{
protected override string GetComponentName(System.Reflection.MethodInfo method, object[] arguments)
{
if(arguments[0] is bool && (bool)arguments[0] == true)
// Return a certain component name
else
// Return a different component name
}
}