演示者的依赖注入

时间:2009-12-18 05:19:24

标签: c# dependency-injection inversion-of-control autofac mvp

我有一个Presenter,它在其构造函数中将Service和View Contract作为参数:

public FooPresenter : IFooPresenter {
    private IFooView view;
    private readonly IFooService service;

    public FooPresenter(IFooView view, IFooService service) {
        this.view = view;
        this.service = service;
    }
}

我使用Autofac解决了我的服务:

private ContainerProvider BuildDependencies() {
    var builder = new ContainerBuilder();
    builder.Register<FooService>().As<IFooService>().FactoryScoped();  

    return new ContainerProvider(builder.Build());  
}

在我的ASPX页面中(查看实现):

public partial class Foo : Page, IFooView {
    private FooPresenter presenter;

    public Foo() {
        // this is straightforward but not really ideal
        // (IoCResolve is a holder for how I hit the container in global.asax)
        this.presenter = new FooPresenter(this, IoCResolve<IFooService>());

        // I would rather have an interface IFooPresenter so I can do
        this.presenter = IoCResolve<IFooPresenter>();
        // this allows me to add more services as needed without having to 
        // come back and manually update this constructor call here
    }
}

问题是FooPresenter的构造函数需要特定的Page,而不是容器创建一个新的。

我是否可以为此解决方案提供视图的特定实例(当前页面)到容器?这样做是否有意义,或者我应该采取另一种方式吗?

2 个答案:

答案 0 :(得分:2)

在解决Autofac中的依赖关系时,解决传递我想要调用数据参数的方法是使用生成的工厂

(更新:this question讨论了同样的问题,my article显示了如何避免大量的工厂代表。

您的问题的解决方案将如下所示:

首先,声明一个只有 接受数据参数的工厂代理:

public delegate IFooPresenter FooPresenterFactory(IFooView view);

您的演示者不变:

public FooPresenter : IFooPresenter {
    private IFooView view;
    private readonly IFooService service;

    public FooPresenter(IFooView view, IFooService service) {
        this.view = view;
        this.service = service;
    }
}

接下来是Autofac容器设置:

var builder = new ContainerBuilder();
builder.Register<FooService>().As<IFooService>().FactoryScoped();  
builder.Register<FooPresenter>().As<IFooPresenter>().FactoryScoped();  
builder.RegisterGeneratedFactory<FooPresenterFactory>();

现在,在您的页面中,您可以以两行代码解决演示者,首先获取工厂,然后致电工厂为您解决问题:

public partial class Foo : Page, IFooView {
    private FooPresenter presenter;

    public Foo() {
        var factory = IoCResolve<FooPresenterFactory>();
        this.presenter = factory(this);
    }
}

答案 1 :(得分:0)

我实际上解决了这个确切的问题并围绕它构建了一个框架。我使用Autofac parameters将现有视图传递给演示者解析调用。

首先,我定义了一个源自Autofac的自定义分辨率界面:

public interface IMvpContext : IContext
{
    T View<T>();
}

允许我注册解析视图的演示者:

builder.RegisterPresenter(c => new FooPresenter(
    c.View<IFooView>(),
    c.Resolve<IFooService>()));

使用在IContext的实现中包含Autofac的IMvpContext的扩展方法:

public static IConcreteRegistrar RegisterPresenter<T>(
    this ContainerBuilder builder,
    Func<IMvpContext, T> creator)
{
    return builder
        .Register((context, parameters) => creator(new MvpContext(context, parameters)))
        .FactoryScoped();
}

我定义了一个表示视图参数的参数类型:

public class MvpViewParameter : NamedParameter
{
    public static readonly string ParameterName = typeof(MvpViewParameter).AssemblyQualifiedName;

    public MvpViewParameter(object view) : base(ParameterName, view)
    {}
}

它使用自己的程序集限定类型名称作为参数名称。这与合法参数冲突的可能性非常低。

MvpContext将所有标准分辨率调用传递给基本上下文。对于视图,它使用众所周知的名称解析参数:

public sealed class MvpContext : IMvpContext
{
    private IContext _context;
    private IEnumerable<Parameter> _resolutionParameters;

    public MvpContext(IContext context, IEnumerable<Parameter> resolutionParameters)
    {
        _context = context;
        _resolutionParameters = resolutionParameters;
    }

    #region IContext

    // Pass through all calls to _context

    #endregion

    #region IMvpContext

    public T View<T>()
    {
        return _resolutionParameters.Named<T>(MvpViewParameter.ParameterName);
    }
    #endregion
}

解析演示者的调用提供了view参数:

public partial class Foo : Page, IFooView
{
    private readonly FooPresenter presenter;

    public Foo()
    {
        this.presenter = IoCResolve<IFooPresenter>(new MvpViewParameter(this));
    }
}