0
\$\begingroup\$

I have an ASP.NET MVC 5 app and I map properties between model and viewmodel and vice versa.

I could use automapper to do this but since I don't need mapping rules to convince me to use it as I mostly do simple 1 to 1 mapping the performance overhead that automapper will add is not worthwhile. If I have mapping differences I'll map it manually where I reference it in my controller.

Here is my code

public class Dog
{
    public int Id { get; set; }

    public string Name { get; set; }

    public DateTime? BirthDate { get; set; }

    public int? CreatedBy{ get; set; }

    public DateTime? DateCreated { get; set; }
}

public class DogViewModel
{
    public int Id { get; set; }

    public string Name { get; set; }

    public DateTime? BirthDate { get; set; }


    public static implicit operator DogViewModel(Dog dm)
    {
        var vm= new DogViewModel{
            Id = dm.Id,
            Name = dm.Name,
            BirthDate = dm.BirthDate 
       };
        return vm;
    }

   public static implicit operator Dog(DogViewModel vm)
   {
       var dm = new Dog
       {
            Id = vm.Id,
            Name = vm.Name,
            BirthDate = vm.BirthDate 
        };
        return dm;
    }
}

Question: Is there a way to use a common mapping method in my ViewModel class or some other way to so that I don't have to duplicate code for copying properties?

\$\endgroup\$
  • \$\begingroup\$ Did you mean "eliminate"? \$\endgroup\$ – coderodde Oct 23 '15 at 11:13
  • 5
    \$\begingroup\$ The only way to have a common mapping method is by using reflection. If you're going to use reflection, you might as well use AutoMapper. \$\endgroup\$ – Jeroen Vannevel Oct 23 '15 at 11:25
  • \$\begingroup\$ Why are mapping the models at all? Is this really necessary? \$\endgroup\$ – t3chb0t Oct 23 '15 at 12:06
  • 1
    \$\begingroup\$ Make them derive from a common interface, that should allow for a common method for copying the properties. \$\endgroup\$ – rjnilsson Oct 23 '15 at 13:56
  • 4
    \$\begingroup\$ Ah, the classic "performance overhead". Automapper is pretty quick to be honest; it's never been a bottleneck for me. \$\endgroup\$ – RobH Oct 23 '15 at 15:28
2
\$\begingroup\$

Good for you for resisting the temptations of Automapper.

My answer to your question is to design around it.

I often write my viewmodels generically so I don't have to copy properties. This does not work in every case but works in more cases than you might think. Your viewmodel might become something like this:

public class BaseViewModel<T>  
{
    public T Model {get; set;}

    public BaseViewModel( T model)
    {
        this.Model = model;
    }
}

public class DogViewModel : BaseViewModel<Dog>
{
    public DogViewModel(Dog dog) : base(dog)
    {

    }

    public void DoSomething()
    {
        // do something with Dog object...
        Model.Weight = 10;
    }

}

I wrote a little about it on my site: http://www.samwheat.com/Post/The-function-of-ViewModels-in-MVC-web-development

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.