- 数据访问层新建接口文件夹,创建IBaseRepository类
- 添加Repository文件夹,创建BaseRepository类
- 写业务层的
- 配置注入,在UI层创建文件夹Dependency,创建类AutofacConfig.cs
5. 在Global.asax注入
6. 控制器调用业务层
复制代码
- IBaseRepository.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
namespace CY.UnifiedAuthority.DAL.IRepository
{
public interface IBaseRepository<T> where T : class
{
#region 添加数据
bool Add(T entity);
bool AddRange(List<T> entities);
#endregion
#region 修改数据
bool Update(T entity);
#endregion
#region 删除数据
bool Delete(T entity);
bool BatchDelete(List<T> entities);
bool BatchDelete(params int[] ids);
#endregion
#region 获取数据
List<T> QueryList(Expression<Func<T, bool>> whereLambda);
T Query(Expression<Func<T, bool>> whereLambda);
bool Exists(Expression<Func<T, bool>> whereLambda);
List<T> QueryPageList<S>(int pageIndex, int pageSize, Expression<Func<T, bool>> whereLambda, Expression<Func<T, S>> orderbyLambda, out int total, bool isAsc);
#endregion
}
}
- BaseRepository.cs
using CY.UnifiedAuthority.DAL.IRepository;
using CY.UnifiedAuthority.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
namespace CY.UnifiedAuthority.DAL.Repository
{
public class BaseRepository<T>: IBaseRepository<T>