Value Injector: Dto to Domain (NHibernate) Model

I am using ValueInjecter to map properties from a domain model to a DTO serviced through a service level. The service in question also receives updates ... therefore, the updated DTO is transmitted, and then it is entered into the domain object and saved.

// Domain public class Member { public Country Country { get; set; } } public class Country { public string Code { get; set; } public string Name { get; set; } } //Dto public class MemberDto { public string CountryCode { get; set; } } //Transformation Method attempt 1 public Member InjectFromDto (MemberDto dto, Member source) { source = source.InjectFrom<UnflatLoopValueInjection>(dto); return source; } 

Now all of the above code updates Property Member.Country.Code, which is clearly not what I need for this.

So, from the docs, I decided that I needed to create an override and get the following:

 public class CountryLookup: UnflatLoopValueInjection<string, Country> { protected override Country SetValue(string sourcePropertyValue) { return countryService.LookupCode(sourcePropertyValue); } } //revised transformation call //Transformation Method attempt 2 public Member InjectFromDto (MemberDto dto, Member source) { source = source.InjectFrom<UnflatLoopValueInjection>(dto) .InjectFrom<CountryLookup>(dto); return source; } 

My problem is during debugging, CountryLookup is never called.

Possible reasons I can think of:

  • Nhibernate proxy classes that inject values ​​do not match country type? This does not make sense, because it works during flattening.
  • Perhaps for some reason unflattening does not shoot. Ie Dto - CountryCode and Domain - Country.Code

I need to use the CountryCode property on Dto to call countryService.LookupCode to return the correct object to use during the update.

+6
nhibernate valueinjecter dto automapping
source share
3 answers

Using a suggestion / link from Omu, this was a specific problem code.

  public class CountryLookup : ExactValueInjection { private ICountryService countryservice; public CountryLookup(ICountryService countryService) { this.countryService = countryService; } protected override bool TypesMatch(Type s, Type t) { return (s == typeof(string)) && (t == typeof (Country)); } protected override Object SetValue(object v) { if (v == null) return null; var country = countryService.LookupCode((string) v); return country; } public override string SourceName() { return "CountryCode"; } public override string TargetName() { return "Country"; } } public Member InjectFromDto (MemberDto dto, Member source) { source = source.InjectFrom<UnflatLoopValueInjection>(dto) .InjectFrom<CountryLookup>(dto); return source; } 
+3
source share

unflattening would have to do this:

 entity.Country.Code <- dto.CountryCode 

what you need:

 entity.Country <- dto.CountryCode 

so the solution for you would be to inherit ExactValueInjection, where you could go from CountryCode to country.

what I recommend you do , does the same thing as in a live demo of my other project http://awesome.codeplex.com

where i have something like this:

  public class Entity { public int Id{get;set;} } public class Member : Entity { public Country Country{get;set;} } public class MemberDto : DtoWithId { public int? Country {get;set;} } 

and use these injections to go from entity to dto and back

  public class NullIntToEntity : LoopValueInjection { protected override bool TypesMatch(Type sourceType, Type targetType) { return sourceType == typeof(int?) && targetType.IsSubclassOf(typeof(Entity)); } protected override object SetValue(object sourcePropertyValue) { if (sourcePropertyValue == null) return null; var id = ((int?) sourcePropertyValue).Value; dynamic repo = IoC.Resolve(typeof(IRepo<>).MakeGenericType(TargetPropType)); return repo.Get(id); } } //(you also need to have a generic repository, notice IRepo<>) public class EntityToNullInt : LoopValueInjection { protected override bool TypesMatch(Type sourceType, Type targetType) { return sourceType.IsSubclassOf(typeof (Entity)) && targetType == typeof (int?); } protected override object SetValue(object o) { if (o == null) return null; return (o as Entity).Id; } } 

will these injections handle not only the transition from int? Country and vice versa, as well as any other type that inherits Entity

+3
source share

Is the environment a setter method call? On most DI infrastructures, the standard has lowercase letters in setMethod (). Just the first thought.

0
source share

All Articles