使用ninject不起作用的依赖注入

时间:2018-01-25 18:18:37

标签: c# wpf visual-studio dependency-injection ninject

我的目标很简单:我希望已经存在的类“MainWindowVM”(实现IMainWindowVM)的实例被注入到类“StaticTestsResultsViewModel”中。我不想(!)想要实例化类“MainWindowVM”的新实例。相反,我希望将此类已存在的实例注入到“StaticTestsResultsViewModel”类中。

2 个答案:

答案 0 :(得分:1)

  

我的目标很简单:我希望已经存在的类“MainWindowVM”(实现IMainWindowVM)的实例被注入到类“StaticTestsResultsViewModel”中。我不想(!)想要实例化类“MainWindowVM”的新实例。相反,我希望将此类已存在的实例注入到“StaticTestsResultsViewModel”类中。

在这种情况下,您应该将其注册为:

kernel.Bind<IMainWindowVM>().To<MainWindowVM>().InSingletonScope();

请记住,当您将类注册为单例时,这会隐式地使其所有依赖项单例化。见Captive Dependency。如果这对您的应用程序设计来说是不可接受的,那么您应该将存储共享属性的单例实例移动到MainWindowVM而不是 make MainWindowVM单例的依赖项(例如瞬态)。

                          PropertyHolder (Singleton)
                         /
MainWindowVM (Transient)
                        \
                          OtherDependency (Any scope shorter than singleton)

答案 1 :(得分:-1)

几个月后我遇到了同样的问题。 删除从nugget安装Ninject时创建的类,并将以下类粘贴到同一目录中。从我记得它不需要任何其他配置。让我知道,

你真诚的。

using Ninject;
using Ninject.Modules;
using Project.Data;
using Project.Interfaces;
using Project.Managers;
using Project.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Http;
using System.Web.Http.Dependencies;


namespace Project.App_Start
{
    /// <summary>
    /// Resolves Dependencies Using Ninject
    /// </summary>
    public class NinjectHttpResolver : IDependencyResolver, IDependencyScope
    {
        public IKernel Kernel { get; private set; }
        public NinjectHttpResolver(params NinjectModule[] modules)
        {
            Kernel = new StandardKernel(modules);
        }

        public NinjectHttpResolver(Assembly assembly)
        {
            Kernel = new StandardKernel();
            Kernel.Load(assembly);
        }

        public object GetService(Type serviceType)
        {
            return Kernel.TryGet(serviceType);
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return Kernel.GetAll(serviceType);
        }

        public void Dispose()
        {
            //Do Nothing
        }

        public IDependencyScope BeginScope()
        {
            return this;
        }
    }


    // List and Describe Necessary HttpModules
    // This class is optional if you already Have NinjectMvc
    public class NinjectHttpModules
    {
        //Return Lists of Modules in the Application
        public static NinjectModule[] Modules
        {
            get
            {
                return new[] { new MainModule() };
            }
        }

        //Main Module For Application
        public class MainModule : NinjectModule
        {
            public override void Load()
            {

                //Configure Your Bindings Here e.g


                Kernel.Bind<ILoginManager>().To<LoginManager>();
               >
    /// Its job is to Register Ninject Modules and Resolve Dependencies
    /// </summary>
    public class NinjectHttpContainer
    {
        private static NinjectHttpResolver _resolver;

        //Register Ninject Modules
        public static void RegisterModules(NinjectModule[] modules)
        {
            _resolver = new NinjectHttpResolver(modules);
            GlobalConfiguration.Configuration.DependencyResolver = _resolver;
        }

        public static void RegisterAssembly()
        {
            _resolver = new NinjectHttpResolver(Assembly.GetExecutingAssembly());
            //This is where the actual hookup to the Web API Pipeline is done.
            GlobalConfiguration.Configuration.DependencyResolver = _resolver;
        }

        //Manually Resolve Dependencies
        public static T Resolve<T>()
        {
            return _resolver.Kernel.Get<T>();
        }
    }
}