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. js生成缩略图后上传并利用canvas重绘

    function drawCanvasImage(obj,width, callback){ var $canvas = $('<canvas></canvas>'), can ...

  2. SpannableString可以被点击的文字

    1 TextView tv= (TextView) findViewById(R.id.textview_z); String text="一段可以被点击点击的文字,文字可以变成图片&quo ...

  3. DBCP数据源的使用

    DBCP(DataBase Connection Pool)是一个开源的数据源工具,实际开发直接使用就行了 导入需要的jar包,数据库使用mysql测试

  4. Pelican主题配置:elegant

    简介 elegant是Mac风格的优秀主题,简单,专注文章本身. A responsive, minimal, and stylish theme for Pelican:https://github ...

  5. 安装VMWare WorkStation 10 异常【 Failed to create the requested registry keyKey:Installer Error: 1021】

    下载了新的workstation 10,在安装的时候出现了异常Failed to create the requested registry keyKey:Installer Error: 1021. ...

  6. 压测软件-Tsung.安装篇

    author :James,jimingsong@vip.qq.com author :James,jimingsong@vip.qq.com since :2015-03-02 tsung介绍 ts ...

  7. UltraISO PE(软碟通) V9.5.5.2960 官方中文版

    软件名称: UltraISO PE(软碟通)软件语言: 简体中文授权方式: 免费试用运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 1.9MB图片预览: 软件简介 ...

  8. [帖子收集]通用Windows平台(UWP)

    通用Windows平台,universal windows platform,UWP 什么是通用 Windows 平台 (UWP) 应用?(微软MSDN) 如何在通用 Windows 平台应用中使用现 ...

  9. Python基础(七)-文件操作

    一.文件处理流程 1.打开文件,得到文件句柄赋值给一个变量 2.通过句柄对文件进行操作 3.关闭文件 二.基本操作 f = open('zhuoge.txt') #打开文件 first_line = ...

  10. ie6的png24问题

    解决IE6的PNG透明JS插件 DD_belatedPNG 引:http://www.cnblogs.com/cobby/archive/2012/05/11/2495801.html IE6的PNG ...