Question

I'm trying to auto-register NHibernate entities as NCommon IRepository's using Castle and I can't figure it out.

On a previous project, we used autofac and this is how we accomplished it:

autofacContainerBuilder.RegisterGeneric(typeof (NHRepository<>))
                .As(typeof (IRepository<>))
                .InstancePerLifetimeScope();

Is there an equivalent for Castle? Thanks!

EDIT:

This is as close as I can get it, but the ComponentActivator fails on instantiating NHRepository:

.Register(Component.For(typeof(IRepository<>))
                   .ImplementedBy(typeof(NHRepository<>))
                   .LifeStyle.Transient)

ComponentActivator could not instantiate NCommon.Data.NHibernate.NHRepository

Instantiating manually blows up:

NHRepository<MyEntity> blah = new NHRepository<MyEntity>();

with:

*Object reference not set to an instance of an object at Microsoft.Practices.ServiceLocation.ServiceLocator.get_Current() in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocator.cs:line 17*

SOLVED:

I was missing this:

ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(Container));

WindsorServiceLocator is available here:

http://commonservicelocator.codeplex.com/wikipage?title=Castle%20Windsor%20Adapter&referringTitle=Home

More info here:

http://prashantbrall.wordpress.com/2010/11/22/service-locator-pattern-with-windsor-castle/

Était-ce utile?

La solution

I was missing this:

ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(Container));

WindsorServiceLocator is available here:

http://commonservicelocator.codeplex.com/wikipage?title=Castle%20Windsor%20Adapter&referringTitle=Home

More info here:

http://prashantbrall.wordpress.com/2010/11/22/service-locator-pattern-with-windsor-castle/

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top