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. C#删除只读文件或文件夹(解决File.Delete无法删除文件)

    引用: http://www.jb51.net/article/72181.htm   C#删除只读文件的方法: if (File.GetAttributes(FFName).ToString().I ...

  2. kali客户端攻击

    浏览器攻击 browser_autpwn2 (BAP2) mkdir /test 为接受响应的服务器创建目录   use auxiliary/server/browser_autopwn2  set ...

  3. PAT乙级1004. 成绩排名 (20)

    读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每个测试输入包含1个测试用例,格式为 第1行:正整数n 第2行:第1个学生的姓名 学号 成绩 第3行:第2个学生 ...

  4. Scala AOP

    trait Action { def doAction } trait TBeforeAfter extends Action { abstract override def doAction { p ...

  5. ios 显示其他app的购买页面

    using UnityEngine; using System.Collections; using System.Runtime.InteropServices ; public class IOS ...

  6. 【实验室笔记】C#以本地时间创建txt文件

    前段时间做的一个小项目,要求上位机在打开时候,以打开软件的系统时间的建立一个txt文件来存储下位机发送来的数据. 在第一版上位机上,取名的办法太弱了,先是读取系统时间,然后截取字符串,太笨拙.昨天,查 ...

  7. 解决Windows内存问题的两个小工具RamMap和VMMap(这个更牛更好)

    来源:http://www.cr173.com/html/13006_1.html .net程序内存监测分配工具(CLR Profiler for .NET Framework 4)官方安装版 类型: ...

  8. Linux 网络性能tuning向导

    本文的目的不完全在于提供调优信息,而是在于告诉读者了解Linux kernel如何处理数据包,从而能够在 自己的实践中发挥Linux 内核协议栈最大的性能 The NIC ring buffer 接收 ...

  9. 如何在IIS8.5上面部署php

    一.开启,设置win8.1自带的IIS 8.5组件服务器. 进入控制面板,选择程序和功能,打开或关闭Windows 功能,找到Internet information services,分别开启FTP ...

  10. directive(指令里的)的compile,pre-link,post-link,link,transclude

    The nitty-gritty of compile and link functions inside AngularJS directives  The nitty-gritty of comp ...