PageRenderTime 183ms CodeModel.GetById 90ms app.highlight 5ms RepoModel.GetById 86ms app.codeStats 0ms

/examples/ioc/PropertyInjectionUsingFluentInterfaces/Program.cs

http://github.com/philiplaureano/LinFu
C# | 97 lines | 67 code | 21 blank | 9 comment | 0 complexity | c01b350de3aff1361f32e583d4d65dec MD5 | raw file
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5using CarLibrary3;
 6using CarLibrary4;
 7using LinFu.IoC;
 8using LinFu.IoC.Interfaces;
 9
10namespace PropertyInjectionUsingFluentInterfaces
11{
12    class Program
13    {
14        static void Main(string[] args)
15        {
16            string directory = AppDomain.CurrentDomain.BaseDirectory;
17            IServiceContainer container = new ServiceContainer();
18
19            // Load CarLibrary3.dll; If you need load
20            // all the libaries in a directory, use "*.dll" instead
21            container.LoadFrom(directory, "CarLibrary3.dll");
22
23            // Inject the OldPerson type
24            container.Inject<IPerson>("OldPerson").Using<Person>();
25            container.Initialize<IPerson>("OldPerson")
26                .With(p =>
27                {
28                    p.Age = 99;
29                    p.Name = "OldPerson";
30                });
31
32            // Inject the YoungPerson type
33            container.Inject<IPerson>("YoungPerson").Using<Person>();
34            container.Initialize<IPerson>("YoungPerson")
35                .With(p =>
36                {
37                    p.Age = 16;
38                    p.Name = "YoungPerson";
39                });
40
41            // Inject the DeadEngine type
42            container.Inject<IEngine>("DeadEngine").Using<DeadEngine>()
43                .OncePerRequest();
44
45            // Inject the OldEngine type
46            container.Inject<IEngine>("OldEngine").Using<OldEngine>()
47                .OncePerRequest();
48
49            // Inject the BrokenVehicle type into the container
50            container.Inject<IVehicle>("BrokenVehicle")
51                .Using<Car>().OncePerRequest();
52
53            #region Broken Vehicle Configuration
54
55            AddVehicle(container, "BrokenVehicle", "DeadEngine", "YoungPerson");
56
57            #endregion
58
59            #region Old Vehicle Configuration
60
61            AddVehicle(container, "OldVehicle", "OldEngine", "OldPerson");
62
63            #endregion
64
65            // Inject the OldVehicle type into the container
66            container.Inject<IVehicle>("OldVehicle")
67                .Using<Car>().OncePerRequest();
68
69            Person person = new Person();
70            person.Name = "Someone";
71            person.Age = 18;
72
73            container.AddService<IPerson>(person);
74            var brokenVehicle = container.GetService<IVehicle>("BrokenVehicle");
75            var oldVehicle = container.GetService<IVehicle>("OldVehicle");
76
77            Console.Write("Broken Vehicle: ");
78            brokenVehicle.Move();
79
80            Console.Write("Old Vehicle: ");
81            oldVehicle.Move();
82
83            Console.WriteLine("Press ENTER to continue...");
84            Console.ReadLine();
85        }
86
87        private static void AddVehicle(IServiceContainer container, string vehicleName, string engineName, string personName)
88        {
89            container.Initialize<IVehicle>(vehicleName)
90                .With((ioc, vehicle) => vehicle.Engine = ioc.GetService<IEngine>(engineName));
91
92            // Set the person type
93            container.Initialize<IVehicle>(vehicleName)
94                .With((ioc, vehicle) => vehicle.Driver = ioc.GetService<IPerson>(personName));
95        }
96    }
97}