So the basic idea was:
We have some business model
public class Model
{
public int Foo { get; set; }
}
And it's view model representation
public class ViewModel
{
public string Foo { get; set; }
public static explicit operator ViewModel(Model b)
{
// Map instances using AutoMapper or whatever
return new ViewModel { Foo = b.Foo.ToString() };
}
}
Our basic instinct is to map model to view model. As you can see I want to perform mapping using explicit operator
so I could do
var model = new Model { Foo = 42 }; // Get model
var viewModel = (ViewModel)model; // Map to view model
and therefore have my controller code as clean as possible... BUT I want to make view model and mapping logic stay separated. How can I move explicit operator
implementation to some external class? Similar to extension methods:
public static class Extensions
{
public static explicit operator ViewModel(Model b)
{
// Map instances using Automapper or whatever
return new ViewModel { Foo = b.Foo.ToString() };
}
}
This code, obviously, isn't compiling because of two reasons:
- Static classes can not contain user-defined operators
- Either parameter or return type must be Extensions
Also, as an option, I could make view model partial and split model itself and operator to separate .cs files but it would not make the case. Architecturally they would still be the same class in the same namespace. I want to be able to implement mapping logic, for example, in another project in solution.
I just want to achieve similar to extension methods effect. How can I do that?
The main advantage of the extension method is to add new methods in the existing class without using inheritance. You can add new methods in the existing class without modifying the source code of the existing class. It can also work with sealed class.
You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called. At compile time, extension methods always have lower priority than instance methods defined in the type itself.
Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.
Essentially, an extension method is a special type of a static method and enable you to add functionality to an existing type even if you don't have access to the source code of the type. An extension method is just like another static method but has the “this” reference as its first parameter.
There's nothing better than just:
public class ModelToViewModelMapper
{
public ViewModel Map(Model b)
{
return new ViewModel { Foo = b.Foo.ToString() };
}
}
Extension methods can do the same work, but what if you want to change the mapping logic. It will be easy if you use dependency injection and non-static classes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With