猿问

在Simple注入器中注册Decorator,通用接口

我对使用装饰器简单注入器的 IoC 有问题。


如何在 Ioc Simple 注入器中注册它?


var data = new Data();

var test = new Data2Decorator (data,

    new Data1Decorator(data, new XxxData(data)))

我这样尝试过。


_container.Register<Ixxx1>();

_container.Register<Ixxx2>();

_container.Register<Ixxx, Data>();

_container.Register<IData, XxxData>();

container.RegisterDecorator<IData, Data1Decorator>();

_container.RegisterDecorator<IData, Data2Decorator>():          

public interface Ixxx1

{

    public IEnumerable<...> x1 { get; set; }

}

public class Data1Decorator : DataDecorator

{

    public readonly Ixxx1 _xxx1; 


    public Data1Decorator(Ixxx1 xxx1, IData data) : base(data)

    {

        _xxx1 = xxx1;

    }


    public override Task Load()

    {

        return Task.WhenAll(new List<Task>() { ..., base.Load() })

        .ContinueWith((b) =>

        { 

               _xxx1.x1 = ...

        });

    }

}

常用存储数据


public interface Ixxx :

    Ixxx1,

    Ixxx2

{


}


public class Data: Ixxx

    public IEnumerable<...> x1 { get; set; }


    public IEnumerable<...> x2 { get; set; }

}


HUWWW
浏览 120回答 1
1回答

qq_遁去的一_1

container.Register<Ixxx, Data>();_container.Register<IData<Ixxx>, XxxData>();_container.RegisterDecorator<IData<Ixxx>, Data1Decorator<Ixxx>>();_container.RegisterDecorator<IData<Ixxx>, Data2Decorator<Ixxx>>():&nbsp; &nbsp; public interface IData<out TData>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; TData Data { get; }&nbsp; &nbsp; &nbsp; &nbsp; Task Load();&nbsp; &nbsp; }&nbsp; &nbsp; public abstract class DataDecorator<TData> : IData<TData>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public readonly IData<TData> _data;&nbsp; &nbsp; &nbsp; &nbsp; public TData Data => _data.Data;&nbsp; &nbsp; &nbsp; &nbsp; protected DataDecorator(IData<TData> data) => _data = data;&nbsp; &nbsp; &nbsp; &nbsp; public virtual Task Load() => _data.Load();&nbsp; &nbsp; }&nbsp; &nbsp; public interface Ixxx :&nbsp; &nbsp; &nbsp; &nbsp; Ixxx1,&nbsp; &nbsp; &nbsp; &nbsp; Ixxx2&nbsp; &nbsp; {&nbsp; &nbsp; }&nbsp; &nbsp; public class XxxData : IData<Ixxx>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public Ixxx Data { get; }&nbsp; &nbsp; &nbsp; &nbsp; public XxxData(Ixxx data) => Data = data;&nbsp; &nbsp; &nbsp; &nbsp; public Task Load() => Task.Run(() => { });&nbsp; &nbsp; }&nbsp; &nbsp; public interface Ixxx1&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public IEnumerable<...> x1 { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; public class Data1Decorator<TData> : DataDecorator<TData>&nbsp; &nbsp; &nbsp; &nbsp; where TData : Ixxx1&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public Data1Decorator(IData data) : base(data)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public override Task Load()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Task.WhenAll(new List<Task>() { ..., base.Load() })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ContinueWith((b) =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Data.x1 = ...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答