2

これがすでに尋ねられている場合は最初に申し訳ありませんが、この「特定のケース」に対する回答が見つかりません。

私は作業単位のインターフェースを持っています:

public interface IUnitOfWork
{
    DbContext Context { get; set; }
    void Dispose();
    void Save();
}

そして、汎用リポジトリ クラスを使用します。

public class GenericRepository<TEntity> where TEntity : class
    {

        private DbSet<TEntity> dbSet;

        private IUnitOfWork UnitOfWork { get; set; }
        private DbContext context { get { return UnitOfWork.Context; } }

        public GenericRepository(IUnitOfWork unitOfWork)
        {
            UnitOfWork = unitOfWork;
            this.dbSet = context.Set<TEntity>();
        }

        public virtual IEnumerable<TEntity> Get(
            Expression<Func<TEntity, bool>> filter = null,
            Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
            string includeProperties = "")
        {
            IQueryable<TEntity> query = dbSet;

            if (filter != null)
            {
                query = query.Where(filter);
            }

            foreach (var includeProperty in includeProperties.Split
                (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                query = query.Include(includeProperty);
            }

            if (orderBy != null)
            {
                return orderBy(query).ToList();
            }
            else
            {
                return query.ToList();
            }
        }

        public virtual TEntity GetByID(object id)
        {
            return dbSet.Find(id);
        }

        public virtual void Insert(TEntity entity)
        {
            dbSet.Add(entity);
        }

        public virtual void Delete(object id)
        {
            TEntity entityToDelete = dbSet.Find(id);
            Delete(entityToDelete);
        }

        public virtual void Delete(TEntity entityToDelete)
        {
            if (context.Entry(entityToDelete).State == EntityState.Detached)
            {
                dbSet.Attach(entityToDelete);
            }
            dbSet.Remove(entityToDelete);
        }

        public virtual void Update(TEntity entityToUpdate)
        {
            dbSet.Attach(entityToUpdate);
            context.Entry(entityToUpdate).State = EntityState.Modified;
        }
    }

MVC コントローラーでロジックを実行したくないので、ビジネスレイヤーを追加しました。私の質問は、コントローラーのどこで IUnitOfWork をインスタンス化 (および破棄) し、それをビジネス レイヤーに渡す必要があるかということです。例:

 public static class CircleLogic
    {
        public static void DeleteCircle(IUnitOfWork uow, int id)
        {
            try
            {
                var circleRep = new GenericRepository<Circle>(uow);

                var circle = circleRep.GetByID(id);
                 ......
                  circleRep.Delete(id);            

                uow.Save();

            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }

これを見たことがありますが、ビジネス層でインスタンス化したくありません。最善のアプローチは何ですか?

ありがとう!

4

2 に答える 2

3

あなたが提案したように、ビジネスレイヤーに渡しても害はありません。ただし、ビジネスレイヤーを完全に永続化しないようにしたい場合は、IRepository<T>インターフェイスを導入して代わりに渡すことをお勧めします。

オブジェクトの破棄に関しては、両方のIUnitOfWork/Repository クラスを実装して、ステートメントIDisposableを利用できるようにします。using

public ActionResult DeleteCircle(int id)
{
    using (IUnitOfWork uow = new UnitOfWork())
    {
        using (IRepository<Circle> repo = new GenericRepository<Circle>(uow))
        {
            CircleLogic.DeleteCircle(repo, id);
        }
        uow.Save();
    }
}

...

public static class CircleLogic
{
    public static void DeleteCircle(IRepository<Circle> repo, int id)
    {
        var circle = repo.GetById(id);
        ...
        repo.Delete(id);
    }
}
于 2012-10-18T08:27:20.970 に答える
1

具体的なUnitOfWork実装は永続層にある可能性が高いため、永続層またはビジネス層の 1 つ上の層でインスタンス化するのが賢明です。UI は、エンティティ/データを永続化するために使用しているテクノロジを認識してはなりません。

于 2012-10-18T08:02:39.867 に答える