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,以供 ...
随机推荐
- react children技巧总结
在使用该技巧时,建议先看一下相关的知识,点我查看 假如使用该属性时,想把父组件的所有属性及部分方法传递给子组件,该怎么办呢?看代码 const Child = ({ doSomething, valu ...
- Python-类-函数参数-takes 0 positional arguments but 1 was given
在学习Python基础的时候,在创建某一个shownametest()函数,解析器会报错 TypeError: shownametest() takes 0 positional arguments ...
- 自动化测试元素查找利器firepath介绍
自动化测试查找元素和确定元素xpath路径是否正确在业界有个很好的工具就是firefox 浏览器的 firepath 问题: firefox 最新版本已经不支持firebug和firepath这两个插 ...
- C计算了一下
#include <stdio.h> int main(){ int a,b,c,e; a=6 + 5 / 4 - 2; b=2 + 2 * (2 * 2 - 2) % 2 / 3; c= ...
- tensorflow nmt基本配置(tf-1.4)
随着tensorflow的不断更新,直接按照nmt的教程搭建nmt环境会报错的...因此,需要一些不太好的办法来避免更多的问题出现.tensorflow看来在ubuntu和debian中运行是没有问题 ...
- Kindle 3(非常旧的版本) 隔一段时间自动重启问题
买了本新书后,kindle 3 自己没事就在那边重启,几分钟一次 查到解决方案1: https://answers.yahoo.com/question/index?qid=2014040815565 ...
- DFS——CodeForces740DAlyona and a tree
一.题目回顾 题目链接:Alyona and a tree Examples Input 52 5 1 4 61 71 13 53 6 Output 1 0 1 0 0 Input 59 7 8 ...
- java文件的I/O
[原创] java文件的I/O操作,简单来说就是向文件中写入数据以及从文件中读出数据,这是我们平日做的最多的操作,这里给出两种文件I/O操作,当然还有许多的操作方法,各种流的使用,可谓是高深莫测:不管 ...
- 算法(9)Find the Duplicate Number
一个数组中的长度是n+1,里面存放的数字大小的范围是[1,n],根据鸽巢原理,所以里面肯定有重复的数字,现在预定重复的数字就1个,让你找到这个数字! http://bookshadow.com/web ...
- [剑指Offer] 19.顺时针打印矩阵
[思路]本题关键在于 右->左 和 下->上 两个循环体中的判断条件,即判断是否重复打印. class Solution { public: vector<int> print ...