我正在尝试使用 Castle 将 NHibernate 实体自动注册为 NCommon IRepository,但我无法弄清楚。
在之前的项目中,我们使用了 autofac,这就是我们完成它的方式:
autofacContainerBuilder.RegisterGeneric(typeof (NHRepository<>))
.As(typeof (IRepository<>))
.InstancePerLifetimeScope();
有城堡的等价物吗?谢谢!
编辑:
这是我能得到的最接近的结果,但是 ComponentActivator 在实例化 NHRepository 时失败:
.Register(Component.For(typeof(IRepository<>))
.ImplementedBy(typeof(NHRepository<>))
.LifeStyle.Transient)
ComponentActivator 无法实例化 NCommon.Data.NHibernate.NHRepository
手动实例化会爆炸:
NHRepository<MyEntity> blah = new NHRepository<MyEntity>();
和:
*对象引用未设置为 Microsoft.Practices.ServiceLocation.ServiceLocator.get_Current() 中 c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocator.cs:line 17 中的对象实例*
解决了:
我错过了这个:
ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(Container));
WindsorServiceLocator 可在此处获得:
更多信息在这里:
http://prashantbrall.wordpress.com/2010/11/22/service-locator-pattern-with-windsor-castle/