简体   繁体   中英

C# generics basic knowledge?

I have following two classes

public class CommandFetcher : ICommandFetcher
{
  private readonly Dictionary<TypeAndVersion, Type> _configuration;

  public CommandFetcher(Dictionary<TypeAndVersion, Type> configuration)
  {
      _configuration = configuration;
  }

    public ICommand FetchFrom(MessageEnvelope messageEnvelope)
    {
        TypeAndVersion typeAndVersion = new TypeAndVersion(messageEnvelope.MetaData.MessageType,
            messageEnvelope.MetaData.MessageTypeVersion);
        return (ICommand)JsonConvert.DeserializeObject(messageEnvelope.MessageData, _configuration[typeAndVersion]);
    }
}

and

public class MessageDeserializer : IMessageDeserializer
{
    private readonly IDictionary<TypeAndVersion, Type> _configuration;

    public MessageDeserializer(IDictionary<TypeAndVersion, Type> configuration)
    {
        _configuration = configuration;
    }

    public IMessage Deserialize(string messageData, IMetaData metaData)
    {
        var typeAndVersion = new TypeAndVersion(metaData.MessageType, metaData.MessageTypeVersion);
        if (!_configuration.ContainsKey(typeAndVersion))
            throw new InvalidOperationException("Invalid version");
        return (IMessage) JsonConvert.DeserializeObject(messageData, _configuration[typeAndVersion]);
    }
}

i rewrite the configuration part in both class but now i want to get that configuration from MessageDeserializer class into CommandFetcher class how can i do it ? Thank you !

No idea if I am reading your question correctly...but it sounds like you are wanting to instantiate a CommandFetcher from within the MessageDesrializer?? If this is correct you could try making the Dictionary on the CommandFetcher an IDictionary instead of a Dictionary

There are multiple problems in your question:

  1. You are not saying anything about the dependencies between the classes. Does one use the other?
  2. You are not sharing what configuration actually is. Does each class need their own configuration?

So here is my guess: Your two classes actually use the same _configuration . In that case, this has nothing to do with generics. All you need to do is pass the same Dictionary to both object constructors.

If you are attempting to make a single generic method that handles both those cases it would look something like this:

public class GenericDeserializer<T> : IGenericDeserializer
{
    private readonly IDictionary<TypeAndVersion, Type> _configuration;

    public GenericDeserializer(IDictionary<TypeAndVersion, Type> configuration)
    {
        _configuration = configuration;
    }

    public T Deserialize(string messageData, IMetaData metaData)
    {
        var typeAndVersion = new TypeAndVersion(metaData.MessageType, metaData.MessageTypeVersion);
        if (!_configuration.ContainsKey(typeAndVersion))
            throw new InvalidOperationException("Invalid version");
        return (T)JsonConvert.DeserializeObject(messageData, _configuration[typeAndVersion]);
    }
}

That said, you would need to normalize ICommand and IMessage as well as create a generic interface for ICommandFetcher and IMessageDeserializer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM