简化对象映射Mapper,简化自定义仓储

This commit is contained in:
橙子
2023-02-06 23:08:12 +08:00
parent 1d7c17e253
commit fdd1eda9ec
37 changed files with 64 additions and 523 deletions

View File

@@ -1,34 +0,0 @@

using AutoMapper;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Core.Helper;
namespace Yi.Framework.Autofac.Extensions
{
public static class AutoMapperExtensions
{
public static IServiceCollection AddAutoMapperService(this IServiceCollection services)
{
//这里会通过反射,扫码全部程序集获取继承Profile的类
var assemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
var profileList = new List<Type>();
assemblies.ForEach(a =>
{
if (a.FullName is not null)
{
profileList.AddRange(AssemblyHelper.GetClassByParentClass(a.FullName, typeof(Profile)));
}
});
services.AddAutoMapper(profileList.ToArray());
return services;
}
}
}

View File

@@ -6,12 +6,16 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Yi.Framework.Core\Yi.Framework.Core.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Extensions\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Mapster" Version="7.3.0" />
</ItemGroup>
</Project>

View File

@@ -1,7 +1,7 @@
using Microsoft.AspNetCore.Builder;
using MapsterMapper;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using StartupModules;
using Yi.Framework.Autofac.Extensions;
using Yi.Framework.Core.Attributes;
namespace Yi.Framework.Core.AutoMapper
@@ -9,7 +9,7 @@ namespace Yi.Framework.Core.AutoMapper
[DependsOn(
typeof(YiFrameworkCoreModule)
)]
public class YiFrameworkCoreAutoMapperModule : IStartupModule
public class YiFrameworkCoreMapsterModule : IStartupModule
{
public void Configure(IApplicationBuilder app, ConfigureMiddlewareContext context)
@@ -20,7 +20,7 @@ namespace Yi.Framework.Core.AutoMapper
{
//添加全局自动mapper
services.AddAutoMapperService();
services.AddSingleton<IMapper, Mapper>();
}
}
}

View File

@@ -22,7 +22,10 @@ namespace Yi.Framework.Core.Sqlsugar.Repositories
public SqlsugarRepository(ISqlSugarClient context) : base(context)
{
}
protected ISugarQueryable<T> _DbQueryable { get { return base.AsQueryable(); } set { } }
/// <summary>
/// 注释一下严格意义这里应该protected但是我认为 简易程度 与 耦合程度 中是需要进行衡量的
/// </summary>
public ISugarQueryable<T> _DbQueryable => base.AsQueryable();
protected ISqlSugarClient _Db { get { return Context; } set { } }

View File

@@ -1,4 +1,5 @@
using System;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
@@ -10,8 +11,12 @@ using Yi.Framework.Ddd.Entities;
namespace Yi.Framework.Ddd.Repositories
{
public interface IRepository<T>
public interface IRepository<T>
{
/// <summary>
/// 注释一下严格意义这里应该protected但是我认为 简易程度 与 耦合程度 中是需要进行衡量的
/// </summary>
ISugarQueryable<T> _DbQueryable { get; }
//单查
Task<T> GetByIdAsync(dynamic id);
Task<T> GetSingleAsync(Expression<Func<T, bool>> whereExpression);

View File

@@ -1,4 +1,4 @@
using AutoMapper;
using MapsterMapper;
using Microsoft.Extensions.DependencyInjection;
using Yi.Framework.Core.Model;

View File

@@ -1,4 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -39,6 +40,8 @@ where TEntityDto : IEntityDto<TKey>
/// </summary>
protected IRepository<TEntity> _repository { get => ServiceLocatorModel.Instance.GetRequiredService<IRepository<TEntity>>(); }
protected ISugarQueryable<TEntity> _DbQueryable => _repository._DbQueryable;
//Mapper
protected virtual Task<TGetOutputDto> MapToGetOutputDtoAsync(TEntity entity)
{

View File

@@ -10,7 +10,7 @@
<ItemGroup>
<ProjectReference Include="..\Yi.Framework.Core.Autofac\Yi.Framework.Core.Autofac.csproj" />
<ProjectReference Include="..\Yi.Framework.Core.AutoMapper\Yi.Framework.Core.AutoMapper.csproj" />
<ProjectReference Include="..\Yi.Framework.Core.AutoMapper\Yi.Framework.Core.Mapster.csproj" />
<ProjectReference Include="..\Yi.Framework.Core\Yi.Framework.Core.csproj" />
</ItemGroup>

View File

@@ -12,7 +12,7 @@ using Yi.Framework.Core.AutoMapper;
namespace Yi.Framework.Ddd
{
[DependsOn(
typeof(YiFrameworkCoreAutoMapperModule)
typeof(YiFrameworkCoreMapsterModule)
)]
public class YiFrameworkDddModule:IStartupModule
{

View File

@@ -4,6 +4,11 @@
<name>Yi.Framework.Ddd</name>
</assembly>
<members>
<member name="P:Yi.Framework.Ddd.Repositories.IRepository`1._DbQueryable">
<summary>
注释一下严格意义这里应该protected但是我认为 简易程度 与 耦合程度 中是需要进行衡量的
</summary>
</member>
<member name="M:Yi.Framework.Ddd.Services.CrudAppService`7.CreateAsync(`5)">
<summary>