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