我肯定会使用
委托工厂来避免对IoC容器本身的依赖。通过使用键控服务查找,您的代码/工厂将与Autofac紧密耦合。
这是一个很好且干净的例子,没有任何对Autofac的依赖:
策略:
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()"); } };
你想要进行 switch 的枚举:
public enum ESomeEnum
{
UseStrategyA,
UseStrategyB,
}
消费策略的上下文:
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();
}
最后是容器配置:
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();
}
};
});
var container = builder.Build();
container.Resolve<Context>().DoSomething();
如果策略没有使用容器中注册的任何依赖项,您可以自己创建它们,并像这样简化配置:
var builder = new ContainerBuilder();
builder.RegisterType<Context>().AsSelf().SingleInstance();
builder.Register<Func<ESomeEnum, IStrategy>>(c => StrategyFactory.GetStrategy);
var container = builder.Build();
container.Resolve<Context>().DoSomething();
将 switch case 放在一个单独的类中:
public static class StrategyFactory
{
internal static IStrategy GetStrategy(ESomeEnum someEnum)
{
switch (someEnum)
{
case ESomeEnum.UseStrategyA:
return new ConcreteStrategyA();
case ESomeEnum.UseStrategyB:
return new ConcreteStrategyB();
default:
throw new ArgumentException();
}
}
}
完整的运行代码示例 - 在.NET Fiddle中检查:
using Autofac;
using System;
using System.Reflection;
namespace Samples.Autofac.StrategyPattern
{
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();
}
}
public class AutofacExample
{
public static void Main()
{
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();
}
};
});
var container = builder.Build();
container.Resolve<Context>().DoSomething();
}
}
}
someEnum
是一个配置设置还是运行时值? - Yacoub Massad