在Simple Injector

时间:2016-08-11 16:06:03

标签: c# .net dependency-injection ioc-container simple-injector

我尝试根据某些条件将相同的实例注入某些对象,将另一个实例注入另一个对象列表。

interface IChannelContext
{
    TransformManyBlock<byte[], Message> Decoder { get; }
    IFilter Filter { get; }
}

interface IMsgSeqNumFilter
{
    bool CheckMsgSeqNumGreater(byte[] buffer);
}

class ChannelContext : IChannelContext
{
    public TransformManyBlock<byte[], Message> Decoder { get; }
    public IMsgSeqNumFilter MsgSeqNumFilter { get; }

    public ChannelContext(TransformManyBlock<byte[], Message> decoder, IMsgSeqNumFilter msgSeqNumFilter )
    {
        Decoder = decoder;
        MsgSeqNumFilter = msgSeqNumFilter;
    }
}

class MsgSeqNumFilter : IMsgSeqNumFilter
{
    int maxMsgSeqNum = -1;

    public bool CheckMsgSeqNumGreater(byte[] buffer)
    {
        return true;
    }
}

所以我必须为相同的解码器实例注入相同的过滤器实例。对于不同的解码器实例,应该注入不同的实例。

我该怎么做?感谢。

根据史蒂文的评论,

更新1 对象图。不使用容器和接口。

class MsgSeqNumFilter
{
    int maxMsgSeqNum = -1;

    public bool CheckMsgSeqNumGreater(byte[] buffer)
    {
        return true;
    }
}

class NetworkAddress
{
    public string IP { get; }
    public int Port { get; }

    public NetworkAddress(string ip, int port)
    {
        IP = ip;
        Port = port;
    }
}

class ChannelContext
{
    public NetworkAddress Address { get; }
    public TransformManyBlock<byte[], Message> Decoder { get; }
    public MsgSeqNumFilter MsgSeqNumFilter { get; }

    public ChannelContext(NetworkAddress address, TransformManyBlock<byte[], Message> decoder, MsgSeqNumFilter msgSeqNumFilter )
    {
        Address = address;
        Decoder = decoder;
        MsgSeqNumFilter = msgSeqNumFilter;
    }
}

class NetworkManager
{
    CancellationToken parentToken;

    public MulticastReceiveManager(IEnumerable<ChannelContext> channels, CancellationToken parentToken)
    {
        this.parentToken = parentToken;
        foreach (var channelContext in channels) AddChannel(channelContext);
    }

    public void AddChannel(ChannelContext channelContext)
    {
    }
}

class PacketDecoder
{
    public TransformManyBlock<byte[], Message> DecoderA { get; } = new TransformManyBlock<byte[], Message>();
    public TransformManyBlock<byte[], Message> DecoderB { get; } = new TransformManyBlock<byte[], Message>();
}

class Program
{
    static void Main(string[] args)
    {
        var parentTokenSource = new CancellationTokenSource();

        NetworkAddress addrA1 = new NetworkAddress("1.1.1.1", 101);
        NetworkAddress addrA2 = new NetworkAddress("1.1.1.2", 102);
        MsgSeqNumFilter filterA = new MsgSeqNumFilter();
        NetworkAddress addrB1 = new NetworkAddress("2.2.2.1", 201);
        NetworkAddress addrB2 = new NetworkAddress("2.2.2.2", 202);
        MsgSeqNumFilter filterB = new MsgSeqNumFilter();

        var packetDecoder = new PacketDecoder();
        var channels = new List<ChannelContext>();
        channels.Add(new ChannelContext(addrA1, packetDecoder.DecoderA, filterA);
        channels.Add(new ChannelContext(addrA2, packetDecoder.DecoderA, filterA);
        channels.Add(new ChannelContext(addrB1, packetDecoder.DecoderB, filterB);
        channels.Add(new ChannelContext(addrB2, packetDecoder.DecoderB, filterB);

        var NetworkManager = new NetworkManager (channels, parentTokenSource.Token);
    }
}

1 个答案:

答案 0 :(得分:-2)

创建static个解码器数组并将它们传递给ChannelContext个实例。考虑使用依赖注入