IOC containers use Autofac

Today, I was immersed in the pursuit of the IOC design patterns, listening to a lot of class, also saw many examples, this is one of my test project

To test the code, I first prepared two classes a Car and interfaces ICar, these two classes and we usually similar project in DAL with IDAL, and now we start!

1. First, add a class in our program InitAutofac

Yes, this is my assembly

This little problem, because I ICar and Car SourceClass on the document but not a big problem

 

2. Now we quote Autofac

3. Download Autofac

May have a small partner in the online search autofac many examples, but I want to tell you, this version is very important! Because of this code can not run after 4.0

What this is it? We do not look at things before the new keyword, attentive junior partner will see this is not the config configuration file ah, yes! This is the method by injecting the configuration file, remember, from after 4.0 autofac does not support such an approach! 
Recommended xml, json file or way to inject! Here we mention just look down!
 builder.RegisterModule(new ConfigurationSettingsReader("autofac"));

This thing must download!

See reference 4. Add code +

 

Is not add these two things, and now have tools, we have the code to debut!

The first is the interface class ICar

public interface ICar {
        void Engine(int value);
        void Run();
    }

里面定义了一个引擎发动的档数和汽车的跑方法,因为这是汽车运行的最基本方法吧

然后到Car中是这么写的

可以看到我们的车库有三款车,其实我最喜欢宝马23333!

当然这三款车都继承了ICar的接口去实现方法

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("奥迪车正以{0}码的速度行驶!",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("奔驰车正以{0}码的速度行驶!", 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("福特车正以{0}码的速度行驶!", Speed);
        }
    }

这个时候在看看我们的Program类里面的代码,我注册了一个司机,让他去驾驶车辆

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();
        }
    }

可以看到,司机挂挡然后可以开着奥迪车了,那我们运行一下看看

 

奥迪车可以运行了,说明我们的代码没有问题,但是!!!

司机这段代码引用了两个类,一个是接口类,一个是接口的实现类(如果我说的你不明白,请你好好补习接口与类),这并不是我们想要的,

因为如果车坏了,那么此时奥迪车就已经不能开了,那么司机就必须要换车!但是在项目中,这个类相当于已经被编译好了,你已经不能再修改这个类了,此时你就只能看着程序出错了!

所以为了解决这个问题,我们就需要用到依赖倒置原则!为什么要用依赖倒置原则呢?因为我要依赖抽象而不是依赖细节,这样我不管程序出现了什么错误只要我给他一个车他都能开,

那么司机对于车的依赖就会变得松散了,司机就不只是只会开奥迪的司机了,它还可以开其他的车去兜风了!

理解了这段话,说明你已经是可以完全入门的程序员了!

当然这不是我们的主题,现在我们就需要修改代码让这个车变得抽象化

5.IOC框架登场!

首先在我们原先定义的InitAutofac类中添加以下代码

public static class InitAutofac
    {
        static ContainerBuilder _Builder;//申明容器
        public static void InitAutofacs() {
            _Builder = new ContainerBuilder();//实例化
            _Builder.RegisterType<AudiCar>().As<ICar>();//将需要用到的奥迪车注册到容器中
            //_Builder.RegisterType<BenzCar>().As<ICar>();//将需要用到的奔驰车注册到容器中
        }
        static IContainer _container;//申明一个字段这个字段用来对接容器

        static IContainer Container //将对接的内容传输入这个属性!
        {
            get
            {
                if (_container == null)
                {
                    _container = _Builder.Build(); 
                }
                return _container;
            }
        }
        public static T GetFromFac<T>()//定义一个方法在外部调用,使得可以调用车辆
        {
            T t = Container.Resolve<T>();//回传已经被注册在容器内的类----AudiCar!
            return t;
        }
    }

通过这个类,在Program类中调用的形式如下

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();
        }
    }

运行发现,可以运行!这个是结果!说明我们使用这个工具已经成功了

但是我在InitAutofac中注释了一段代码!

//_Builder.RegisterType<BenzCar>().As<ICar>();//将需要用到的奔驰车注册到容器中

就是这个,现在我取消注释后运行,???

发现Audi车改为奔驰车了!那我们的Audi车去哪里了呢?我通过断点测试看

 

 已经进入到断点了,那我们继续执行,发现运行的结果还是奔驰车!说明,奥迪车在注册进入容器的时候被覆盖了!

 这个就是一个问题了,下一篇我就继续讲解IOCAutofac容器的各个方法使用!

通过这个例子我深刻了解到了IOC的强大,使得抽象编程变的会更简单!

这篇博客仅供参观,转载和收藏价值不高,只是一个测试,适合刚入门的程序员,大佬勿喷,如果发现概念理解错误,请及时留言,我会更正博客内容或删除博客

实打实原创小例子 现在是早上01点39分,好了我要去睡觉了!

Guess you like

Origin www.cnblogs.com/sandaman2019/p/10971217.html