Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.45 KB | None | 0 0
  1. using Ninject;
  2. using Ninject.Activation;
  3. using Ninject.Extensions.Factory;
  4. using Ninject.Planning.Targets;
  5. using System;
  6. using System.Linq;
  7.  
  8. namespace DI
  9. {
  10.     public class D1
  11.     {
  12.         public string A { get; }
  13.  
  14.         public D1(string a)
  15.         {
  16.             A = a;
  17.         }
  18.     }
  19.  
  20.     public class D2
  21.     {
  22.         public D1 D1 { get; }
  23.         public string B { get; }
  24.  
  25.         public D2(D1 d1, string b)
  26.         {
  27.             D1 = d1;
  28.             B = b;
  29.         }
  30.     }
  31.  
  32.     public class D3
  33.     {
  34.         public D4 D4 { get; }
  35.  
  36.         public D3(D4 d4)
  37.         {
  38.             D4 = d4;
  39.         }
  40.     }
  41.  
  42.     public class D4
  43.     {
  44.         public D1 D1 { get; }
  45.  
  46.         public D4(D1 d1)
  47.         {
  48.             D1 = d1;
  49.         }
  50.     }
  51.  
  52.     public class Algorithm
  53.     {
  54.         public D1 D1 { get; }
  55.         public D2 D2 { get; }
  56.         public D3 D3 { get; }
  57.  
  58.         public Algorithm(D1 d1, D2 d2, D3 d3)
  59.         {
  60.             D1 = d1;
  61.             D2 = d2;
  62.             D3 = d3;
  63.         }
  64.     }
  65.  
  66.     public interface IAlgorithmFactory
  67.     {
  68.         Algorithm Create((string a, string b) input);
  69.     }
  70.  
  71.     class Program
  72.     {
  73.         static void Main(string[] args)
  74.         {
  75.             var a = "valueOfA";
  76.             var b = "valueOfB";
  77.             var algorithm = GetAlgorithmUsingNInject(a, b);
  78.  
  79.             // Conditions
  80.             Console.WriteLine(algorithm.D1.A == a);
  81.             Console.WriteLine(algorithm.D2.B == b);
  82.             Console.WriteLine(algorithm.D2.D1 == algorithm.D1);
  83.             Console.WriteLine(algorithm.D3.D4.D1 == algorithm.D1);
  84.         }
  85.  
  86.         private static Algorithm GetAlgorithmUsingPureDi(string a, string b)
  87.         {
  88.             var d1 = new D1(a);
  89.             var d2 = new D2(d1, b);
  90.             var d4 = new D4(d1);
  91.             var d3 = new D3(d4);
  92.             var algorithm = new Algorithm(d1, d2, d3);
  93.  
  94.             return algorithm;
  95.         }
  96.  
  97.         private static Algorithm GetAlgorithmUsingNInject(string a, string b)
  98.         {
  99.             var scope = new object();
  100.  
  101.             var kernel = new StandardKernel(new FuncModule());          
  102.             kernel.Bind<Algorithm>().ToSelf();
  103.             kernel.Bind<IAlgorithmFactory>().ToFactory();
  104.             kernel.Bind<D1>().ToSelf().InScope(x => scope).WithConstructorArgument("a", ArgumentValueCallback(tuple => tuple.a));
  105.             kernel.Bind<D2>().ToSelf().InScope(x => scope).WithConstructorArgument("b", ArgumentValueCallback(tuple => tuple.b));
  106.             kernel.Bind<D3>().ToSelf().InScope(x => scope);
  107.             kernel.Bind<D4>().ToSelf().InScope(x => scope);
  108.  
  109.             return kernel.Get<IAlgorithmFactory>().Create((a,b));
  110.         }
  111.  
  112.         private static Func<IContext, ITarget, object> ArgumentValueCallback(Func<(string a, string b), object> extractionFunction)
  113.         {
  114.             return (context, target) =>
  115.             {
  116.                 // Find the requested parameter with the name "input"
  117.                 var inputParameter = context
  118.                         .Parameters
  119.                         .Where(p => p.Name == "input")
  120.                         .Single();
  121.  
  122.                 // Extract the input tuple (we know it should be (string, string))
  123.                 var input = (ValueTuple<string,string>)inputParameter.GetValue(context, target);
  124.  
  125.                 // Extract the value
  126.                 return extractionFunction(input);
  127.             };
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement