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,以供 ...
随机推荐
- Django笔记 —— 模型高级进阶
最近在学习Django,打算玩玩网页后台方面的东西,因为一直很好奇但却没怎么接触过.Django对我来说是一个全新的内容,思路想来也是全新的,或许并不能写得很明白,所以大家就凑合着看吧- 本篇笔记(其 ...
- Android中通过拨号调起应用的实现方式及特殊情况处理
Android中有时我们会有这样的需求:通过拨号调起我们的程序.这个需求如何实现呢? 思路当然是在我们的应用中实现一个广播接收器(BroadcastReceiver),对打电话时系统发出的广播进行拦截 ...
- 项目总结(一)->项目的七宗罪
大半夜来这一份总结,心中夹杂着各种各样的心情,酸甜苦辣都有,今天为止,整个项目终于完结了,对于这样一个本可以正而八经吃吃薯片,看看毛片就可以完成项目,最后演变成一个一月之内连续加班105个小时的项目, ...
- Ngix配置,让所有请求指向同一个文件
统一入口使所有请求在同一个文件先验证处理,Ngix添加如下代码: location / { try_files '' /index.php; }
- Vm-Ubuntu下配置Qt开发环境
在昨天的Ubuntu换降下,安装Qt发现编译的时候是缺少opengl的 奈何找了好多方式都无法安装opengl 今天看到另一位大神写的,才发下自己找的还是有问题 大神帖子网址:http://blog. ...
- Kotlin操作符重载:把标准操作加入到任何类中(KAD 17)
作者:Antonio Leiva 时间:Mar 21, 2017 原文链接:https://antonioleiva.com/operator-overload-kotlin/ 就像其他每种语言一样, ...
- 接口测试工具postman(一)下载安装说明
Postman是Google开发的一款功能强大的网页调试与发送网页HTTP请求,并能运行测试用例的的Chrome插件. 主要功能包括: 1.模拟各种HTTP requests 从常用的 GET.POS ...
- java.sql.Date java.sql.Time java.sql.Timestamp 之比较
java.sql.Date,java.sql.Time和java.sql.Timestamp 三个都是java.util.Date的子类(包装类). java.sql.Date是java.util.D ...
- result returns more than one elements此种错误,解决
场景:公司产品开发完成后,接入第三方厂商,在进行接口联调的时候出现此问题.此接口报文中的每一个数据都要进行校验,有些是与已经存入产品数据库中的数据进行对比,看是否存在. 问题:在测试中,有些测试没有问 ...
- JAVA集合面面观
List的常用实现:vector,ArrayList,linkedList. 总体关系如下(java8): vector和arraylist 两者底层都是采用数组的形式.但是有些许不同 // Arra ...