0

dapper 拡張機能を使用していますが、クラス マッパーについて質問があります。残念ながら、私のテーブルのほとんどは、異なるスキーマなどのマッピングを行う必要があります。

したがって、私は通常、以下のように DefaultMapper を頻繁に交換していることがわかります。

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

たとえば、2 つのテーブルにアクセスする場合、これを 2 回行う必要があります。

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

4

1 に答える 1

0

エンティティにカスタムの再マッピングを適用する一連のクラスをアプリ内に追加できます。たとえば、これら 3 つの空のクラスは PrefixDapperTableMapper を Profile および FileNotificationAdhocRecipient クラスに適用し、AnotherDifferentTypeOfDapperClassMapper を NotificationProfile に適用します。

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 })
于 2016-08-25T11:40:43.800 に答える