我想实现一个类似于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
不适合存储此映射寄存器。在这种情况下,什么数据类型是什么?
答案 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
使用GetHashCode
和Equals
方法进行查找。 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>
,因为我认为元组不表达意图/用法。请记住覆盖GetHashCode
和Equals
,以便它与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
看起来像这样(对Registration
和Map
方法进行相应的更改):
private readonly static Dictionary<TypeMapping, Delegate> Mappings = new Dictionary<TypeMapping, Delegate>();