Autofac 多個型別與策略模式的使用

C#知識系列

筆記一下Autofac多的實作類別

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net.Mime;
using Autofac;
using Autofac.Core;
using IContainer = Autofac.IContainer;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            var config = Configure();
            var bll = config.Resolve<Bll>();
            bll.Run();
            //var d = config.ResolveKeyed<IAnimal>(Type.Dog); 
            //d.Run();
            //var c = config.ResolveKeyed<IAnimal>(Type.Cat);
            //c.Run();
          
        }
        public static IContainer Configure()
        {
            var builder = new ContainerBuilder();
            builder.RegisterType<Dog>().Keyed<IAnimal>(Type.Dog);
            builder.RegisterType<Cat>().Keyed<IAnimal>(Type.Cat);
            builder.RegisterType<Bll>();
            return builder.Build();
        }
    }

    public class Bll
    {
        public void Run()
        {
            Console.Write("test");
        }
    }
    public enum Type
    {
        Dog,
        Cat
    }
    public interface IAnimal
    {
        void Run();
    }

    public class Dog : IAnimal
    {
        public void Run()
        {
            Console.WriteLine("汪");
        }
    }

    public class Cat : IAnimal
    {
        public void Run()
        {
            Console.WriteLine("喵");
        }
    }
}

如果是策略模式的話該怎麼用?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net.Mime;
using System.Reflection;
using Autofac;
using Autofac.Core;
using IContainer = Autofac.IContainer;

namespace ConsoleApp2
{

    class Program
    {
        static void Main(string[] args)
        {
            var config = Configure();
            var bll = config.Resolve<Bll>();
            bll.Run();
            var context = config.Resolve<Context>();
            context.DoSomething();
            //var d = config.ResolveKeyed<IAnimal>(Type.Dog); 
            //d.Run();
            //var c = config.ResolveKeyed<IAnimal>(Type.Cat);
            //c.Run();

        }
        public static IContainer Configure()
        {
            var builder = new ContainerBuilder();

            builder.RegisterType<Context>().AsSelf().SingleInstance();
            builder.RegisterAssemblyTypes(Assembly.GetAssembly(typeof(IStrategy)))
                .Where(t => typeof(IStrategy).IsAssignableFrom(t))
                .AsSelf();
            builder.Register<Func<ESomeEnum, IStrategy>>(c =>
            {
                var cc = c.Resolve<IComponentContext>();
                return (someEnum) =>
                {
                    switch (someEnum)
                    {
                        case ESomeEnum.UseStrategyA:
                            return cc.Resolve<ConcreteStrategyA>();
                        case ESomeEnum.UseStrategyB:
                            return cc.Resolve<ConcreteStrategyB>();
                        default:
                            throw new ArgumentException();
                    }
                };
            });
            builder.RegisterType<Bll>();
            builder.RegisterType<Context>();
            return builder.Build();
        }
    }

    public class Bll
    {
        public void Run()
        {
            Console.WriteLine("test");
        }
    }

    public interface IStrategy
    {
        void Do();
    }

    public class ConcreteStrategyA : IStrategy
    {
        public void Do()
        {
            Console.WriteLine("Called ConcreteStrategyA.Do()");
        }
    };

    public class ConcreteStrategyB : IStrategy
    {
        public void Do()
        {
            Console.WriteLine("Called ConcreteStrategyB.Do()");
        }
    };

    public enum ESomeEnum
    {
        UseStrategyA, UseStrategyB,
    }

    public class Context
    {
        private readonly Func<ESomeEnum, IStrategy> _strategyFactory;

        public Context(Func<ESomeEnum, IStrategy> strategyFactory)
        {
            _strategyFactory = strategyFactory;
        }

        public void DoSomething()
        {
            _strategyFactory(ESomeEnum.UseStrategyB).Do();
            _strategyFactory(ESomeEnum.UseStrategyA).Do();
        }
    }
}

 

 

參考來源:

https://blog.darkthread.net/blog/autofac-notes-6/

https://dotnetfiddle.net/ABCBiA

 

元哥的筆記