BaseEntity, 所有的业务表都继承这个类,每个表的主键都是GUID, 主键名Id.

public abstract class BaseEntity{
public BaseEntity()
{
this.Id = Guid.NewGuid();
}
public vritual Guid Id{get;set;}
}

Student, 例子类

public Student:BaseEntity
{
public Name {get;set;}
}

StudentContext

public class StudentContext : DbContext
{
public DbSet<Student> Students {get;set;}
}

IRepository<T> where T :BaseEntity

public IRepository<T> where T :BaseEntity
{
T GetById(object Id);
void Insert(T entity);
void Update(T entity);
void Delete(T entity);
IQueryable<T> Table{get;}
// other common operations ...
}

EFRepository<T>

public class EFRepository<T>:IRepository<T> where T: BaseEntity
{
DbContext _db;
public EFRepository(DbContext db)
{
_db = db;
}
// implement the operations below. GetById, Insert, Update.....
}

IUnitOfwork

public interface IUnitOfWork
{
IRepository<Student> StudentRepository {get;set}
// add other repositories below. //commit
void Save(); Task SaveAsync();
}

UnitOfWork

public class UnitOfWork:IUnitOfWork
{
DbContext _db;
public UnitOfWork(
DbContext db,
IRepository<Student> studentRepository){
StudentRepository =studentRepository;
_db =db;
}
public IRepository<Student> StudentRepository {get;set}
// add other repositories below. //commit
public void Save()
{
_db.SaveChanges();
} public async Task SaveAsync()
{
await _db.SaveChangesAsync();
}
}

ISudentService, UI 业务操作的接口和实现

public interface IStudentService
{
void Create(Student stud);
} public class StudentService:IStudentService
{
IUnitOfWork _uof;
public StudentService(IUnitOfWork uof)
{
_uof = uof;
}
pubilc void Create(Student stud)
{
_uof.StudentRepository.Insert(stud);
_uof.Save();
}
}

http://files.cnblogs.com/files/sgciviolence/RightManager.zip

EF Unit Of Work的更多相关文章

  1. 关于EF Unit of Work Repository的简单用法

    其实ef本身就是unit of work+repository的 其中继承自DbContext的类就是unit of work context中的DbSet<T>属性就是repositor ...

  2. MASA Framework - 整体设计思路

    源起 年初我们在找一款框架,希望它有如下几个特点: 学习成本低 只需要学.Net每年主推的技术栈和业务特性必须支持的中间件,给开发同学减负,只需要专注业务就好 个人见解:一款好用的框架应该是补充,而不 ...

  3. %E3%80%90%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B%E3%80%91

    "%3Cdiv%20class%3D%22htmledit_views%22%20id%3D%22content_views%22%3E%0A%20%20%20%20%20%20%20%20 ...

  4. 使用xUnit,EF,Effort和ABP进行单元测试(C#)

    返回总目录<一步一步使用ABP框架搭建正式项目系列教程> 本篇目录 介绍 创建测试项目 准备测试基类 创建第一个测试 测试异常 在测试中使用仓储 测试异步方法 小结 介绍 在这篇博客中,我 ...

  5. 1.【使用EF Code-First方式和Fluent API来探讨EF中的关系】

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/relationship-in-entity-framework-using-code-firs ...

  6. 2.EF中 Code-First 方式的数据库迁移

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/code-first-migrations-with-entity-framework/ 系列目 ...

  7. 3.EF 6.0 Code-First实现增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-entity-framework-5-0-code- ...

  8. Code First开发系列实战之使用EF搭建小型博客平台

    返回<8天掌握EF的Code First开发>总目录 本篇目录 理解应用需求 数据库设计 创建实体数据模型 创建实体类 创建关系和导航属性 实现DbContext类 执行数据访问 理解仓储 ...

  9. ABP使用及框架解析系列 - [Unit of Work part.1-概念及使用]

    前言 ABP ABP是“ASP.NET Boilerplate Project”的简称. ABP的官方网站:http://www.aspnetboilerplate.com ABP在Github上的开 ...

随机推荐

  1. IntelliJ Idea设置默认换行符

    IntelliJ换行CRLF, LF, CR的解释和默认设置 在window下开发有一个大坑,就是换行默认是CRLF,也就是回车换行,但是Linux下只有换行LF,这样代码提交后,会出现编译问题,所以 ...

  2. 查看Oracle正在执行的任务

    select a.program, b.spid, c.sql_text,c.SQL_ID from v$session a, v$process b, v$sqlarea c where a.pad ...

  3. 在iOS9中 xcode7 网络请求 如图片请求不显示等

    Application Transport Security has blocked a cleartext HTTP (http://) resource load since it is inse ...

  4. docker openvswitch网络方案

    1. 测试环境 75机(10.11.150.75):Red Hat Enterprise Linux Server 7.0,无外网访问权限,已安装Docker Server 74机(10.11.150 ...

  5. php自定义函数求取平方根

    <?phpfunction sqare($a, $left, $right){ $mid = ($left + $right)/2; if($mid * $mid == $a || (abs($ ...

  6. snmp协议接口

    所有网络设备上都会支持smap,获取服务器的基本信息,这样就不用在客户端上装应用就可以检测到基本的信息,是基于socket开发 内存调用这些命令来提取服务器的信息 snmpgetlocalhost - ...

  7. NIC Bonding: 2 nic port as 1 interface

    The following is concluded from here. Consider we have 2 interfaces: eth0 & eth1 bond the two in ...

  8. Latex问题收集

    1. Latex中的空格 两个quad空格 a \qquad b 两个m的宽度 quad空格 a \quad b 一个m的宽度 大空格 a\ b 1/3m宽度 中等空格 a\;b 2/7m宽度 小空格 ...

  9. mysql 用户权限设置

    windows使用的zip包的mysql.配置成功后,root用户没有权限,需要进行权限分配.管理员运行cmd,执行以下命令,进入mysql. mysql -u root -p Passwd Pass ...

  10. gerrit的merge conflict

    找了很多资料,最后参考http://blog.csdn.net/w_jewelry/article/details/8123639 解决的. 先把gerrit的那几个commit abandon掉. ...