The service base of EF I am using
using CapMon.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Data.Entity;
using System.Linq.Expressions;
using CapMon.Utilities; namespace CapMon.Business.Base
{
public class ServiceBase<TObject> where TObject : class
{
protected CapMonEntities _context; public ServiceBase()
{
_context = new CapMonEntities();
} /// <summary>
/// The contructor requires an open DataContext to work with
/// </summary>
/// <param name="context">An open DataContext</param>
public ServiceBase(CapMonEntities context)
{
_context = context;
}
/// <summary>
/// Returns a single object with a primary key of the provided id
/// </summary>
/// <remarks>Synchronous</remarks>
/// <param name="id">The primary key of the object to fetch</param>
/// <returns>A single object with the provided primary key or null</returns>
public TObject Get(int id)
{
return _context.Set<TObject>().Find(id);
}
/// <summary>
/// Returns a single object with a primary key of the provided id
/// </summary>
/// <remarks>Asynchronous</remarks>
/// <param name="id">The primary key of the object to fetch</param>
/// <returns>A single object with the provided primary key or null</returns>
public async Task<TObject> GetAsync(int id)
{
return await _context.Set<TObject>().FindAsync(id);
}
/// <summary>
/// Gets a collection of all objects in the database
/// </summary>
/// <remarks>Synchronous</remarks>
/// <returns>An ICollection of every object in the database</returns>
public ICollection<TObject> GetAll()
{
return _context.Set<TObject>().ToList();
}
/// <summary>
/// Gets a collection of all objects in the database
/// </summary>
/// <remarks>Asynchronous</remarks>
/// <returns>An ICollection of every object in the database</returns>
public async Task<ICollection<TObject>> GetAllAsync()
{
return await _context.Set<TObject>().ToListAsync();
}
/// <summary>
/// Returns a single object which matches the provided expression
/// </summary>
/// <remarks>Synchronous</remarks>
/// <param name="match">A Linq expression filter to find a single result</param>
/// <returns>A single object which matches the expression filter.
/// If more than one object is found or if zero are found, null is returned</returns>
public TObject Find(Expression<Func<TObject, bool>> match)
{
return _context.Set<TObject>().SingleOrDefault(match);
}
/// <summary>
/// Returns a single object which matches the provided expression
/// </summary>
/// <remarks>Asynchronous</remarks>
/// <param name="match">A Linq expression filter to find a single result</param>
/// <returns>A single object which matches the expression filter.
/// If more than one object is found or if zero are found, null is returned</returns>
public async Task<TObject> FindAsync(Expression<Func<TObject, bool>> match)
{
return await _context.Set<TObject>().SingleOrDefaultAsync(match);
}
/// <summary>
/// Returns a collection of objects which match the provided expression
/// </summary>
/// <remarks>Synchronous</remarks>
/// <param name="match">A linq expression filter to find one or more results</param>
/// <returns>An ICollection of object which match the expression filter</returns>
public ICollection<TObject> FindAll(Expression<Func<TObject, bool>> match)
{
return _context.Set<TObject>().Where(match).ToList();
}
/// <summary>
/// Returns a collection of objects which match the provided expression
/// </summary>
/// <remarks>Asynchronous</remarks>
/// <param name="match">A linq expression filter to find one or more results</param>
/// <returns>An ICollection of object which match the expression filter</returns>
public async Task<ICollection<TObject>> FindAllAsync(Expression<Func<TObject, bool>> match)
{
return await _context.Set<TObject>().Where(match).ToListAsync();
}
/// <summary>
/// Inserts a single object to the database and commits the change
/// </summary>
/// <remarks>Synchronous</remarks>
/// <param name="t">The object to insert</param>
/// <returns>The resulting object including its primary key after the insert</returns>
public TObject Add(TObject t)
{
_context.Set<TObject>().Add(t);
_context.SaveChanges();
return t;
}
/// <summary>
/// Inserts a single object to the database and commits the change
/// </summary>
/// <remarks>Asynchronous</remarks>
/// <param name="t">The object to insert</param>
/// <returns>The resulting object including its primary key after the insert</returns>
public async Task<TObject> AddAsync(TObject t)
{
_context.Set<TObject>().Add(t);
await _context.SaveChangesAsync();
return t;
}
/// <summary>
/// Inserts a collection of objects into the database and commits the changes
/// </summary>
/// <remarks>Synchronous</remarks>
/// <param name="tList">An IEnumerable list of objects to insert</param>
/// <returns>The IEnumerable resulting list of inserted objects including the primary keys</returns>
public IEnumerable<TObject> AddAll(IEnumerable<TObject> tList)
{
_context.Set<TObject>().AddRange(tList);
_context.SaveChanges();
return tList;
}
/// <summary>
/// Inserts a collection of objects into the database and commits the changes
/// </summary>
/// <remarks>Asynchronous</remarks>
/// <param name="tList">An IEnumerable list of objects to insert</param>
/// <returns>The IEnumerable resulting list of inserted objects including the primary keys</returns>
public async Task<IEnumerable<TObject>> AddAllAsync(IEnumerable<TObject> tList)
{
_context.Set<TObject>().AddRange(tList);
await _context.SaveChangesAsync();
return tList;
}
/// <summary>
/// Updates a single object based on the provided primary key and commits the change
/// </summary>
/// <remarks>Synchronous</remarks>
/// <param name="updated">The updated object to apply to the database</param>
/// <param name="key">The primary key of the object to update</param>
/// <returns>The resulting updated object</returns>
public TObject Update(TObject updated, int key)
{
if (updated == null)
return null; TObject existing = _context.Set<TObject>().Find(key);
if (existing != null)
{
_context.Entry(existing).CurrentValues.SetValues(updated);
_context.SaveChanges();
}
return existing;
}
/// <summary>
/// Updates a single object based on the provided primary key and commits the change
/// </summary>
/// <remarks>Asynchronous</remarks>
/// <param name="updated">The updated object to apply to the database</param>
/// <param name="key">The primary key of the object to update</param>
/// <returns>The resulting updated object</returns>
public async Task<TObject> UpdateAsync(TObject updated, int key)
{
if (updated == null)
return null; TObject existing = await _context.Set<TObject>().FindAsync(key);
if (existing != null)
{
_context.Entry(existing).CurrentValues.SetValues(updated);
await _context.SaveChangesAsync();
}
return existing;
}
/// <summary>
/// Deletes a single object from the database and commits the change
/// </summary>
/// <remarks>Synchronous</remarks>
/// <param name="t">The object to delete</param>
public void Delete(TObject t)
{
_context.Set<TObject>().Remove(t);
_context.SaveChanges();
}
/// <summary>
/// Deletes a single object from the database and commits the change
/// </summary>
/// <remarks>Asynchronous</remarks>
/// <param name="t">The object to delete</param>
public async Task<int> DeleteAsync(TObject t)
{
_context.Set<TObject>().Remove(t);
return await _context.SaveChangesAsync();
} /// <summary>
/// Gets the count of the number of objects in the databse
/// </summary>
/// <remarks>Synchronous</remarks>
/// <returns>The count of the number of objects</returns>
public int Count()
{
return _context.Set<TObject>().Count();
}
/// <summary>
/// Gets the count of the number of objects in the databse
/// </summary>
/// <remarks>Asynchronous</remarks>
/// <returns>The count of the number of objects</returns>
public async Task<int> CountAsync()
{
return await _context.Set<TObject>().CountAsync();
}
}
}
The service base of EF I am using的更多相关文章
- 【干货】利用MVC5+EF6搭建博客系统(一)EF Code frist、实现泛型数据仓储以及业务逻辑
习MVC有一段时间了,决定自己写一套Demo了,写完源码再共享. PS:如果图片模糊,鼠标右击复制图片网址,然后在浏览器中打开即可. 一.框架搭建 二.创建数据库 1.创建一个空的EF code fr ...
- 基于EF的一个简单实战型分层架构
注:此博客仅适合于刚入门的Asp.net Core初学者,仅做参考. 学了3个月的Asp.net Core,将之前一个系统(http://caijt.com/it)的PHP后端用Asp.net Cor ...
- 我能不能理解成 ssh中service就相当于与jsp+servlet+dao中的servlet???
转文 首先解释面上意思,service是业务层,dao是数据访问层.(Data Access Objects) 数据访问对象 1.Dao其实一般没有这个类,这一般是指java中MVC架构中的model ...
- 【Java EE 学习 69 下】【数据采集系统第一天】【实体类分析和Base类书写】
之前SSH框架已经搭建完毕,现在进行实体类的分析和Base类的书写.Base类是抽象类,专门用于继承. 一.实体类关系分析 既然是数据采集系统,首先调查实体(Survey)是一定要有的,一个调查有多个 ...
- ssh base 写法
BaseDao package wl.oa.dao.base; public interface BaseDao<T>{ public void saveEntry(T t); } Bas ...
- 项目中Service层的写法
截取自项目中的一个service实现类,记录一下: base类 package com.bupt.auth.service.base; import javax.annotation.Resource ...
- Cloud Foundry中 JasperReports service集成
Cloud Foundry作为业界第一个开源的PaaS解决方案,正越来越多的被业界接受和认可.随着PaaS的发展,Cloud Foundry顺应潮流,充分发挥开源项目的特点,到目前为止,已经支持了大批 ...
- A basic Windows service in C++ (CppWindowsService)
A basic Windows service in C++ (CppWindowsService) This code sample demonstrates creating a basic Wi ...
- Cloud Foundry中通用service的集成
目前,CloudFoundry已经集成了很多第三方的中间件服务,并且提供了用户添加自定义服务的接口.随着Cloud Foundry的发展,开发者势必会将更多的服务集成进Cloud Foundry,以供 ...
随机推荐
- 自动化测试(一)-get和post的简单应用
今天主要介绍两种测试的接口post和get: get和post是http的两种基本请求方式,区别在于get把参数包含在url中传递:给而post把参数以json或键值对的方式利用工具传递. get的传 ...
- 第三十三篇 Python中关于OOP(面向对象)的常用术语
面向对象的优点 从编程进化论可知,面向对象是一种更高等级的结构化编程方式,它的好处主要有两点: 1. 通过封装明确了内外,你做为类的缔造者,你就是女娲,女娲造物的逻辑别人无需知道,女娲想让你知道,你才 ...
- webpack使用时可能出现的问题
1.在配置完webpack.config.js准备进行热加载开发时,修改React内容浏览器不会自动局部刷新,而且会console出一些提示: The following modules couldn ...
- P3950部落冲突
题面 \(Solution:\) 法一:LCT裸题 又好想又好码,只不过常数太大. 法二:树链剖分 每次断边将该边权的值++,连边--,然后边权化点权(给儿子),询问就查询从x到y的路径上的边权和,树 ...
- HDU 4433 locker(DP)(2012 Asia Tianjin Regional Contest)
Problem Description A password locker with N digits, each digit can be rotated to 0-9 circularly.You ...
- Git&GitHub&GitBook
一.定义 Git(分布式版本控制系统) GitHub gitHub是一个面向开源及私有软件项目的托管平台,因为只支持git 作为唯一的版本库格式进行托管,故名gitHub. gitHub于2008年4 ...
- STL应用——hdu1412(set)
set函数的应用 超级水题 #include <iostream> #include <cstdio> #include <algorithm> #include ...
- DFS——hdu5682zxa and leaf
一.题目回顾 题目链接:zxa and leaf Sample Input 2 3 2 1 2 1 3 2 4 3 9 6 2 1 2 1 3 1 4 2 5 2 6 3 6 5 9 Sample ...
- wutianqi 博客 母函数
母函数(Generating function)详解 — Tanky Woo 在数学中,某个序列的母函数(Generating function,又称生成函数)是一种形式幂级数,其每一项的系数可以提供 ...
- 转---详细的Android开发环境搭建教程
五步搞定Android开发环境部署——非常详细的Android开发环境搭建教程 引言 在windows安装Android的开发环境不简单也说不上算复杂,本文写给第一次想在自己Windows上建立A ...