我一直在阅读这两种解决依赖关系的方法,并找到了一些用于ninject实现的示例代码。

对于服务定位器遵循类似

 public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver
 {
    IKernel kernel;

    public NinjectDependencyResolver(IKernel kernel)
        : base(kernel)
    {
        this.kernel = kernel;
    }

    public IDependencyScope BeginScope()
    {
        return new NinjectDependencyScope(kernel.BeginBlock());
    }
}




public class NinjectDependencyScope : IDependencyScope
{
    IResolutionRoot resolver;

    public NinjectDependencyScope(IResolutionRoot resolver)
    {
        this.resolver = resolver;
    }

    public object GetService(Type serviceType)
    {
        if (resolver == null)
            throw new ObjectDisposedException("this", "This scope has been disposed");

        return resolver.TryGet(serviceType);
    }

    public System.Collections.Generic.IEnumerable<object> GetServices(Type serviceType)
    {
        if (resolver == null)
            throw new ObjectDisposedException("this", "This scope has been disposed");

        return resolver.GetAll(serviceType);
    }

    public void Dispose()
    {
        IDisposable disposable = resolver as IDisposable;
        if (disposable != null)
            disposable.Dispose();

        resolver = null;
    }
}


开箱即用类

    public static class NinjectWebCommon
{
    private static readonly Bootstrapper bootstrapper = new Bootstrapper();

    /// <summary>
    /// Starts the application
    /// </summary>
    public static void Start()
    {
        DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
        DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
        bootstrapper.Initialize(CreateKernel);
    }

    /// <summary>
    /// Stops the application.
    /// </summary>
    public static void Stop()
    {
        bootstrapper.ShutDown();
    }

    /// <summary>
    /// Creates the kernel that will manage your application.
    /// </summary>
    /// <returns>The created kernel.</returns>
    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        try
        {
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

            RegisterServices(kernel);
            GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);

            return kernel;
        }
        catch
        {
            kernel.Dispose();
            throw;
        }
    }

    /// <summary>
    /// Load your modules or register your services here!
    /// </summary>
    /// <param name="kernel">The kernel.</param>
    private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<MembersService>().To<MembersService>();
        kernel.Bind<MemberContext>().To<MemberContext>();
    }


对于组合根,我遵循-https://gist.github.com/paigecook/3860942

 public class NinjectKernelActivator: IHttpControllerActivator
{
    private readonly IKernel _kernel;

    public NinjectKernelActivator(IKernel kernel)
    {
        _kernel = kernel;
    }

    public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
    {
        var controller = (IHttpController) _kernel.Get(controllerType);

        request.RegisterForDispose( new Release(()=> _kernel.Release(controller)));

        return controller;
    }
}

internal class Release : IDisposable
{
    private readonly Action _release;

    public Release(Action release)
    {
        _release = release;
    }

    public void Dispose()
    {
        _release();
    }
}


并对NinjectWebCommon中的Create(..)进行了单个更改。

                //GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
            GlobalConfiguration.Configuration.Services.Replace(
              typeof(IHttpControllerActivator),
                new NinjectCompositionRoot(kernel));


编辑

控制器和服务创建

public class MembersController : ApiController
{
    private readonly IMembersService _membersService;

    public MembersController(IMembersService membersService)
    {
        _membersService = embersService;
    }

    ...
}


public class MembersService : IMembersService
{
    private readonly MembersContext _context;

    public MembersService(MemberContext context)
    {
        _context = context;
    }

    ...
}


我是否正确实施了合成根目录?
我真的看不出这两种方法之间的区别是什么?

最佳答案

Composition root(这是您应该执行依赖项注入的方式)与Service Locator之间的区别在于,组合根目录应位于应用程序的一个位置(尽可能靠近应用程序的入口点)。这并不意味着它将仅被调用一次。例如,在MVC / WebAPI的情况下,组合根的好地方是控制器工厂,该工厂为每个HTTP请求应用程序接收的创建控制器。关键是,在控制器工厂中实现的合成根应创建处理该请求所需的整个对象图(具有其所有依赖关系的控制器),以便在此请求期间无需从容器中单独解析其他依赖关系。

另一方面,服务定位器是一种在需要时从服务定位器检索依赖项的方法。服务定位器在您的应用程序中成为环境上下文(通常提供静态ServiceLocator.Get<T>()方法)。服务定位器与Dependency Injection相反,因为无需注入依赖项,而是在需要它们时检索它们。这意味着在整个应用程序代码中都具有ServiceLocator.Get<T>()方法调用,并且应用程序的所有层都依赖于服务定位器。这种方法有几个缺点,其中之一是它使代码更难进行单元测试,因为所有测试都需要与同一全局服务定位器类进行交互以设置被测类的虚假依赖关系。

假设您没有在公共静态属性中的其他地方公开NinjectKernelActivator以便稍后使用它来获取不注入的依赖项,则复合根的IKernel实现是正确的。

10-08 17:29