2016-08-25 6 views
0

더 복잡한 확장 프로그램을 사용 중이고 class mapper에 대한 질문이 있습니다. 나는이 액세스하는 경우덤프 확장 프로그램을 사용할 때 다중 맵 추가

public Hierarchies HierarchyGetByName(string aName) 
{ 
    Hierarchies result; 

    using (SqlConnection cn = GetSqlConnection()) 
    { 
     cn.Open(); 

     Type currModelMapper = DapperExtensions.DapperExtensions.DefaultMapper; 
     try 
     { 
      DapperExtensions.DapperExtensions.DefaultMapper = typeof(HierarchiesMapper); 
      IFieldPredicate predicate = Predicates.Field<Hierarchies>(f => f.Name, Operator.Eq, aName); 
      result = cn.GetList<Hierarchies>(predicate).FirstOrDefault(); 
     } 
     finally 
     { 
      DapperExtensions.DapperExtensions.DefaultMapper = currModelMapper; 
     } 


     cn.Close(); 
    } 

    return result; 
} 

: 불행하게도 내 테이블의 대부분은 일부 매핑 등 다른 스키마 그래서 내가 일반적으로 아래에 따라, DefaultMapper 스와핑을 많이하고있는 중이 야 찾을

으로 그들에게 할 필요가 예를 들어 두 번해야합니다.

컬렉션을 말하기 위해 모든 매퍼 클래스를 한 번에 추가 할 수있는 방법이 있으며 액세스중인 테이블에 따라 올바른 것이 선택되어 있습니까?

답변

0

엔티티에 맞춤 매핑을 적용하는 클래스를 앱에 추가 할 수 있습니다. 예를 들어, 이러한 3 개의 빈 클래스는 AnotherDifferentTypeOfDapperClassMapper가 NotificationProfile에 적용되는 동안 PrefixDapperTableMapper를 Profile 및 FileNotificationAdhocRecipient 클래스에 적용합니다.

public class ProfileMapper : PrefixDapperTableMapper<Domain.Entities.Profile> 
{ 
} 

public class FileNotificationAdhocRecipientMapper : PrefixDapperTableMapper<Domain.Entities.FileNotificationAdhocRecipient> 
{ 
} 

public class NotificationProfileMapper : AnotherDifferentTypeOfDapperClassMapper<Domain.Entities.NotificationProfile> 
{ 
} 

과 실제 매핑 코드는 별도의 매퍼에 존재하는 (나는 AnotherDifferentTypeOfDapperClassMapper 표시되지했지만 그 아래에 유사하다) 같은 어셈블리로 그들이있는 한

public class PrefixDapperTableMapper<T> : ClassMapper<T> where T : class 
{ 
    public PrefixDapperTableMapper() 
    { 
     AutoMap(); 
    } 

    //name or schema manipulations in some overrides here. 
} 

을 DapperExtensions 찾아서 사용하거나 유사한 코드로 매핑 어셈블리를 설정할 수 있습니다 것만큼 그들은 모든 D입니다 그래서

DapperExtensions.DapperExtensions.SetMappingAssemblies({ typeof(ProfileMapper).Assembly }) 
+0

내 모든 AutoClassMapper 클래스가 발견됩니다 같은 집회에서 엘 캐어? 뭐라구? 같은 어셈블리? – TheEdge

+0

동일한 어셈블리에서 cn.GetList 메서드를 호출합니다. 하나의 프로젝트/어셈블리 만 있다면 문제가되지 않습니다. 솔루션 내에 여러 프로젝트가있는 경우에 대비하여 언급했습니다. –

+1

호출 어셈블리에 맵이있을 필요가 없습니다. 다음을 사용할 수 있습니다. DapperExtensions.SetMappingAssemblies (new [] {typeof (MyCustomClassMapper) .Assembly}); –