如何使用运行时参数轻松管理适配器和/或装饰器?

Kug*_*gel 3 c# inversion-of-control autofac

我将举一个非常简单的例子.

class Implementation: IMyInterface
{
    string mArg;

    public Implementation(string arg)
    {
        mArg = arg;
    }

    public DoStuff(object param)
    {
        // snip
    }
}

class Decorator: IMyInterface
{
    IMyInterface mWrapped;

    public Decorator(IMyInterface wrapped)
    {
        mWrapped = wrapped;
    }

    public DoStuff(object param)
    {
        var result = mWrapped.DoStuff(param);

        // snip

        return result;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,我需要实现构造函数的参数,我在运行时从用户那里得到.

IMyInterface GetMyObject()
{
    string arg = AskUserForString();

    return mContext.Resolve // HOW?
}
Run Code Online (Sandbox Code Playgroud)

那么设置它并解析装饰实例的正确方法是什么?

这个例子很简单.但是想象有更多的层(装饰器/适配器)和最里面的实现需要参数我在运行时获得.

The*_*ser 5

您可以为实现和每个装饰器创建一个工厂对象,并让装饰器的工厂像实现工厂的装饰器一样,只是将输入传递给resolve调用.您可能在应用程序启动时或在某些配置文件中设置的那些.

为了非常灵活,我会建立一个需要的工厂

Func<IMyInterface, IMyInterface>
Run Code Online (Sandbox Code Playgroud)

甚至是

Func<IMyInterface, string, IMyInterface>)
Run Code Online (Sandbox Code Playgroud)

所以它可以构建你想要的任何类型的IMyInterface implmentation.

所以这样的事情

interface IMyInterfaceFactory
{
   IMyInterface Resolve(string parameter);
}

class DecoratorFactory : IMyInterfaceFactory
{
   IMyInterfaceFactory parent;

   Func<IMyInterface, string, IMyInterface> resolver;

   public DecoratorFactory(
      IMyInterfaceFactory parent, 
      Func<IMyInterface, string, IMyInterface> resolver)
   {
      this.parent = parent;
      this.resolver= resolver;
   }

   public IMyInterface Resolve(string parameter)
   {
      var decoratee = parent.Resolve(parameter);

      return resolver(decoratee, parameter);
   }
}
Run Code Online (Sandbox Code Playgroud)

如果您需要在Resolve方法中执行更复杂的操作,例如在多个父级或解析程序委托之间进行选择,那么您可以为这些操作另外实现工厂接口.


Can*_*cer 5

我假设你正在使用autofac,因为它在问题的标签中.你可以尝试这些方面:

public class ImplementationFactory
{
    private readonly IComponentContext container;

    public ImplementationFactory(IComponentContext container)
    {
        this.container = container;

    }
    public IMyInterface GetImplementation(string arg)
    {
        return container.Resolve<IMyInterface>(new NamedParameter("arg", arg));            
    }
}
Run Code Online (Sandbox Code Playgroud)

注册/解决部分:

var builder = new ContainerBuilder();
builder.RegisterType<ImplementationFactory>();
builder.RegisterType<Implementation>().Named<IMyInterface>("implementation");

builder.Register((c, args) => new Decorator(c.ResolveNamed<IMyInterface>("implementation", args.First())))
    .As<IMyInterface>();

var container = builder.Build();

var factory = container.Resolve<ImplementationFactory>();
var impl = factory.GetImplementation("abc"); //returns the decorator
Run Code Online (Sandbox Code Playgroud)

如果您有多个装饰器,您可以按照注册中的顺序链接它们:

builder.Register((c, args) => new Decorator(c.ResolveNamed<IMyInterface>("implementation", args.First())));
builder.Register((c, args) => new SecondDecorator(c.Resolve<Decorator>(args.First()))).As<IMyInterface>();
Run Code Online (Sandbox Code Playgroud)

这是一篇关于autofac中装饰器支持的好文章.