using AutoMapper;
using AutoMapper.Internal;
using Kean.Application.Utilities.Interfaces;
using Kean.Infrastructure.Database;
using Kean.Infrastructure.Database.Repository.Default;
using Pluralize.NET;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading.Tasks;
namespace Kean.Application.Utilities.Implements
{
///
/// 增删改查通用服务实现
///
public class CrudService : ICrudService
{
private readonly IMapper _mapper; // 模型映射
private readonly IDefaultDb _database; // 默认数据库
private readonly IPluralize _pluralizer; // 类型名转换器
private string _typeName; // 类型名
private TypeMap _typeMap; // 类型映射
private object _schema; // 数据库对象
private Type _schemaType; // 数据库对象类型
private MethodInfo _schemaFactory; // 数据库对象工厂
///
/// 依赖注入
///
public CrudService(
IMapper mapper,
IDefaultDb database,
IPluralize pluralizer)
{
_mapper = mapper;
_database = database;
_pluralizer = pluralizer;
}
/*
* 实现 Kean.Application.Utilities.Interfaces.ICrudService.GetCount 方法
*/
public Task GetCount(string type, IDictionary parameters)
{
CreateSchema(type);
if (parameters.Count > 0)
{
InvokeWhere(parameters);
}
return InvokeCount();
}
/*
* 实现 Kean.Application.Utilities.Interfaces.ICrudService.GetList 方法
*/
public Task> GetList(string type, IDictionary parameters, string sort, int? offset, int? limit)
{
CreateSchema(type);
if (parameters.Count > 0)
{
InvokeWhere(parameters);
}
if (!string.IsNullOrEmpty(sort))
{
InvokeOrderBy(sort);
}
if (offset.HasValue)
{
InvokeSkip(offset.Value);
}
if (limit.HasValue)
{
InvokeTake(limit.Value);
}
return InvokeSelect();
}
/*
* 实现 Kean.Application.Utilities.Interfaces.ICrudService.GetItem 方法
*/
public Task