LCOV - code coverage report
Current view: top level - Ninject/Cqrs.Ninject/Configuration - NinjectDependencyResolver.cs Hit Total Coverage
Test: doc-coverage.info Lines: 6 7 85.7 %
Date: 2017-07-26

          Line data    Source code
       1             : using System;
       2             : using System.Collections.Generic;
       3             : using System.Linq;
       4             : using Cqrs.Configuration;
       5             : using Ninject;
       6             : using Ninject.Modules;
       7             : using Ninject.Parameters;
       8             : using Ninject.Syntax;
       9             : 
      10             : namespace Cqrs.Ninject.Configuration
      11             : {
      12             :         /// <summary>
      13             :         /// An implementation of <see cref="IDependencyResolver"/> using Ninject
      14             :         /// </summary>
      15             :         public class NinjectDependencyResolver : IDependencyResolver
      16           1 :         {
      17             :                 /// <summary>
      18             :                 /// The current instance of the <see cref="IDependencyResolver"/>.
      19             :                 /// </summary>
      20             :                 public static IDependencyResolver Current { get; protected set; }
      21             : 
      22             :                 /// <summary>
      23             :                 /// The inner Ninject <see cref="IKernel"/> used by this instance.
      24             :                 /// </summary>
      25             :                 protected IKernel Kernel { get; private set; }
      26             : 
      27             :                 /// <summary>
      28             :                 /// A collection of <see cref="INinjectModule"/> instances to load when we call <see cref="PrepareKernel"/>
      29             :                 /// </summary>
      30             :                 public static IList<INinjectModule> ModulesToLoad = new List<INinjectModule>();
      31             : 
      32             :                 /// <summary>
      33             :                 /// A user supplied <see cref="Func{TResult}"/> that will be called during <see cref="Start"/> to create and populate <see cref="Current"/>.
      34             :                 /// </summary>
      35             :                 public static Func<IKernel, NinjectDependencyResolver> DependencyResolverCreator { get; set; }
      36             : 
      37             :                 /// <summary>
      38             :                 /// Instantiates a new instance of <see cref="NinjectDependencyResolver"/>
      39             :                 /// </summary>
      40             :                 /// <param name="kernel"></param>
      41           1 :                 public NinjectDependencyResolver(IKernel kernel)
      42             :                 {
      43             :                         Kernel = kernel;
      44             :                         BindDependencyResolver();
      45             :                 }
      46             : 
      47           0 :                 protected virtual void BindDependencyResolver()
      48             :                 {
      49             :                         bool isDependencyResolverBound = Kernel.GetBindings(typeof(IDependencyResolver)).Any();
      50             :                         if (!isDependencyResolverBound)
      51             :                         {
      52             :                                 Kernel.Bind<IDependencyResolver>()
      53             :                                         .ToConstant(this)
      54             :                                         .InSingletonScope();
      55             :                         }
      56             :                 }
      57             : 
      58             :                 /// <summary>
      59             :                 /// Starts the <see cref="NinjectDependencyResolver"/>
      60             :                 /// </summary>
      61             :                 /// <remarks>
      62             :                 /// this exists to the static constructor can be triggered.
      63             :                 /// </remarks>
      64           1 :                 public static void Start(IKernel kernel = null, bool prepareProvidedKernel = false)
      65             :                 {
      66             :                         if (kernel == null)
      67             :                         {
      68             :                                 kernel = new StandardKernel();
      69             :                                 prepareProvidedKernel = true;
      70             :                         }
      71             : 
      72             :                         if (DependencyResolverCreator != null)
      73             :                                 Current = DependencyResolverCreator(kernel);
      74             :                         else
      75             :                                 Current = new NinjectDependencyResolver(kernel);
      76             : 
      77             :                         if (prepareProvidedKernel)
      78             :                                 PrepareKernel(kernel);
      79             :                 }
      80             : 
      81             :                 /// <summary>
      82             :                 /// Calls <see cref="IKernel.Load(IEnumerable{INinjectModule})"/> passing in <see cref="ModulesToLoad"/>
      83             :                 /// </summary>
      84             :                 /// <param name="kernel">The <see cref="IKernel"/> the <see cref="ModulesToLoad"/> will be loaded into.</param>
      85           1 :                 public static void PrepareKernel(IKernel kernel)
      86             :                 {
      87             :                         kernel.Load
      88             :                         (
      89             :                                 ModulesToLoad
      90             :                         );
      91             :                 }
      92             : 
      93             :                 /// <summary>
      94             :                 /// Calls <see cref="IResolutionRoot.Resolve"/>
      95             :                 /// </summary>
      96           1 :                 public virtual T Resolve<T>()
      97             :                 {
      98             :                         return (T)Resolve(typeof(T));
      99             :                 }
     100             : 
     101             :                 /// <summary>
     102             :                 /// Calls <see cref="IResolutionRoot.Resolve"/>
     103             :                 /// </summary>
     104           1 :                 public virtual object Resolve(Type serviceType)
     105             :                 {
     106             :                         return Kernel.Resolve(Kernel.CreateRequest(serviceType, null, new Parameter[0], true, true)).SingleOrDefault();
     107             :                 }
     108             :         }
     109             : }

Generated by: LCOV version 1.10