简体   繁体   中英

Castle Windsor Component Lifestyle advice

I'm looking for some advice on how to best configure this situation. I have the following components in an ASP MVC application.

  • Controllers - Transient by requirement of MVC
  • Services - Controllers call these to get to the business layer
  • Validators - Services use these to validate components. These are expensive to create so I need them to be Singleton
  • Repositories - Services and Validators use these to access data
  • ISession - NHibernate Session configured as PerWebRequest

I'm looking at how best to configure Services and Repositories. On the surface it seems like they could both be configured the same. Repositories only dependency (passed in on the constructor) is ISession. Services only dependencies (passed in on the constructor) are Repositories. When a Service needs a Validator it gets it from the ServiceLocator. Validators get Repositories in the constructor. I think that because Validators are Singleton passing Transient Repositories is problematic because the Validator will hold onto a Repository that has a disposed ISession.

Any help here would be greatly appreciated.

Any repository that rely on an UnitOfWork(ORM, custom implementaton...) has to be transient or at least same UoW lifestyle .

In case your Validator needs a "read-only" access to the storage via Repository... you may consider to have a Singleton Repository but be very carefull on its implementation

  • Session PerWebRequest
  • Repositories depending on using a ISessionProvider (could be singleton), if they are injected with the session they are bound to the lifetime of the session so PerWebRequest
  • Validators : expensive to create ???? ok if they need a repo or a session same applies here as the repos itself
  • services: can probably be also per web request

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM