1

I have a objects in my context:

abstract public partial class MyAbstractObject
{
    public int Id{set;get;}
    public string Name{set;get;}
}

public partial class MyChildObject: MyAbstractObject
{
    public string Details {set;get;} 
}

And i have a view models for ths objects:

public class MyAbstractViewModel
{
    public int Id{set;get;}
    public string Name{set;get;}
}

public class MyChildViewModel: MyAbstractViewModel
{
    public string Details {set;get;} 
}

Now for convert my view models to objects i use something like:

public MyChildObject MakeChildObject(MyChildViewModel vm)
{
  var child = new  MyChildObject();
  child.Id = vm.Id;
  child.Name = vm.Name;
  child.Details= vm.Details;

  return child;
 }

My problem in MyAbstractObject properties. If i have a several child objects i create a MakeChildObject methods for each child object. And all this methods have a same code lines for MyAbstractObject properties:

  child.Id = vm.Id;
  child.Name = vm.Name;

How can i minimize my code?

FishySwede
  • 1,563
  • 1
  • 17
  • 24
Kliver Max
  • 5,107
  • 22
  • 95
  • 148

3 Answers3

2

Consider if AutoMapper can be used in your case. It will automatically map objects of different types based on property names. It might save you the time of writing all the mapping code yourself.

https://github.com/AutoMapper/AutoMapper

You will then bootstrap the mapper like so:

Mapper.CreateMap<MyChildViewModel, MyChildObject>();

And instead of your method MakeChildObject you will use the following:

MyChildObject vmMapped = Mapper.Map<MyChildViewModel>(vm);
doorstuck
  • 2,299
  • 1
  • 22
  • 29
  • 1
    This sounds spot on for the problem described. – Mr. Mr. Mar 20 '15 at 10:47
  • I don't want to use any libraries. Just want to understad how peoples solving this problem. – Kliver Max Mar 20 '15 at 10:51
  • 4
    @KliverMax When enough people have solved the same problem someone, somewhere, will develop a library that solves the problem. Then people will stop trying to solve the same problem and use the well tested and robust library. AutoMapper is open source. You can look at how it does recursive mapping of objects. – doorstuck Mar 20 '15 at 10:53
1

Try the below code sample to solve your issue.

public class MyAbstractViewModel
{
    public int Id{set;get;}
    public string Name{set;get;}

  public void MakeChildObject(MyAbstractViewModel vm, MyAbstractObject child)
  {
    child.Id = vm.Id;
    child.Name = vm.Name;
  }
}

public class MyChildViewModel: MyAbstractViewModel
{
  public string Details {set;get;} 

  public MyChildObject MakeChildObject(MyChildViewModel vm)
  {
    var child = new  MyChildObject();
    this.MakeChildObject(this, child);

    child.Details= vm.Details;

    return child;
  }
}
petchirajan
  • 4,152
  • 1
  • 18
  • 20
  • Have a look into this link http://stackoverflow.com/questions/3672742/cast-class-into-another-class-or-convert-class-to-another – petchirajan Mar 20 '15 at 10:51
0

Have a look at automapper http://automapper.org/

It will automatically map similar named properties. It also includes a feature to map base objects. See also This post

Hintham
  • 1,078
  • 10
  • 29