首页 > 解决方案 > 从基于另一种类型的集合中检索泛型类

问题描述

我在实现将一些 CRUD 逻辑和 dto-entity 映射封装在单独的服务中的解决方案时遇到了麻烦,并且真的可以使用一些帮助。

因此,假设我们有下一个接口,它提供对数据存储的访问端点,并且每个实体都有一个单独的客户端

public interface IClient<T> where T : class
{
    T Get();
    void Set(T entity);
}

然后我们有 ClientService 作为聚合器,为所有现有客户端提供单个访问点

public class A { };
public class B { };

public class ClientService
{
    private IDictionary<Type, object> _clientDict;

    public ClientService(IClient<A> clientA, IClient<B> clientB)
    {
        _clientDict = new Dictionary<Type, object>
        {
            { typeof(A), clientA },
            { typeof(B), clientB }
        };
    }

    public T Get<T>() where T : class
    {
        var client = this.GetClient<T>();
        return client.Get();
    }

    public void Set<T>(T entity) where T : class
    {
        var client = this.GetClient<T>();
        client.Set(entity);
    }

    private IClient<T> GetClient<T>() where T : class
    {
        if (_clientDict.TryGetValue(typeof(T), out object client))
        {
            return (IClient<T>)client;
        }

        throw new ArgumentException();
    }
}

到目前为止一切都很好。

现在的问题。我需要将它转换为使用 DTO 对象来使用,IClient<T>同时仍然接受非 DTO 模型到ClientService. 伪代码:

public class A { };
public class A_DTO { };
public class B { };
public class B_DTO { };

public class ClientService
{
    private IDictionary<Type, object> _clientDict;

    public ClientService(IClient<A_DTO> clientA, IClient<B_DTO> clientB)
    {
        _clientDict = new Dictionary<Type, object>
        {
            { typeof(A), clientA },
            { typeof(B), clientB }
        };
    }

    public void Set<T>(T entity) where T : class
    {
        var dtoType = GetDTOType(T);
        var dtoEntity = _autoMapper.Map(entity, typeof(T), GetDTOType(T));

        var client = this.GetClient<T, dtoType>();
        client.Set(dtoEntity);
    }

    private IClient<DTO> GetClient<T, DTO>() where T : class
                                            where DTO: class
    {
        if (_clientDict.TryGetValue(typeof(T), out object client))
        {
            return (IClient<DTO>)client;
        }

        throw new ArgumentException();
    }
}

我确实理解,由于泛型在编译期间被解析,我们无法使它们依赖于变量,但是可能有任何其他方式可以让客户使用,即有没有任何方式我们如何实现IClient<T2> GetClient<T1, T2>where T1isAT2is A_DTO?请注意,最终用户无权访问 DTO 对象,因此无法将其作为通用类型提供,一切都需要在内部进行。

是否有任何通用实现,或者我只是在浪费时间,应该为每个客户端实现(即等)创建特定的Get<A>()方法Get<B>

标签: c#.netgenerics.net-corepolymorphism

解决方案


您实际上不需要在您的客户服务类中包含您的 DTO。一旦你的模型开始增长,那肯定会变得一团糟。您可以做的是使用AutoMapper之类的工具将结果从您的服务类转换为您的 dto。这很简单。你可以有这样的东西:

public class A { };

public class DtoA { };

var source = ServiceClass.Get<A>();
var dto = Mapper.Map<A, ADto>(source);

请参阅链接查看更多细节。


推荐阅读