3

I am trying to separate my MVC3 project into a proper DAL/Domain/ViewModel architecture, but I'm running into a problem with AutoMapper and mapping calculated fields from my domain to my view model.

Here's an example of what I'm trying to do:

Interface

public interface IRequirement
{
    int Id { get; set; }
    ... bunch of others
    public decimal PlanOct { get; set; }
    public decimal PlanNov { get; set; }
    public decimal PlanDec { get; set; }
    ... and so on
    decimal PlanQ1 { get; }
    ... etc
    decimal PlanYear { get; }
    ... repeat for ActualOct, ActualNov ... ActualQ1 ... ActualYear...
}

Domain Model

public class Requirement : IRequirement
{
    public int Id { get; set; }
    ... bunch of others
    public decimal PlanOct { get; set; }
    public decimal PlanNov { get; set; }
    public decimal PlanDec { get; set; }
    ... and so on
    public decimal PlanQ1 { get { return PlanOct + PlanNov + PlanDec; } }
    ... etc
    public decimal PlanYear { get { return PlanQ1 + PlanQ2 + PlanQ3 + PlanQ4; } }
    ... repeat for ActualOct, ActualNov ... ActualQ1 ... ActualYear...
}

There are also VarianceX properties, i.e. VarianceOct which is calculated as (PlanOct - ActualOct), etc.

My view model looks almost exactly the same, except instead of calculated fields it has the default getter/setter syntax, for example:

public decimal PlanQ1 { get; set; }

My AutoMapper config in Global.asax looks like this:

Mapper.CreateMap<Domain.Abstract.IRequirement, Models.Requirement.Details>();

This works fine on all properties except the calculated ones. None of my calculated fields (i.e. *Q1, *Q2, *Q3, *Q4, *Year, and all the Variance* fields) are actually mapped -- they all show up with the default value of 0.00.

I'm pretty stumped on this, and I'm also a novice at this and AutoMapper, so maybe I missed something. My intuition is that since the property signatures aren't identical (i.e. the domain object has only a non-default getter and no setter, while the view model has default getter and setter) then AutoMapper isn't picking it up. But I also did this:

Mapper.CreateMap<Domain.Abstract.IRequirement, Models.Requirement.Details>()
            .ForMember(dest => dest.PlanQ1, opt => opt.MapFrom(src => src.PlanQ1);

And it still resolved to 0. I confirmed this in the debugger as well.

What am I doing wrong?

Thanks in advance.

EDIT 1

After following Wal's advice I ran the test and it worked, so I began working backwards one step at a time, first pasting in the Field1/Field2/Field3 parts into the interface/domain/view model classes and verifying it worked in my controller, then changing one thing at a time. What I found is that, since I am dealing with decimal types, if I hard-code in integer or double values then I get zero, but if I cast to a decimal or use a decimal literal then it works. But only if I manually set them, not if I pull the values from the database.

In other words, this works (i.e. PlanQ1 = 6):

var D = new Requirement { PlanOct = (decimal) 1.0, PlanNov = (decimal) 2.0, PlanDec = (decimal) 3.0 };
var V = Mapper.Map<IRequirement, Details>(D);

And this works:

var D = new Requirement { PlanOct = 1M, PlanNov = 2M, PlanDec = 3M };
var V = Mapper.Map<IRequirement, Details>(D);

But this does not (pulling a single domain object from a repository object, that in turn pulls from SQL Server using Entity Framework):

var D = requirementRepository.Requirement(5);
var V = Mapper.Map<IRequirement, Details>(D);

With the above all I get is 0 for PlanQ1 and PlanYear. I verified that PlanOct = 1, PlanNov = 2, and PlanDec = 3 in the domain object (D). I also verified that the type in all objects, including the EF generated object, is decimal, and the SQL Server type is decimal. I even tried mapping to a created view model, just to rule that out, and I still get 0 for PlanQ1 and PlanYear:

var D = requirementRepository.Requirement(5);
var V = new Details();
Mapper.Map<IRequirement, Details>(D, V);
Dave
  • 1,057
  • 2
  • 12
  • 18

3 Answers3

2

is PlanQ1 a member of IRequirement ? you have implied it is by your last code snippet but if it isn't then you will get the behavior exactly as you describe.

Consider a simplied example of what you are doing:

public interface IFoo
{
    string Field1 { get; set; }
    string Field2 { get; set; }
    //string Field3 { get; }
}

public class Foo1 : IFoo
{
    public string Field1 { get; set; }
    public string Field2 { get; set; }
    public string Field3 { get { return Field1 + Field2; } }
}
public class Foo2
{
    public string Field1 { get; set; }
    public string Field2 { get; set; }
    public string Field3 { get; set; }
}

Note in this instance I have omitted Field3 from the interface; now when I run the following the mapping fails

[TestMethod]
public void Map()
{
    Mapper.CreateMap<IFoo, Foo2>();
    var foo1 = new Foo1() { Field1 = "field1", Field2 = "field2" };
    var foo2 = new Foo2();
    Mapper.Map(foo1, foo2);
    Assert.AreEqual("field1field2", foo2.Field3);//fails, not mapped
}

So if I comment in Field3 from IFoo everything works again. Check this simplified example with your code.

wal
  • 17,409
  • 8
  • 74
  • 109
  • Good point. Unfortunately, yes PlanQ1 and the others are defined in the interface, as decimal PlanQ1 { get; }. Is it failing because it only defines a getter while the view model has a getter and setter? – Dave Nov 16 '12 at 14:26
  • Nope. I just added a default set{} to the interface so now there are a setter and getter for the domain model interface, domain model class, and view model class. Still get 0.00 in the result. – Dave Nov 16 '12 at 14:34
  • what you most recently commented is describing my example - how is yours different from the example i gave? there must be a difference causing the erroneous behaviour. – wal Nov 16 '12 at 14:43
  • OK I updated my post to include new code. The automap seems to work, now it looks like it is an issue where it is not picking up the underlying value from the database. Weird. – Dave Nov 16 '12 at 15:37
  • @Dave I've read your updated question. You said: `I verified that PlanOct = 1, PlanNov = 2, and PlanDec = 3 in the domain object...and I still get 0 for PlanQ1` - ok, if that is true then you need to put a breakpoint on the `PlanQ1` property and see what is going on; this must not be called during the automapper conversion (or perhaps it is, but you need to determine this) - Secondly, what is the `typeof(D)` ? I'm not familiar with EF but perhaps its returning a proxy object which has a dummy/fake `Field3` method that always returns 0. – wal Nov 16 '12 at 21:40
2

Consider @Wal post, Try this Map,

Mapper.CreateMap<IFoo, Foo2>()
    .ForMember(destination => destination.Field3, options => options.MapFrom(source => source.Field1 + source.Field2));

And

[TestMethod]
public void Map()
{
    Mapper.CreateMap<IFoo, Foo2>()
        .ForMember(destination => destination.Field3, options => options.MapFrom(source => source.Field1 + source.Field2));
    var foo1 = new Foo1() { Field1 = "field1", Field2 = "field2" };
    var foo2 = new Foo2();
    Mapper.Map(foo1, foo2);
    Assert.AreEqual("field1field2", foo2.Field3); // True
}
Prasad Kanaparthi
  • 6,423
  • 4
  • 35
  • 62
  • Yes I did this yesterday, but doesn't this violate all kinds of OO encapsulation principles? A field's calculation should be known only to that field and clients should not care about the implementation, right? I mean I can do this if it really is the only way around the issue, but it smells bad to me. Unless I'm missing something? – Dave Nov 16 '12 at 14:28
1

Just realized this was left unanswered so I wanted to close it out. Technically it is unanswered because I couldn't get Automapper to play nice in this scenario for some reason. What I wound up doing is going back and creating a couple of mapping methods inside my repository, one to map a single instance of the DAL object to the IRequirement object, and one to map a collection. Then in the repository instead of calling Mapper.Map I just call my custom mapping methods and it works perfectly.

I still don't understand why this doesn't work, but I've run into a few other classes where Automapper just throws up and I have to manually map at least one or two fields, though Automapper does take care of the rest in those cases.

I'm sure there's something about it I just don't see yet. But in any case, falling back to partial or fully manual mapping was my workaround.

Dave
  • 1,057
  • 2
  • 12
  • 18