EF Unit Of Work
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的更多相关文章
- 关于EF Unit of Work Repository的简单用法
其实ef本身就是unit of work+repository的 其中继承自DbContext的类就是unit of work context中的DbSet<T>属性就是repositor ...
- MASA Framework - 整体设计思路
源起 年初我们在找一款框架,希望它有如下几个特点: 学习成本低 只需要学.Net每年主推的技术栈和业务特性必须支持的中间件,给开发同学减负,只需要专注业务就好 个人见解:一款好用的框架应该是补充,而不 ...
- %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 ...
- 使用xUnit,EF,Effort和ABP进行单元测试(C#)
返回总目录<一步一步使用ABP框架搭建正式项目系列教程> 本篇目录 介绍 创建测试项目 准备测试基类 创建第一个测试 测试异常 在测试中使用仓储 测试异步方法 小结 介绍 在这篇博客中,我 ...
- 1.【使用EF Code-First方式和Fluent API来探讨EF中的关系】
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/relationship-in-entity-framework-using-code-firs ...
- 2.EF中 Code-First 方式的数据库迁移
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/code-first-migrations-with-entity-framework/ 系列目 ...
- 3.EF 6.0 Code-First实现增删查改
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-entity-framework-5-0-code- ...
- Code First开发系列实战之使用EF搭建小型博客平台
返回<8天掌握EF的Code First开发>总目录 本篇目录 理解应用需求 数据库设计 创建实体数据模型 创建实体类 创建关系和导航属性 实现DbContext类 执行数据访问 理解仓储 ...
- ABP使用及框架解析系列 - [Unit of Work part.1-概念及使用]
前言 ABP ABP是“ASP.NET Boilerplate Project”的简称. ABP的官方网站:http://www.aspnetboilerplate.com ABP在Github上的开 ...
随机推荐
- js-数据转换
<script type="text/javascript"> var msg = '{"code": 0, "data": 2 ...
- jquery点击目标DIV以外关闭效果
$(function(){ $(".cover").hide(); $("#call").click(function(){ console.log(" ...
- js作用域详解
// 作用域:(1)域:空间.范围.区域…… (2) 作用:读.写 script 全局变量.全局函数 自上而下 函数 由里到外 浏览器: “JS解析器” 1)“找一些东西” :var func ...
- Redis--Latest Windows Version
Redis Windows Ver https://github.com/MSOpenTech/redis/releases service-install.batredis-server.exe - ...
- uiscrollview 事件冲突
self.scrollView.panGestureRecognizer.delaysTouchesBegan = YES;设置scrollView的延迟接收点击触摸事件,关闭触摸事件 self.sc ...
- Introduction to Programming Contests (stanford)
http://web.stanford.edu/class/cs9http://web.stanford.edu/class/cs97si/7si/
- C#发送邮件类
一.定义邮件发送类 /// <summary> /// 发送邮件 /// </summary> public class MailHelper { #region 私有变量 p ...
- 学习自动化工具gulp
<什么是gulp>官网地址:http://gulpjs.com/ gulp是可以自动化执行任务的工具,在开发流程里,一定有一些动作需要手工的重复的去执行,例如: ·把一个文件拷贝到另外一个 ...
- C#中string.Substring 的用法
String.SubString(int startIndex,int length) startIndex:截取字符串开始的位置 length:截取字符串的长度 例子:用户 输 ...
- ip地址分类和网段区分
IP地址分类/IP地址10开头和172开头和192开头的区别/判断是否同一网段 简单来说在公司或企业内部看到的就基本都是内网IP,ABC三类IP地址里的常见IP段. 每个IP地址都包含两部分,即网络号 ...