В моем проекте MVC я настроил мой MvcApplication_start ():
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
И успешно связали .To относительно моего IProductsRepository с MySqlProductsRepository:
public class NinjectControllerFactory : DefaultControllerFactory
{
private readonly IKernel _kernel = new StandardKernel(new MyServices());
protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
return null;
return (IController) _kernel.Get(controllerType);
}
public class MyServices: NinjectModule
{
public override void Load()
{
Bind<IProductsRepository>().To<MySqlProductsRepository>();
}
}
}
Но я использую NHibernate и у меня есть отдельный класс Session Factory, у которого есть метод GetSession (), который возвращает ISession.
public static ISessionFactory SessionFactory = CreateSessionFactory();
private static ISessionFactory CreateSessionFactory()
{
var cfg = new Configuration().Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "nhibernate.config"));
cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionStringName, System.Environment.MachineName);
NHibernateProfiler.Initialize();
return cfg.BuildSessionFactory();
}
public static ISession GetSession()
{
return SessionFactory.GetCurrentSession();
}
Я хотел настроить его так, чтобы мой MySqlProductsRepository был передан и объект ISession объектом Ninject при его создании:
public class MySqlProductsRepository : IProductsRepository
{
private readonly ISession _session;
public MySqlProductsRepository(ISession session)
{
_session = session;
}
И моему контроллеру будет передан экземпляр IProductsRepository:
public class AdminController : Controller
{
private readonly IProductsRepository _productsRepository;
public AdminController(IProductsRepository productsRepository)
{
_productsRepository = productsRepository;
}
МОЯ ПРОБЛЕМА:
Я не могу понять в своем контейнере IoC, где я связываю свой IProductsRepository с моим репозиторием, как зарегистрировать ISession, как передать ISession моему объекту MyProductsRepository при его создании и передать объект MyProductsRepository мой контроллер?