Today, I am immersed in IOC's pursuit of design patterns. I have listened to many lectures and seen many examples. This is a test project of mine.
In order to test the code, I first prepared two classes: Car and ICar. These two classes are similar to DAL and IDAL in our usual project. Now let's start!
1. First add a class InitAutofac to our program
By the way, this is my assembly.
This is a little bit of a problem, because I have ICar and Car in the SourceClass file, but it's not a big problem.
2. Now let's quote Autofac
3. Download Autofac
There may be a lot of examples of autofac searched on the Internet, but I want to tell you, this version number is very important! Because this code won't work after 4.0
What is this? Let's not look at what happened before the new keyword. A careful buddy will see if this config is a configuration file. That's right! That's how configuration file injection works. Remember, autofac doesn't support this since 4.0!
Recommend using xml, or json file to inject! Here's just a mention of looking down!
builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
It's also downloadable!
4. View References + Add Codes
Are these two things added? Now that the tools are available, it's time for our code to come on the scene! __________?
First is the interface class of ICar.
public interface ICar { void Engine(int value); void Run(); }
It defines the number of gears the engine starts and the way the car runs, because that's the most basic way the car runs.
And then it's written in Car.
You can see there are three cars in our garage. In fact, I like BMW 23333 best!
Of course, all three cars inherit the ICar interface to implement the method.
public class AudiCar : ICar { private static int Speed; public void Engine(int value) { int Row = value * 300; Speed = Row / 13; } public void Run() { Console.WriteLine("Audi car is in the middle of{0}Code speed!",Speed); } } public class BenzCar : ICar { private static int Speed; public void Engine(int value) { int Row = value * 300; Speed = Row / 12; } public void Run() { Console.WriteLine("Mercedes-Benz is on its way{0}Code speed!", Speed); } } public class FordCar : ICar { private static int Speed; public void Engine(int value) { int Row = value * 300; Speed = Row / 15; } public void Run() { Console.WriteLine("Ford is on the way{0}Code speed!", Speed); } }
Looking at the code in our Program class at this time, I registered a driver to drive the car.
class Program { static void Main(string[] args) { Person person = new Person(); person.Driver(4); Console.Read(); } } public class Person { public void Driver(int value) { ICar car = new AudiCar(); car.Engine(value); car.Run(); } }
As you can see, the driver is in gear and can drive the Audi. Let's run it for a while.
Audi car can run, that means our code is not a problem, but!!!
The driver's code refers to two classes, one is the interface class and the other is the implementation class of the interface (if you don't understand what I said, please make good use of the interface and class), which is not what we want.
Because if the car breaks down, then the Audi can't drive at this time, so the driver has to change! But in the project, this class is equivalent to compiled, you can no longer modify this class, at this time you can only see the program is wrong!
So in order to solve this problem, we need to use the principle of inversion of dependency! Why use the principle of inversion of dependency? Because I have to rely on abstraction rather than detail, so that I can drive as long as I give him a car, no matter what mistakes the program makes.
Then the driver's dependence on the car will become loose, the driver will not only drive Audi's driver, it can also drive other cars to go for a ride!
Understanding this phrase shows that you are a programmer who can completely get started!
Of course, this is not our topic. Now we need to modify the code to make the car abstract.
5.IOC Framework!
First add the following code to our original InitAutofac class
public static class InitAutofac { static ContainerBuilder _Builder;//Statement container public static void InitAutofacs() { _Builder = new ContainerBuilder();//instantiation _Builder.RegisterType<AudiCar>().As<ICar>();//Registration of Audi Vehicles to Containers //_Builder.RegisterType<BenzCar>().As<ICar>();//Registration of Mercedes-Benz Vehicles to Containers } static IContainer _container;//Declare a field. This field is used to dock containers. static IContainer Container //Transfer docked content into this property! { get { if (_container == null) { _container = _Builder.Build(); } return _container; } } public static T GetFromFac<T>()//Define a method to be called externally so that the vehicle can be called { T t = Container.Resolve<T>();//Return classes that have been registered in containers----AudiCar! return t; } }
Through this class, the form of invocation in the Program class is as follows
class Program { static void Main(string[] args) { Person person = new Person(); person.Driver(4); Console.Read(); } } public class Person { public void Driver(int value) { InitAutofac.InitAutofacs(); ICar car = InitAutofac.GetFromFac<ICar>(); car.Engine(value); car.Run(); } }
Running Discovery, Running!This is the result! It shows that we have succeeded in using this tool.
But I commented a piece of code in InitAutofac!
// Builder. RegisterType < BenzCar >(). As < ICar >(); // Mercedes-Benz cars that will be used will be registered in containers
That's it. Now I cancel the comment and run it.???
Find out that the Audi has changed to a Mercedes Benz! Where did our Audi go? I passed the breakpoint test.
We have reached the breakpoint, then we continue to implement, and found that the result of the operation is still Mercedes-Benz! Explain that the Audi car was covered when it was registered into the container!
This is a problem. In the next article, I will continue to explain the various methods of using IOCAutofac containers.
Through this example, I deeply understand the power of IOC, making Abstract Programming easier!
This blog is only for visit, reprint and collection value is not high, just a test, suitable for beginners of programmers, big guys do not spray, if you find concept understanding error, please leave a message in time, I will correct the content of the blog or delete the blog.
It's 01:39 in the morning. Okay, I'm going to bed!