2011-01-24 3 views
6

ValueInjecter을 사용하여 도메인 모델의 속성을 서비스 레이어를 통해 제공되는 DTO에 매핑합니다. 문제의 서비스는 또한 업데이트를 받아들입니다 ... 그래서 업데이트 된 DTO가 전달되고 이것은 도메인 객체에 주입되어 저장됩니다.값 인젝터 : Dto to Domain Model (NHibernate)

// 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; 
    } 

지금이 위의 코드가하는 모든이는 내가하는 데 필요한 분명하지 않은 속성 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; 
    } 

내 문제가 디버깅 중입니다, CountryLookup가 호출되지 없구요. 국가 유형과 일치하지에 injecter 값을 일으키는

  • NHibernate에 프록시 클래스 : 내가 생각할 수있는

    가능한 이유? 그것이 평평하게하는 동안 작동하기 때문에 이것은 의미가 없습니다.

  • 아마도 어떤 이유로 인해 비평 탄화가 발생하지 않을 수 있습니다. 즉 DTO는 COUNTRYCODE하고 도메인 나는 업데이트 주입시 사용하는 올바른 개체를 반환하는 countryService.LookupCode를 호출 할 DTO에 COUNTRYCODE 속성을 사용할 필요가 Country.Code

입니다.

+0

달성하려는 목표는 무엇입니까? 첫 번째 시도는 효과가 있지만 필요한 것은 아닙니다. 무엇이 필요합니까? – Omu

+0

CountryLookup이라는 이름의 주입이 문자열에서 Country로 unflat됩니다. 즉, string 유형의 CountryCode에서 값을 가져 와서 국가 – Omu

+0

Country.Code에 넣습니다. @Omu thats correct Country Country : Country : USA, Name : United States} 그리고 내 Dto는 CountryCode로 전달합니다. "CA"는 Country.Code 속성을 CA로 설정하고 'Name'속성을 미국으로 남겨 둡니다. 미리 채워진 도메인 객체를 업데이트하고 있다는 것을 기억하십시오.이 때문에 올바른 Country 객체를 찾기 위해 country 메소드를 호출해야합니다. Dto에서 'Code'를 캡처 한 다음 해당 코드를 사용하여 올바른 Country 객체를 조회하려고합니다. – Galen

답변

3

을 상속 다른 유형.

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; 
} 
+1

네, 이것은 작동해야합니다 (ICountryService 생성자 매개 변수를 어디에 설정했는지는 알지 못함). 모든 것을 주입하는 좀 더 일반적인 방법을 찾고 싶습니다. – Omu

+0

생성자 매개 변수는 Castle Windsor를 통해이 경우에 설정됩니다. 의존성 주입. 실제 look up service call이 그다지 중요하지 않기 때문에, 솔루션을위한 소용없는 점이 있습니다. – Galen

+0

또한, 귀하의 제안을 사용하여이 프로젝트에 대한 일반적인 해결책을 고려할 것입니다, 감사합니다 :) – Galen

0

프레임 워크가 setter 메서드를 호출합니까? 대부분의 DI 프레임 워크에서 표준은 setMethod()에서 소문자입니다. 그냥 첫 번째로 생각한 추천입니다.

+0

SetValue 메서드를 언급하고 있습니까? 그렇다면 ValueInjecter 프레임 워크의 일부입니다. – Galen

+0

Nick, 이것은 Java가 아니라 .NET입니다. .NET 표준은 낙타의 경우가 아니라 메소드 이름의 파스칼 케이스입니다. – Phill

3

unflattening이 작업을 수행하는 것입니다 :

entity.Country.Code <- dto.CountryCode 

무엇을 당신이 필요로하는 것은 :

entity.Country <- dto.CountryCode 

그래서 솔루션 당신이 나라에 COUNTRYCODE에서 갈 것 인 ExactValueInjection을 상속하는 것을 위해.

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;} 
    } 

이들를 사용 : 당신이을하는 것이 좋습니다 무엇

내가 이런 일이 어디에 어떻게 난 내 http://awesome.codeplex.com

의 다른 프로젝트의 라이브 데모에서했던 것과 같은 것입니다 엔티티에서 dto 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; 
      } 
     } 

이러한 주사는 int에서? 나라 다시뿐만 아니라이 문제에 대한 특정 코드이었다 OMU에서 제안/참조를 사용 법인

+0

이 의견은 내 문제에 대한 구체적인 대답으로 이어지지 만 정확한 답변은 아닙니다. 도움이 될 수 있었나요? – Galen