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. RandomAccessFile类初次使用

    RandomAccessFile : java提供的对文件内容的访问 既可以读文件 也可以写文件 支持随机访问文件 可以访问文件的任意位置 (1)java文件模型 : 在硬盘上的文件是byte byt ...

  2. fedora 使用trove的redstack 安装openstack环境

    以下命令可能是你经常需要用到的: dnf list installed 列出所有dnf安装的包 dnf remove packagename 删除包   先获取trove-integration gi ...

  3. iOS开发系列-UI基础-KVC

    这些知识是UI初级学习的,目前我还在学习中,适合初学者看 KVC—Key Value Coding 也就是键值编码 是一种获取值和设置值的方式 当我们创建一个类文件,为这个类设置成员属性的时候: 创建 ...

  4. hadoop操作

    常用命令: https://hadoop.apache.org/docs/r1.0.4/cn/hdfs_shell.html ls命令 /usr/bin/hadoop/software/hadoop/ ...

  5. mac关机快捷键

    1.Ctrl + 关机:弹出关机提示 2.Ctrl + Option + 关机 : 正常关机快捷键 3.Command + Option + 关机 :进入休眠状态 4.Ctrl + Command + ...

  6. Linux文件挂载命令mount

    在linux系统中硬盘.u盘.光驱等其他设备都需要挂载后才能正常使用.下面是对挂载命令mount使用方法的一些总结. 文件挂载命令mountmount [-t 文件系统类型][-L卷标名][-o特殊选 ...

  7. Python基础篇-day11 - 协程

    本节主要内容: 1.Gevent协程2.Select\Poll\Epoll异步IO与事件驱动3.RabbitMQ队列 1.Gevent协程 1.1协程的好处 无需线程上下文切换的开销无需原子操作锁定及 ...

  8. Android;设置TextView加粗 代码设置

    我用过paint的那种方式,不好使. private void setTextBold(TextView textView) { //android中为textview动态设置字体为粗体 textVi ...

  9. Android:View颤抖的动画效果代码

    public void propertyValuesHolderDown(final View view) { PropertyValuesHolder pvhX = PropertyValuesHo ...

  10. [转]查看Android源码版本

    有时候我们辛苦取到Android的源代码,想知道它的确切版本号,比如有时候我们只粗略知道拿到的是2.3的源码,但并不明确他的小版本号,这就是有时候明明都是2.3的代码可比较起来还是有差异的原因,比方说 ...