1

さて、Code Firstの依存性注入を伴う作業単位を使用した例はたくさんあり、汎用リポジトリーやその他すべての優れたものを使用しています。

データベースファースト(dbContext Generator(T4)を使用したedmx)、関数インポートとしてのストアドプロシージャ、依存性注入を使用した作業単位でこれを行う例はありますか?

4

1 に答える 1

2

code first または dbfirst のコンテキストは同じになります (DbContext)。

ストアド プロシージャは、context.Customers を呼び出す代わりにリポジトリにマップされます。context.Database.Query("Proc_whatever") を呼び出します。

ヘルプが必要な特定の場所はありますか?コード サンプルがあるかもしれませんが、上記のすべては、di、コード ファースト、汎用リポジトリなどと同じ方法で行われます。UnitOfWork を実装するための唯一の変更は、リポジトリは SaveChanges を呼び出しません。UnitOfWork インターフェイスに Save() というメソッドがあり、それが変更の保存を呼び出します。

https://github.com/adamtuliper/EF5-for-Real-Web-Applicationsのコードを更新して、作業単位を含めます。私は実装が好きではありません.

IUnitOfWork を注入する IUnitOfWork には、同じく注入されて Context にマップされる IContext が含まれています。IUnitOfWork は UnitOfWork 具象実装にマップされます。UnitOfWork の具体的な実装は、リポジトリを参照します。

これは部分的に私の頭のてっぺんから外れているので、コンパイルエラーを許してください、それは原則として示すことです


public class YourContext : DbContext, IContext
{
   //just a regular DbContext class except use IDbSet
   public IDbSet Customers { get; set; }
}

public interface IUnitOfWork
{
     ICustomerRepository CustomerRepository { get; }
     IOrderRepository OrderRepository { get; }
     void Save();
}


 public class UnitOfWork : IUnitOfWork, IDisposable
 {
        private readonly IContext _context;
        private ICustomerRepository _customerRepository;
        private IOrderRepository _orderRepository;
        private bool _disposed = false;

        public UnitOfWork(IContext context)
        {
            _context = context;
        }

        public ICustomerRepository CustomerRepository
        {
            get
            {
                if (this._customerRepository == null)
                {
                    this._customerRepository = new CustomerRepository(_context);
                }
                return _customerRepository;
            }
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!this._disposed)
            {
                if (disposing)
                {
                    ((IDisposable)_context).Dispose();
                }
            }
            this._disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

public class CustomerController : Controller
{
   private readonly IUnitOfWork _unitOfWork;
   public CustomerController(IUnitOfWork unitOfWork)
   {
      _unitOfWork = unitOfWork;
   }

   [AutoMap(typeof(Customer), typeof(CustomerIndexViewModel)]
   public ActionResult Index()
   {
        return _unitOfWork.CustomersRepository.GetAll();
        //or if not using AutoMapper, use the viewmodel directly:
        //return _unitOfWork.CustomersRepository.GetAll().Select(c => new CustomerIndexViewModel
                                                    {
                                                        CustomerId = c.CustomerId,
                                                        Address = c.Address,
                                                        City = c.City,
                                                        State = c.State,
                                                        FirstName = c.FirstName,
                                                        LastName = c.LastName
                                                    }).ToArray(); ;
   }
}

proc を使用するには、CustomerRepository で次のようにします。


public Customer GetById(int id)
{
      return this.Context.Database.SqlQuery("Proc_GetCustomer @customerID", new SqlParameter("@customerID", id)).Single();
      //instead of:  return this.Context.Customers.Include(o => o.Orders).Single(o => o.CustomerId == id);
}

于 2012-10-31T04:33:02.690 に答える