Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is the best approach? AutoMapper against implicit (C# Reference)

Tags:

c#

automapper

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 = "[email protected]"
}

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;
    }
}
like image 668
Zinov Avatar asked Mar 25 '15 13:03

Zinov


People also ask

Is AutoMapper faster than manual mapping?

Inside this article, it discusses performance and it indicates that Automapper is 7 times slower than manual mapping.

What is AutoMapper good for?

AutoMapper is a simple library that helps us to transform one object type into another. It is a convention-based object-to-object mapper that requires very little configuration. The object-to-object mapping works by transforming an input object of one type into an output object of a different type.

When should I use AutoMapper?

AutoMapper is used whenever there are many data properties for objects, and we need to map them between the object of source class to the object of destination class, Along with the knowledge of data structure and algorithms, a developer is required to have excellent development skills as well.

What is implicit operator C#?

The Implicit Operator According to MSDN, an implicit keyword is used to declare an implicit user-defined type conversion operator. In other words, this gives the power to your C# class, which can accepts any reasonably convertible data type without type casting.


Video Answer


1 Answers

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.

like image 78
Jeroen Vannevel Avatar answered Sep 27 '22 18:09

Jeroen Vannevel