0

Consider this entity:

public class CondRule
{
    public virtual decimal Id { get; set; }
    public virtual string Name { get; set; }
    public virtual CondRuleType RuleType { get; set; }
    public virtual string Statement { get; set; }
}

and CondRuleType is:

public class CondRuleType
{
   public virtual int Id { get; set; }
   public virtual string Name { get; set; }
}

It is obvious that there is a one to one relation between CondRule and CondRuleType entities.

Also I have CondRuleDto:

public class CondRuleDto
{
    public decimal Id { get; set; }
    public string Name { get; set; }
    public CondRuleType RuleType { get; set; }
}

I have mapped CondRule to CondRuleDto using AutoMapper:

Mapper.CreateMap<CondRule, CondRuleDto>();

When I call Session.Get to get CondRule by id and the map the result to CondRuleDto, AutoMapper does not resolve proxies (here RuleType). Here is my code:

var condRule = Session.Get<CondRule>(id);
var condRuleDto = Mapper.Map<CondRuleDto>(condRule);

When I watch condRuleDto, RuleType property is a NHibernate proxy. I want AutoMapper to map RuleType proxy to a POCO. How to make this work?

PS: I have to mention that when I use query and use automapper's Project, it will result a list with no proxies (I know that Project make this happen. May be I need something like Project to use after Session.Get):

Session.Query<CondRule>().Project().To<CondRuleDto>().ToList()
alisabzevari
  • 8,008
  • 6
  • 43
  • 67

1 Answers1

0

Casts won't change the underlying object (i.e. your CondRuleType will be still a proxy even if you map its instance to another property of type CondRuleType).

It seems like you need to create a custom mapping where CondRule.RuleType is mapped creating a new instance of CondRuleType.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • How to do that custom mapping? – alisabzevari Jun 30 '15 at 09:45
  • @alisabzevari try this: http://stackoverflow.com/questions/9132251/how-to-handle-custom-properties-in-automapper (I'm at work and I can't try things... but it might be in the right track) – Matías Fidemraizer Jun 30 '15 at 09:55
  • Thanks, but I want to tel automapper to unproxy every proxied object automatically. With you solution I have to define custom mapping for every reference property of every entity. – alisabzevari Jun 30 '15 at 10:38
  • @alisabzevari With some effort I'm sure you can generalize the solution... At the end of the day, you can't expect AutoMapper to solve your scenario "as is"... I believe that it might be fixed using some reflection to introspect property types and create an instance using `Activator.CreateInstance`... – Matías Fidemraizer Jun 30 '15 at 10:51
  • I know that I have to use reflection. The problem is I don't know where I have to write this code or how to tell automapper when it sees these classes do my operation to performa mapping – alisabzevari Jun 30 '15 at 10:54
  • BTW your answer is correct. I have to ask another question. – alisabzevari Jun 30 '15 at 11:17
  • @alisabzevari Maybe. BTW, have you thought about wrapping that reflection into a re-usable delegate that can be given to many automapper class maps? – Matías Fidemraizer Jun 30 '15 at 13:24
  • I wanted to write one CreateMap for any proxy class – alisabzevari Jun 30 '15 at 13:30
  • @alisabzevari so this is what I try to say :D you can have some helper class like `CustomMappings.UnproxyAssociations(T Source someObject)`... there's more hard work than this, but I want to give you the idea........ – Matías Fidemraizer Jun 30 '15 at 13:41