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