实体框架6提供资料库和UOW开箱 [英] Entity framework 6 providing repositories and UoW out of the box

查看:109
本文介绍了实体框架6提供资料库和UOW开箱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

但你如何使用它?

我有一个 code首先项目设置,并尝试一些东西,这个新EF6。阅读各类文章/博客从至少2岁时约为EF4 / 5。但没有任何关于EF6。

I have a Code First project set up, and trying out some stuff with this new EF6. Reading all kinds of posts/blogs from at least 2 years old about EF4/5. But nothing whatsoever about EF6.

让我们说我有这些实体:

Let's say I have these entities:

public DbSet<Person> Persons { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<Invoice> Invoices { get; set; }

我仍然需要为每个实体库?还是会在足够用一些方法从CRUD做一些自定义计算一边?

Do I still need to create repositories for each entity? Or would a class suffice with some methods to do some custom calculations aside from CRUD?

我知道这行:

kernel.Bind<MyDbContext>().ToSelf().InRequestScope();

就足够了 DI ,而且它是通过构造到上层阶级注入适用。

Would suffice for DI, and that it will inject via constructor to upper layer classes where applicable.

该项目有一个类库和web项目 asp.net-MVC 。凡类的lib项目包含我的实体,具有迁移启用。

The project has a class library and a web project asp.net-mvc. Where the class lib project contains my entities and has Migrations enabled.

在这个问题上的任何光线实在是AP preciated。

Any light on this matter is really appreciated.

推荐答案

我已经添加在EF在几个项目(其同时利用信息库和UOW模式本身在其建设)之上的版本库层,和我已经做了与利用仿制药,这样我只需要一个文件,我所有的实体之一类。您可以决定,如果你想这样做或没有,但我发现它在我的项目非常有用。

I've added a Repository layer on top of EF (which utilizes both Repository and UoW patterns inherently in its construction) in a couple of projects, and I've done that with one class that utilizes generics so that I only needed one file for all of my entities. You can decide if you want to do it or not, but I've found it useful in my projects.

我的仓库通常都开始了像我所如下图所示,与更多的扩展方法跟进,如果/当我遇到一个需要他们(当然我不是展示所有的人,这就是为你来决定如何实现你的资料库)。

My repositories have typically started out like what I've shown below, following up with more extension methods if/when I come across a need for them (obviously I'm not showing all of them, that's up for you to decide how to implement your repository).

public class Repository<T> : IRepository<T> where T : class
{
    protected IDbContext Context;
    protected DbSet<T> DbSet { get { return Context.Set<T>(); } }

    public Repository(IDbContext context = null)
    {
        Context = context ?? new DbContext();
    }

    public void Add(T newRecord)
    {
        DbSet.Add(newRecord);
    }

    public void Update(T record)
    {
        var entry = Context.Entry(record);
        DbSet.Attach(record);
        entry.State = EntityState.Modified;
    }

    public void Remove(T record)
    {
        Context.Entry(record).State = EntityState.Deleted;
        DbSet.Remove(record);
    }

    public IQueryable<T> Where(Expression<Func<T, bool>> predicate)
    {
        return DbSet.Where(predicate);
    }

    public bool Contains(Expression<Func<T, bool>> predicate)
    {
        return DbSet.Count(predicate) > 0;
    }

    public int Count(Expression<Func<T, bool>> predicate)
    {
        return DbSet.Count(predicate);
    }

    public int Save()
    {
        return Context.SaveChanges();
    }
}

我使用存储库2个主要的原因:

I've used repositories for 2 main reasons:


  1. 单元测试。这样的模式让我假底层的数据,而不必在我的数据库错误数据。我需要做的仅仅是创建 IRepository 的另一种实现方式,它使用内存中的列表作为其数据源,我都准备好了我的网页来查询资料库

  1. Unit testing. Doing this pattern allows me to fake the underlying data without having to have bad data in my database. All I need to do is simply create another implementation of IRepository that uses an in-memory list as its data source, and I'm all set for my pages to query that repository.

扩展。一个公平的次数我已经把一些方法进入我的仓库,因为我发现自己经常做同样的逻辑,在我的控制器查询。这一直是非常有用的,特别是您的客户端code并不需要知道它是如何做的,只是它是这样做(这将使它更容易,如果您需要更改一个文件的逻辑VS 。多个文件)。

Extensibility. A fair number of times I've put in some methods into my repository because I found myself constantly doing the same logic with queries in my controllers. This has been extremely useful, especially since your client-side code doesn't need to know how it's doing it, just that it is doing it (which will make it easier if you need to change the logic of one file vs. multiple files).

这不是全部,很明显,但应该够这个答案。如果你想要更多的了解关于这个话题,我没有写上一篇博客文章,你的可以在这里找到

This not all of it, obviously, but that should be enough for this answer. If you want to know more on this topic, I did write a blog post on it that you can find here.

祝你好运。

这篇关于实体框架6提供资料库和UOW开箱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆