0

さまざまなオブジェクトのマッピング値のテーブルを含むデータベースがあります。

たとえば、Colourテーブルには etc が含まれBLK > Black, BLU > BLUE, ORA > ORANGEており、CarTypeテーブルには4DH > Four-door hatchback, 4WD > Four wheel driveetcが含まれています。

私はEntity Frameworkコードファーストを使用しているので、このようなコンテキストを設定しています。

public class MappingContext : DbContext
{
    public MappingContext ()
        : base("Mappings")
    {
    }

    public DbSet<Colour> ColourMappings { get; set; }
    public DbSet<CarType> CarTypeMappings { get; set; }
}

データベース内の各テーブルに関連するすべてのオブジェクトは、Mapping次のような基本クラスから継承されます。

public class Mapping
{
    public int Id { get; set; }

    public string OrignalValue { get; set; }

    public string MappedValue { get; set; }
}

public class CarType : Mapping{}
public class Colour : Mapping{}

今私がしたいことは、マッピングを含む「テンプレート」で満たされた XML ファイルからこれらのマッピングを読み取り、DB に挿入することです。

これを行うには、次の方法があります。

public void InsertMappings(XDocument xml, string templateName, Type typeToInsert)
{
    // Here I find the relevent mappings 

    using (var repo = new Repository())
    {
        var mapppings = mappings.Select(mapping => new Mapping
        {
            MappedValue = mapping.Value,
            OrignalValue = GetCode(mapping)
        });

        foreach (var mapping in mapppings.ToList())
        {
            var map = (typeToInsert)mapping  // << This line will not compile

            repo.Insert(map);
        }

    repo.Save();
   }
}

これは、試行されたキャスト「(typeToInsert)mapping」を認識しないため、準拠しません。

したがって、基本的に私が知る必要があるのは、このマッピング オブジェクトをデータベースに挿入するときに、このマッピング オブジェクトを特定のマッピング オブジェクトにキャストする方法です。または、これを行うためのより良い方法についての提案!

4

1 に答える 1

1

見た目から、 orMappingとしてインスタンスをキャストしようとしていますが、これは基本クラスであるため、これらの型について何も知らないため機能しません。CarTypeColourMapping

コードでは、具象型 ie のインスタンスを作成し、typeToInsertそれを としてキャストする必要がありますMapping。次のようなことができます:

public void InsertMappings(XDocument xml, string templateName, Type typeToInsert)
{
    // Here I find the relevent mappings 

    using (var repo = new Repository())
    {
        foreach (var m in mappings)
        {
            // XML -> Entity
            var mapping = (typeToInsert)Activator.CreateInstance(typeToInsert);
            (mapping as Mapping).MappedValue = m.Value;
            (mapping as Mapping).OriginalValue = GetCode(m);
            // Update database
            repo.Insert(mapping);
        }
        repo.Save();
   }
}

おそらくここでもジェネリックを使用する必要があります。メソッドを次のようにリファクタリングできます。

public void InsertMappings<T>(XDocument xml, string templateName)
{
    // Here I find the relevent mappings 

    using (var repo = new Repository())
    {
        foreach (var m in mappings)
        {
            // XML -> Entity
            var mapping = (T)Activator.CreateInstance(typeof(T));
            (mapping as Mapping).MappedValue = m.Value;
            (mapping as Mapping).OriginalValue = GetCode(m);
            // Update database
            repo.Insert(mapping);
        }
        repo.Save();
   }
}

使用法

InsertMappings<CarType>(xmlDoc, "CarTemplate");
InsertMappings<Colour>(xmlDoc, "ColourTemplate");
于 2012-10-12T11:34:51.877 に答える