0

たとえば、エンティティ オブジェクトがあります。EF から自動生成された記事。次の方法でモデルを作成する場合(エンティティオブジェクトの作成、編集用):

public class ArticleModel
{
    // properties
}

Create、Edit アクションでは、モデルからエンティティの各プロパティに設定します。通常、モデルからエンティティへの自動設定プロパティとエンティティからモデルへの自動ロード プロパティには、次のクラスを使用します。

public interface IEntityModel<TEntity> where TEntity : EntityObject
{
    void LoadEntity(TEntity t);
    TEntity UpdateEntity(TEntity t);
    TEntity CreateEntity();
}

public class EntityModel<TEntity> : IEntityModel<TEntity> where TEntity : EntityObject
{
    public virtual void LoadEntity(TEntity t)
    {
        var entityType = typeof(TEntity);
        var thisType = this.GetType();
        PropertyInfo[] entityProperties = entityType.GetProperties();
        PropertyInfo[] thisProperties = thisType.GetProperties();
        PropertyInfo temp = null;

        lock (this)
        {
            thisProperties.AsParallel()
                   .ForAll((p) =>
                   {
                       if ((temp = entityProperties.SingleOrDefault(a => a.Name == p.Name)) != null)
                           p.SetValue(this, temp.GetValue(t, null), null);
                   }); 
        }
    }

    public virtual TEntity UpdateEntity(TEntity t)
    {
        var entityType = typeof(TEntity);
        var thisType = this.GetType();
        PropertyInfo[] entityProperties = entityType.GetProperties();
        PropertyInfo[] thisProperties = thisType.GetProperties();
        PropertyInfo temp = null;

        lock (t)
        {
            entityProperties.AsParallel()
                   .ForAll((p) =>
                   {
                       if (p.Name.ToLower() != "id" && (temp = thisProperties.SingleOrDefault(a => a.Name == p.Name)) != null)
                           p.SetValue(t, temp.GetValue(this, null), null);
                   }); 
        }

        return t;
    }

    public virtual TEntity CreateEntity()
    {
        TEntity t = Activator.CreateInstance<TEntity>();

        var entityType = typeof(TEntity);
        var thisType = this.GetType();
        PropertyInfo[] entityProperties = entityType.GetProperties();
        PropertyInfo[] thisProperties = thisType.GetProperties();
        PropertyInfo temp = null;

        lock (t)
        {
            entityProperties.AsParallel()
                   .ForAll((p) =>
                   {
                       if (p.Name.ToLower() != "id" && (temp = thisProperties.SingleOrDefault(a => a.Name == p.Name)) != null)
                           p.SetValue(t, temp.GetValue(this, null), null);
                   }); 
        }

        return t;
    }

}

このように、モデルが EntityModel から継承され、プロパティ名がエンティティ プロパティと一致する場合、作成および編集アクションで次のように記述できます。

        // for create entity
        Article a = model.CreateEntity();
        // for update entity
        a = model.UpdateEntity(a);
        // for load from entity
        model.LoadEntity(a);

私は自分のクラスの弱点を知っています。たとえば、一部のプロパティがビューから編集されていない場合、UpdateEntity() メソッドでエンティティの古い値が削除されます。

質問: 私が知らない別の方法または一般的な方法はありますか?

4

1 に答える 1

0

基本的に、あなたは自分で投稿されたウェブページからの変更を伴うモデルの更新全体を書いています。MVCはこれを箱から出して行うことができます。コントローラには、モデルを更新するUpdateModelというメソッドがあります。

例はここここ、ここにあります。それがどのように機能するかについての詳細は、ここにあります。

于 2012-08-16T10:36:42.480 に答える