Hi,
It is very common to create Dependency Injection container for the ASP.NET application so it lives while the application lives.
I create the DI container in each request and release it at the end of the request.
The main purpose is that any DI container supports Disposing objects when the container gets disposed.
Additional: If I need to share resources among requests (NHibernate SessionFactory) I just keep them in a static variable and encapsulate that value in an object per request. Like this:
public class SessionFactoryAggregator : ISessionFactory {
static ISessionFactory actualFactory;
// Implement ISessionFactory and proxy calls to the actualFactory
}
This is just simulated singleton pattern.
My questions:
- Is it ok to do that?
- If not why and what should be done instead?
- Any known performance issues in this approach?
Update: At the moment I use Castle Windsor through my own abstraction of DI provider, so the actual container is pluggable.
Thanks.