简体   繁体   English

AutoMapper映射与通用扩展方法

[英]AutoMapper mapping with generic extension methods

I want to map my objects 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);
    }    
}

And this is viewmodel 这是viewmodel

public class CustomerListViewModel    
{    
    public string FullName { get; set; }
    public string Email { get; set; }    
    public string HomeAddressCountry { get; set; }    
}

So I am creating map, Mapper.CreateMap<Customer, CustomerListViewModel>(); 所以我创建了map, Mapper.CreateMap<Customer, CustomerListViewModel>();

And I want to create an extension method 我想创建一个扩展方法

public static class MapperHelper
{
    public static CustomerListViewModel ToViewModel(this Customer cust)
    {
        return AutoMapper.Mapper.Map<Customer, CustomerListViewModel>(cust);
    }
}

But I want to make generic this helper: 但我想制作通用的帮手:

public static class MapperHelper<TSource, TDest>
{
    public static TDest ToViewModel(this TSource cust)
    {
        return AutoMapper.Mapper.Map<TSource, TDest>(cust);
    }
}

Gives error: Extension method can only be declared in non-generic, non-nested static class 给出错误: 扩展方法只能在非泛型的非嵌套静态类中声明

If I can not make generic, I should create helper class for all mapping. 如果我不能创建泛型,我应该为所有映射创建帮助类。 Is there any way to solution? 有什么方法可以解决吗?

Even better than these solutions is to use the non-generic Map method: 甚至比这些解决方案更好的是使用非通用Map方法:

public static class MapperHelper
{
    public static TDest MapTo<TDest>(this object src)
    {
        return (TDest)AutoMapper.Mapper.Map(src, src.GetType(), typeof(TDest));
    }
}

In your code: 在你的代码中:

var model = customter.MapTo<CustomerViewModel>();

Now you don't need the superfluous Source type in your generic method. 现在,您不需要泛型方法中的多余Source类型。

Can't you just do this?: 你不能这样做吗?:

public static class MapperHelper
{
    public static TDest ToViewModel<TSource, TDest>(this TSource cust)
    {
       return AutoMapper.Mapper.Map<TSource, TDest>(cust);
    }
}

You can't define extension methods in a generic class because there would be no way for you to specify the type parameters when you invoke it! 您无法在泛型类中定义扩展方法,因为在调用它时无法指定类型参数!

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

You have to make the method generic instead:- 你必须使方法变得通用: -

public static class MapperHelper
{
  public static TTarget ToViewModel<TSource, TTarget>(this TSource source)
  {
    ...
  }
}

...

var viewModel = model.ToViewModel<MyModel, MyViewModel>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM