要用于类型映射的正确数据类型

本文关键字:数据类型 映射 用于 类型 | 更新日期: 2023-09-27 18:08:41

我想实现一个类似于AutoMapper的基本映射系统,但都是显式映射,没有基于约定的映射。

为了做到这一点,我写了一个类,它应该维护一个"映射"函数的寄存器,并根据需要查找它们,以便将一种类型映射到另一种类型。我设想的用法是这样的:

启动时:

Mapper.Register<TypeA, TypeB>(typeA =>
{
    var typeB = new TypeB()
    {
        Property1 = typeA.Property1
    };
    return typeB;
}

然后当我想执行映射时…

TypeA typeA = new TypeA();
TypeB typeB = Mapper.Map<TypeA, TypeB>(typeA);

目前我已经使用Dictionary<Tuple<Type, Type>, Delegate>来存储这个映射寄存器,但它没有像我希望的那样工作…

public static class Mapper
{
    private readonly static Dictionary<Tuple<Type, Type>, Delegate> Mappings = new Dictionary<Tuple<Type, Type>, Delegate>();
    public static void Register<TSource, TDestination>(Func<TSource, TDestination> mappingFunction)
        where TSource : class
        where TDestination : class
    {
        Delegate mappingFunc;
        if (Mappings.TryGetValue(new Tuple<Type, Type>(typeof (TSource), typeof (TDestination)), out mappingFunc))
            Mappings[new Tuple<Type, Type>(typeof (TSource), typeof (TDestination))] = mappingFunction;
        else
            Mappings.Add(new Tuple<Type, Type>(typeof (TSource), typeof (TDestination)), mappingFunction);
    }
    public static TDestination Map<TSource, TDestination>(TSource src)
        where TSource : class
        where TDestination : class
    {
        Delegate mappingFunc;
        if (!Mappings.TryGetValue(new Tuple<Type, Type>(typeof (TSource), typeof (TDestination)), out mappingFunc))
            throw new Exception("Invalid mapping: no mapping found for requested types.");
        var func = mappingFunc as Func<TSource, TDestination>;
        if (func == null)
            throw new Exception("Invalid mapping: no mapping found for requested types.");
        return func.Invoke(src);
    }
}

当使用此代码时,注册映射工作正常,但从字典中检索它们失败,我认为这是因为Map方法中的第三行:

Mappings.TryGetValue(new Tuple<Type, Type>(typeof (TSource), typeof (TDestination)), out mappingFunc)

这一行总是不能通过测试,我认为如果我理解正确的话,这是因为Tuple是一个引用类型,所以一个新的Tuple<Type, Type>实例,不管Item1Item2是什么,将永远不会匹配Dictionary中的任何键。

所以,Dictionary<Tuple<Type, Type>, Delegate>不适合存储这个映射寄存器。在这种情况下,数据类型是什么?

要用于类型映射的正确数据类型

当我尝试运行上面的代码时,我得到了我期望的结果:

Mapper.Register<string, Regex>(s => new Regex("not using the given string"));
Mapper.Register<string, Regex>(s => new Regex(s));
var regex = Mapper.Map<string, Regex>(@"'w*");
// regex is now the Regex object instantiated with @"'w*"

换句话说,你的代码似乎运行正常。

这一行总是不能通过测试,我想如果我理解正确的话,这是因为Tuple是一个引用类型,所以Tuple<Type, Type>的一个新实例,无论Item1Item2是什么,都不会匹配Dictionary中的任何键。

实际上,Tuple的实现确实支持你正在尝试做的事情。Dictionary在底层使用GetHashCodeEquals方法进行查找。Equals方法通常检查对象的引用相等性,但是Tuple源代码显示它是专门为使用结构相等性而编程的:

    public override Boolean Equals(Object obj) {
        return ((IStructuralEquatable) this).Equals(obj, EqualityComparer<Object>.Default);;
    }
    Boolean IStructuralEquatable.Equals(Object other, IEqualityComparer comparer) {
        if (other == null) return false;
        Tuple<T1, T2> objTuple = other as Tuple<T1, T2>;
        if (objTuple == null) {
            return false;
        }
        return comparer.Equals(m_Item1, objTuple.m_Item1) && comparer.Equals(m_Item2, objTuple.m_Item2);
    }

Dictionary是这个用例的正确方法,因为可以进行任意数量的注册,并且我们希望确保快速查找。但是,我认为我可能仍然会创建自己的类型TypeMapping来用于Dictionary而不是Tuple<Type, Type>,因为我认为元组不能表达意图/用法。只要记住重写GetHashCodeEquals,这样它就可以正确地与Dictionary一起工作,如下所示:

public class TypeMapping : IStructuralEquatable
{
    public Type From { get; private set; }
    public Type To { get; private set; }
    public TypeMapping (Type from, Type to)
    {
        From = from;
        To = to;
    }
    public override int GetHashCode()
    {
        return ((IStructuralEquatable) this).GetHashCode(EqualityComparer<Object>.Default);
    }
    int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
    {
        var hash = 17;
        unchecked
        {
            hash = hash * 31 + From.GetHashCode();
            hash = hash * 31 + To.GetHashCode();
        }
        return hash;
    }
    public override bool Equals(Object obj) {
        return ((IStructuralEquatable) this).Equals(obj, EqualityComparer<Object>.Default);
    }
    bool IStructuralEquatable.Equals(Object other, IEqualityComparer comparer) {
        if (other == null) return false;
        var otherMapping = other as TypeMapping;
        if (otherMapping == null) return false;
        return comparer.Equals(From, otherMapping.From) && comparer.Equals(To, otherMapping.To);
    }
}

然后你会有你的Dictionary在映射类看起来像这样(与RegistrationMap方法相应的变化):

private readonly static Dictionary<TypeMapping, Delegate> Mappings = new Dictionary<TypeMapping, Delegate>();