哪种方法最好? AutoMapper反对隐式(C#参考) [英] Which is the best approach? AutoMapper against implicit (C# Reference)

查看:102
本文介绍了哪种方法最好? AutoMapper反对隐式(C#参考)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Automapper是一种匹配类型的方法,理想情况下,当您要映射模型及其视图模型时. 但是,这与我们在C#中使用隐式可以采用的方法不同吗? (假设两个模型都具有相同的属性,但名称不同,在这种情况下,您需要在AutoMapper中指定在模型之间链接的属性)

有了autommaper,我们就有了

public class Employee
{
    public string Name { get; set; }
    public string Email { get; set; }
}

public class EmployeeViewItem
{
    public string Name { get; set; }
    public string Email { get; set; }
}

通常,我们这样做:

Employee employee = new Employee
{
    Name = "John SMith",
    Email = "john@codearsenal.net"
}

EmployeeViewItem viewItem = new EmployeeViewItem();
viewItem.Name = employee.Name;
viewItem.Email = employee.Email;

使用AutoMapper

 EmployeeViewItem employeeVIewItem = Mapper.Map<Employee, EmployeeViewItem>(employee); 

现在,使用隐式C#参考

public class Employee
{
    public static implicit operator EmployeeViewItem(Employee employee)
    {
         EmployeeViewItem viewItem = new EmployeeViewItem();
         viewItem.Name = employee.Name;
         viewItem.Email = employee.Email;
         return viewItem;
    }

    public static implicit operator Employee(EmployeeViewItem ev)
    {
        var e = new Employee();
        e.Name = ev.Name;
        e.Email = ev.Email;
        return e;
    }
}

解决方案

AutoMapper使用反射来映射属性(性能开销不大),允许使用高级自定义规则进行映射,并且在基本(常见?)方案中需要1行代码./p>

隐式运算符要求您指定每个属性,容易出错(添加新属性但不将其添加到运算符),更难于设置多种类型,创建大量无用的代码,甚至是最基本的代码设置您仍然有N行代码,其中N是属性的数量.

我认为这是不言而喻的.

Automapper is a way to match types, ideally when you want to map a model and its viewmodel. But is this not the same approach that we can make with implicit in C#? (Suppose that both model have the same properties but with different names, on this case, you need to specify in AutoMapper which is linked between models)

With autommaper we have

public class Employee
{
    public string Name { get; set; }
    public string Email { get; set; }
}

public class EmployeeViewItem
{
    public string Name { get; set; }
    public string Email { get; set; }
}

Usually we do:

Employee employee = new Employee
{
    Name = "John SMith",
    Email = "john@codearsenal.net"
}

EmployeeViewItem viewItem = new EmployeeViewItem();
viewItem.Name = employee.Name;
viewItem.Email = employee.Email;

with AutoMapper

 EmployeeViewItem employeeVIewItem = Mapper.Map<Employee, EmployeeViewItem>(employee); 

Now, with the implicit C# Reference

public class Employee
{
    public static implicit operator EmployeeViewItem(Employee employee)
    {
         EmployeeViewItem viewItem = new EmployeeViewItem();
         viewItem.Name = employee.Name;
         viewItem.Email = employee.Email;
         return viewItem;
    }

    public static implicit operator Employee(EmployeeViewItem ev)
    {
        var e = new Employee();
        e.Name = ev.Name;
        e.Email = ev.Email;
        return e;
    }
}

解决方案

AutoMapper uses reflection to map properties (slight performance overhead), allows advanced custom rules for mapping and requires 1 line of code in basic (common?) scenarios.

Implicit operators require you to specify each property, are prone to errors (adding a new property but not adding it to the operator), are more difficult to setup for multiple types, create lots of useless code and even in the most basic setup you still have N lines of code where N is the amount of properties.

I think it speaks for itself.

这篇关于哪种方法最好? AutoMapper反对隐式(C#参考)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆