2016-08-25 4 views
0

ダッパー拡張機能を使用していて、class mapperに関する質問があります。私は2にアクセスする場合ダッパー拡張機能を使用する場合に複数のマップを追加する

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をスワップアウトをたくさんやって見つける

など、異なるスキーマとして、それらにいくつかのマッピングを行う必要がありますテーブルを作成する場合は、たとえば、これを2回実行する必要があります。

すべてのマッパークラスを一度に追加してコレクションを言う方法があります。アクセスされているテーブルによっては、正しいものが選択されていますか?

答えて

0

エンティティにカスタムリマッピングを適用するクラスをアプリケーション内に追加することができます。たとえば、これらの3つの空のクラスは、NotificationProfileにAnotherDifferentTypeOfDapperClassMapperが適用されている間に、ProfileおよびFileNotificationAdhocRecipientクラスにPrefixDapperTableMapperを適用します。限り、彼らは同じアセンブリにいるよう

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それらを見つけて使用するか、次のようなコードでマッピングアセンブリを設定することができます。

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

すべてのAutoClassMapperクラスは、すべてがd同じアセンブリではエルサレム?何と同じアセンブリですか? – TheEdge

+0

同じアセンブリで、cn.GetListメソッドを呼び出します。プロジェクト/アセンブリが1つしかない場合は問題ありません。ソリューション内に複数のプロジェクトがある場合に備えて、私はそれを述べました。 –

+1

呼び出しアセンブリでマップを持つ必要はありません。 DapperExtensions.SetMappingAssemblies(new [] {typeof(MyCustomClassMapper).Assembly});を使用できます。 –

関連する問題