entity framework + repository + unit or work question

后端 未结 1 902
夕颜
夕颜 2020-12-29 17:29

I\'m thinking about starting a new project using EF 4 and going through some articles, I found an article about EF with repository pattern and unit of work (http://blogs.msd

相关标签:
1条回答
  • 2020-12-29 17:46

    Yep - i believe it's possible.

    The kicker is how handle your Repositories.

    For example, each Repository should take a Context .. so just create one context and pass it to each repository.

    (code please!) Glad u asked :)

    public interface IOrderRepository
    {
        IQueryable<Order> FindAll();
    }
    
    public interface IOperationLogRepository
    {
        IQueryable<OperationLog> FindAll();
    }
    
    public interface IUnitOfWork
    {
        void Commit();
    }
    

    .

    public class SqlServerContext : ObjectContext, IUnitOfWork
    {
        public void SqlServerContext(string connectionString) 
            : base(connectionString)
        {
        }
    
        public void Commit()
        {
            this.SaveChanges();
        }
    
        // Your other POCO's and stuff here ..etc..
    }
    

    .

    public class OrderRepostiory : IOrderRepository
    {
        private readonly SqlServerContext _sqlServerContext;
        public void OrderRepostiory(SqlServerContext sqlServerContext)
        {
            _sqlServerContext = sqlServerContext;
        }
    
        public IQueryable<Order> FindAll()
        {
            _sqlServerContext.Orders;
        }
    }
    

    .. and finally, instantiation. Cause your a good boy/girl/rainbow unicorn, you would be using Dependency Injection ..

    public class SqlServerRegistry : Registry
    {
        public SqlServerRegistry(string connectionString)
        {
            For<SqlServerContext>()
                .HybridHttpOrThreadLocalScoped()
                .Use<SqlServerContext>()
                .Ctor<string>("connectionString")
                    .Is(connectionString);
    
            For<IOrderRepository>().Use<OrderRepository>();
            For<IOperationLogRepository>().Use<OperationLogRepository>();
        }
    }
    

    and because the SqlServerContext is defined as HttpOrThreadLocal, it will be instantied ONCE and reused in multiple Repositories.

    Don't know or understand DI/IoC ?

    then this would also work....

    private SqlServerContext _sqlServerContext;
    private IOrderRepository _orderRepository;
    private IOperationLogRepository _operationLogRepository;
    
    [TestInitialize]
    public void TestInitialise()
    {
        _sqlServerContext = new SqlServerContext(
                 ConfigurationManager.AppSettings["connectionString"]);
        _orderRepository = new OrderRepository(_sqlServerContext);
        _operationLogRepository= new OperationLogRepository(_sqlServerContext);
    }
    
    [TestMethod]
    public void SomeTest()
    {
        // Arrange.
        const int count = 10;
    
        // Act.
        var orders = _orderRepository.FindAll().Take(10).ToArray();
    
        // Assert.
        Assert.IsNotNull(orders);
        CollectionAssert.AllItemsAreNotNull(orders);
        Assert.AreEqual(count, orders.Length);
    }
    

    once more, that's all untested code which i just typed up, as I was thinking about answering this question.

    HTH.

    0 讨论(0)
提交回复
热议问题