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

时间:2016-09-26 04:07:21

标签: c#

我想实现一个类似于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>的新实例,无论是什么{ {1}}和Item1永远不会匹配Item2中的任何键。

因此,Dictionary不适合存储此映射寄存器。在这种情况下,什么数据类型是什么?

1 个答案:

答案 0 :(得分:3)

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

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>的新实例,无论Item1是什么}和Item2是,永远不会匹配Dictionary中的任何键。

实际上,Tuple的实施确实支持您要做的事情。 Dictionary使用GetHashCodeEquals方法进行查找。 Equals方法通常会检查对象的引用相等性,但Tuple source code表明它是专门编程为使用结构相等的:

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

然后,您可以让Mapping类中的Dictionary看起来像这样(对RegistrationMap方法进行相应的更改):

private readonly static Dictionary<TypeMapping, Delegate> Mappings = new Dictionary<TypeMapping, Delegate>();
相关问题