Gal*_*len 6 nhibernate dto automapping valueinjecter
我正在使用ValueInjecter将属性从域模型映射到通过服务层提供的DTO.有问题的服务也接受更新...因此传入更新的DTO,然后将其注入域对象并保存.
// 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;
}
Run Code Online (Sandbox Code Playgroud)
现在所有上面的代码都更新了Property Member.Country.Code,这显然不是我需要它做的.
所以从文档中,我想我需要创建一个覆盖并得到这个:
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;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是在调试期间,CountryLookup永远不会被调用.
我能想到的可能原因:
我需要使用Dto上的CountryCode属性来调用countryService.LookupCode以返回在更新注入期间使用的正确对象.
使用 Omu 的建议/参考,这是问题的具体代码。
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2107 次 |
| 最近记录: |