autofac 服务层
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using wgh.model.Models;
using DBHelper;
using IService;
using wgh.Service;
namespace Service
{
/// <summary>
/// 系统用户服务层
/// </summary>
public class SysUserInfoServie: ISysUserInfoServie, IDependency
{
/// <summary>
/// 根据ID查询单个记录
/// </summary>
public SysUserInfo Get(string id)
{
var session = DapperLiteFactory.GetSession();
return session.QueryById<SysUserInfo>(id);
}
#region 查询总数
/// <summary>
/// 根据ID查询单个记录
/// </summary>
public int GetTotalCount()
{
var session = DapperLiteFactory.GetSession();
return session.QuerySingle<int>("select count(*) from sys_user");
}
#endregion
#region 查询集合
/// <summary>
/// 查询集合
/// </summary>
public List<SysUserInfo> GetList()
{
string sql = "";
var session = DapperLiteFactory.GetSession();
return session.QueryList<SysUserInfo>(sql);
}
#endregion
#region 添加
/// <summary>
/// 添加
/// </summary>
public long Insert(SysUserInfo info)
{
var session = DapperLiteFactory.GetSession();
session.OnExecuting = (s, p) => Console.WriteLine(s); //打印SQL
long id = session.InsertReturnId(info,"");
return id;
}
#endregion
#region 添加(异步)
/// <summary>
/// 添加
/// </summary>
public async Task<long> InsertAsync(SysUserInfo info)
{
var session = DapperLiteFactory.GetSession();
session.OnExecuting = (s, p) => Console.WriteLine(s); //打印SQL
long id = await session.InsertReturnIdAsync(info, "select @@IDENTITY");
return id;
}
#endregion
#region 批量添加
/// <summary>
/// 批量添加
/// </summary>
public void Insert(List<SysUserInfo> list)
{
var session = DapperLiteFactory.GetSession();
try
{
session.OnExecuting = (s, p) => Console.WriteLine(s); //打印SQL
session.BeginTransaction();
session.Insert(list);
session.CommitTransaction();
}
catch
{
session.RollbackTransaction();
throw;
}
}
#endregion
#region 批量添加(异步)
/// <summary>
/// 批量添加
/// </summary>
public async Task InsertAsync(List<SysUserInfo> list)
{
var session = DapperLiteFactory.GetSession();
await session.InsertAsync(list);
}
#endregion
#region 修改
/// <summary>
/// 修改
/// </summary>
public void Update(SysUserInfo info)
{
var session = DapperLiteFactory.GetSession();
session.Update(info);
}
#endregion
#region 修改(异步)
/// <summary>
/// 修改
/// </summary>
public async Task UpdateAsync(SysUserInfo info)
{
var session = DapperLiteFactory.GetSession();
session.OnExecuting = (s, p) => Console.WriteLine(s); //打印SQL
var task = session.UpdateAsync(info);
await task;
}
#endregion
#region 批量修改
/// <summary>
/// 批量修改
/// </summary>
public void Update(List<SysUserInfo> list)
{
var session = DapperLiteFactory.GetSession();
try
{
session.OnExecuting = (s, p) => Console.WriteLine(s); //打印SQL
session.BeginTransaction();
session.Update(list);
session.CommitTransaction();
}
catch
{
session.RollbackTransaction();
throw;
}
}
#endregion
#region 批量修改(异步)
/// <summary>
/// 批量修改
/// </summary>
public async Task UpdateAsync(List<SysUserInfo> list)
{
var session = DapperLiteFactory.GetSession();
var task = session.UpdateAsync(list);
await task;
}
#endregion
#region 删除
/// <summary>
/// 删除
/// </summary>
public bool Delete(long id)
{
var session = DapperLiteFactory.GetSession();
session.OnExecuting = (s, p) =>
{
Console.WriteLine(s); //打印SQL
};
return session.DeleteById<SysUserInfo>(id)>0;
}
#endregion
}
}
本文来自博客园,作者:.net&new,转载请注明原文链接:https://www.cnblogs.com/wugh8726254/p/17533422.html