I've create a sample project trying out some new patterns, namely Dao and IoC.
My Dao is defined as follows:
public class Dao<T> : IDao<T>
{
protected NHibernate.ISessionFactory _sessionFactory;
public Dao(NHibernate.ISessionFactory sessionFactory)
{
this._sessionFactory = sessionFactory;
}
protected NHibernate.ISession Session
{
get { return _sessionFactory.GetCurrentSession(); }
}
public T GetById(object id)
{
return Session.Get<T>(id);
}
...
}
And I have a corresponding installer:
public class DaoInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Component.For(typeof(Data.IDao<>))
.ImplementedBy(typeof(Data.Dao<>))
.ServiceOverrides(ServiceOverride.ForKey("SessionFactory").Eq("FirstSessionFactory"))
.Named("FirstDao"));
}
}
Using MVC pattern I can define a controller with a constructor that would accept IDao<MyClass> myClass
as an argument and Windsor will do all the magic of instantiating Dao with the correct SessionFactory for me. My question is, how do I achieve the same behaviour in non-MVC environment? So on any particular page, how do I get an instance of myClass?