简体   繁体   English

将Class注册到Ninject并在Constructor中注入该类

[英]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.

相关问题 使用Ninject注入类库中,其中构造函数使用多个参数 - Injecting into a class library with Ninject, where the constructor takes multiple arguments 使用NInject在WPF中注入没有无参数构造函数的viewmodel类 - injecting viewmodel class without parameterless constructor in WPF with NInject 使用ninject注入其他程序集中的类 - Injecting a class that is in other assembly using ninject 使用 Autofac 在构造函数中注入带有 IOptions 的类 - Injecting a class with IOptions in the constructor with Autofac Ninject字段注入不适用于构造函数的类 - Ninject field inject not work in class with constructor 使用参数构造函数和 Ninject 配置 Automapper 配置文件类 - Configure Automapper Profile Class with Parameter Constructor and Ninject 使用简单注入器将配置类注入构造函数 - Injecting a configuration class into a constructor using Simple Injector 使用Ninject工厂方法将IEnumerable注入构造函数 - Injecting an IEnumerable into a constructor with a Ninject factory method Ninject 传递构造函数参数 typeof 实现接口的类 - Ninject pass constructor argument typeof class that implements the interface 是否可以使用Ninject在注入的类构造函数中解析URL? - Is it possible to resolve a URL in an injected class constructor using Ninject?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM