在简单注入器中根据上下文注入相同的实例

本文关键字:注入 实例 上下文 简单 注入器 | 更新日期: 2023-09-27 18:02:04

我正在尝试将相同的实例注入到一些对象中,另一个实例注入到另一个对象列表中,基于某些条件。

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;
    }
}

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

我该怎么做?谢谢。

根据Steven的注释更新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);
    }
}

在简单注入器中根据上下文注入相同的实例

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