具有通用扩展方法的 AutoMapper 映射
AutoMapper mapping with generic extension methods
我想用通用扩展方法映射我的对象。
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public Address HomeAddress { get; set; }
public string GetFullName()
{
return string.Format(“{0} {1}”, FirstName, LastName);
}
}
这是视图模型
public class CustomerListViewModel
{
public string FullName { get; set; }
public string Email { get; set; }
public string HomeAddressCountry { get; set; }
}
所以我正在创建地图,Mapper.CreateMap<Customer, CustomerListViewModel>();
我想创建一个扩展方法
public static class MapperHelper
{
public static CustomerListViewModel ToViewModel(this Customer cust)
{
return AutoMapper.Mapper.Map<Customer, CustomerListViewModel>(cust);
}
}
但我想让这个助手通用化:
public static class MapperHelper<TSource, TDest>
{
public static TDest ToViewModel(this TSource cust)
{
return AutoMapper.Mapper.Map<TSource, TDest>(cust);
}
}
给出错误:扩展方法只能在非泛型、非嵌套静态中声明class
如果我不能制作通用的,我应该为所有映射创建助手 class。有什么办法解决吗?
你就不能这样做吗?:
public static class MapperHelper
{
public static TDest ToViewModel<TSource, TDest>(this TSource cust)
{
return AutoMapper.Mapper.Map<TSource, TDest>(cust);
}
}
您不能在泛型中定义扩展方法 class 因为您无法在调用它时指定类型参数!
public static class MapperHelper<TSource, TTarget>
{
public static TTarget ToViewModel(this TSource source)
{
...
}
}
...
// this is no problem if we invoke our method like a
// static method:-
var viewModel = MapperHelper<MyModel, MyViewModel>.ToViewModel(model);
// but if we use the extension method syntax then how
// does the compiler know what TSource and TTarget are?:-
var viewModel = model.ToViewModel();
您必须改为 方法 通用:-
public static class MapperHelper
{
public static TTarget ToViewModel<TSource, TTarget>(this TSource source)
{
...
}
}
...
var viewModel = model.ToViewModel<MyModel, MyViewModel>();
比这些解决方案更好的是使用非通用 Map 方法:
public static class MapperHelper
{
public static TDest MapTo<TDest>(this object src)
{
return (TDest)AutoMapper.Mapper.Map(src, src.GetType(), typeof(TDest));
}
}
在您的代码中:
var model = customter.MapTo<CustomerViewModel>();
现在您的泛型方法中不需要多余的 Source 类型。
我想用通用扩展方法映射我的对象。
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public Address HomeAddress { get; set; }
public string GetFullName()
{
return string.Format(“{0} {1}”, FirstName, LastName);
}
}
这是视图模型
public class CustomerListViewModel
{
public string FullName { get; set; }
public string Email { get; set; }
public string HomeAddressCountry { get; set; }
}
所以我正在创建地图,Mapper.CreateMap<Customer, CustomerListViewModel>();
我想创建一个扩展方法
public static class MapperHelper
{
public static CustomerListViewModel ToViewModel(this Customer cust)
{
return AutoMapper.Mapper.Map<Customer, CustomerListViewModel>(cust);
}
}
但我想让这个助手通用化:
public static class MapperHelper<TSource, TDest>
{
public static TDest ToViewModel(this TSource cust)
{
return AutoMapper.Mapper.Map<TSource, TDest>(cust);
}
}
给出错误:扩展方法只能在非泛型、非嵌套静态中声明class
如果我不能制作通用的,我应该为所有映射创建助手 class。有什么办法解决吗?
你就不能这样做吗?:
public static class MapperHelper
{
public static TDest ToViewModel<TSource, TDest>(this TSource cust)
{
return AutoMapper.Mapper.Map<TSource, TDest>(cust);
}
}
您不能在泛型中定义扩展方法 class 因为您无法在调用它时指定类型参数!
public static class MapperHelper<TSource, TTarget>
{
public static TTarget ToViewModel(this TSource source)
{
...
}
}
...
// this is no problem if we invoke our method like a
// static method:-
var viewModel = MapperHelper<MyModel, MyViewModel>.ToViewModel(model);
// but if we use the extension method syntax then how
// does the compiler know what TSource and TTarget are?:-
var viewModel = model.ToViewModel();
您必须改为 方法 通用:-
public static class MapperHelper
{
public static TTarget ToViewModel<TSource, TTarget>(this TSource source)
{
...
}
}
...
var viewModel = model.ToViewModel<MyModel, MyViewModel>();
比这些解决方案更好的是使用非通用 Map 方法:
public static class MapperHelper
{
public static TDest MapTo<TDest>(this object src)
{
return (TDest)AutoMapper.Mapper.Map(src, src.GetType(), typeof(TDest));
}
}
在您的代码中:
var model = customter.MapTo<CustomerViewModel>();
现在您的泛型方法中不需要多余的 Source 类型。