[英]Registering Class to Ninject and injecting that class in Constructor
I am using NHibernate, and I have a BootStrapper class that has a method that returns an ISession. 我正在使用NHibernate,我有一个BootStrapper类,它有一个返回ISession的方法。 In my Repository class I am passing it an ISession Object. 在我的Repository类中,我传递了一个ISession对象。 In my Controller, I am passing its constructor an IRepository object. 在我的Controller中,我将其构造函数传递给IRepository对象。
I have been successful in binding my IRepository to Repository class, but I can't figure out how to bind/register my Repository class so that it receives a ISession object from the BootStrapper class when it is instantiated and bind my controller to recieve a IRepository object when it is created. 我已成功将我的IRepository绑定到Repository类,但我无法弄清楚如何绑定/注册我的Repository类,以便它在实例化时从BootStrapper类接收一个ISession对象并绑定我的控制器以接收一个IRepository对象创建时。
My Code: 我的代码:
public interface IProductsRepository
{
IQueryable<Product> Products { get; }
void SaveProduct(Product product);
}
public class MySqlProductsRepository : IProductsRepository
{
private readonly ISession _session;
public MySqlProductsRepository(ISession session)
{
_session = session;
}
public IQueryable<Product> Products
{
get
{
return _session.CreateCriteria(typeof (Product)).List<Product>().AsQueryable();
}
}
public class BootStrapper
{
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();
}
}
public class AdminController : Controller
{
private readonly IProductsRepository _productsRepository;
public AdminController(IProductsRepository productsRepository)
{
_productsRepository = productsRepository;
}
}
public class NinjectControllerFactory : DefaultControllerFactory
{
private readonly IKernel _kernel = new StandardKernel(new DaisyBlossomsServices());
protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
return null;
return (IController) _kernel.Get(controllerType);
}
public class DaisyBlossomsServices : NinjectModule
{
public override void Load()
{
Bind<IProductsRepository>().To<MySqlProductsRepository>();
}
}
}
How do I bind a ISession to MySqlProductsRepository so it receives an ISession object for its constructor, and how do I bind it so my controller receives an IProductsRepository in its constructor? 如何将ISession绑定到MySqlProductsRepository,以便它为其构造函数接收ISession对象,以及如何绑定它,以便控制器在其构造函数中接收IProductsRepository?
you can bind ISession to a Provider. 您可以将ISession绑定到提供程序。 In this case you can change the BootStrapper class to inherit from Provider and implement the CreateInstance method. 在这种情况下,您可以更改BootStrapper类以从Provider继承并实现CreateInstance方法。
That would look like that: 那看起来像那样:
public class BootStrapper : Provider<ISession>
{
.....
protected override ISession CreateInstance(IContext context)
{
return GetSession();
}
....
}
After that just add the binding to your modul: 之后,只需将绑定添加到您的模块中即可:
public class DaisyBlossomsServices : NinjectModule
{
public override void Load()
{
Bind<IProductsRepository>().To<MySqlProductsRepository>();
Bind<ISession>().ToProvider<BootStrapper>();
}
}
That should do it. 应该这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.