Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/mapster #17210

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions framework/Volo.Abp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Imaging.MagickNet.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Imaging.AspNetCore.Tests", "test\Volo.Abp.Imaging.AspNetCore.Tests\Volo.Abp.Imaging.AspNetCore.Tests.csproj", "{983B0136-384B-4439-B374-31111FFAA286}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Mapster", "src\Volo.Abp.Mapster\Volo.Abp.Mapster.csproj", "{8642CA09-671F-44C2-9100-09B04BD2F8E6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1363,6 +1365,10 @@ Global
{983B0136-384B-4439-B374-31111FFAA286}.Debug|Any CPU.Build.0 = Debug|Any CPU
{983B0136-384B-4439-B374-31111FFAA286}.Release|Any CPU.ActiveCfg = Release|Any CPU
{983B0136-384B-4439-B374-31111FFAA286}.Release|Any CPU.Build.0 = Release|Any CPU
{8642CA09-671F-44C2-9100-09B04BD2F8E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8642CA09-671F-44C2-9100-09B04BD2F8E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8642CA09-671F-44C2-9100-09B04BD2F8E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8642CA09-671F-44C2-9100-09B04BD2F8E6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1593,6 +1599,7 @@ Global
{1E161A34-10C1-46FA-9EFD-10DD0858A8F5} = {447C8A77-E5F0-4538-8687-7383196D04EA}
{62B2B8C9-8F24-4D31-894F-C1F0728D32AB} = {447C8A77-E5F0-4538-8687-7383196D04EA}
{983B0136-384B-4439-B374-31111FFAA286} = {447C8A77-E5F0-4538-8687-7383196D04EA}
{8642CA09-671F-44C2-9100-09B04BD2F8E6} = {5DF0E140-0513-4D0D-BE2E-3D4D85CD70E6}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BB97ECF4-9A84-433F-A80B-2A3285BDD1D5}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.Extensions.DependencyInjection.Extensions;
using Volo.Abp.Mapster;
using Volo.Abp.ObjectMapping;

namespace Microsoft.Extensions.DependencyInjection;

public static class AbpMapsterServiceCollectionExtensions
{
public static IServiceCollection AddMapsterObjectMapper(this IServiceCollection services)
{
return services.Replace(ServiceDescriptor
.Transient<IAutoObjectMappingProvider, MapsterAutoObjectMappingProvider>());
}

public static IServiceCollection AddMapsterObjectMapper<TContext>(this IServiceCollection services)
{
return services.Replace(
ServiceDescriptor.Transient<IAutoObjectMappingProvider<TContext>, MapsterAutoObjectMappingProvider<TContext>>()
);
}
}
25 changes: 25 additions & 0 deletions framework/src/Volo.Abp.Mapster/Volo.Abp.Mapster.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Nullable>enable</Nullable>
<WarningsAsErrors>Nullable</WarningsAsErrors>
<AssemblyName>Volo.Abp.Mapster</AssemblyName>
<PackageId>Volo.Abp.Mapster</PackageId>
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<RootNamespace />
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Mapster" Version="7.3.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Volo.Abp.ObjectMapping\Volo.Abp.ObjectMapping.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity;
using Volo.Abp.ObjectMapping;

namespace Volo.Abp.Mapster;

[DependsOn(
typeof(AbpObjectMappingModule)
)]
public class AbpMapsterModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddMapsterObjectMapper();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Mapster;
using Volo.Abp.ObjectMapping;

namespace Volo.Abp.Mapster;

public class MapsterAutoObjectMappingProvider : IAutoObjectMappingProvider
{
public TDestination Map<TSource, TDestination>(object source)
{
return source.Adapt<TDestination>();
}

public TDestination Map<TSource, TDestination>(TSource source, TDestination destination)
{
return source.Adapt(destination);
}
}

public class MapsterAutoObjectMappingProvider<TContext> : MapsterAutoObjectMappingProvider,
IAutoObjectMappingProvider<TContext>
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.DependencyInjection;
using Volo.Abp.ObjectMapping;

namespace Volo.Abp.Mapster;

public class MapsterObjectMapper : IObjectMapper, ITransientDependency
{
public MapsterObjectMapper(IServiceProvider serviceProvider,
IAutoObjectMappingProvider autoObjectMappingProvider)
{
ServiceProvider = serviceProvider;
AutoObjectMappingProvider = autoObjectMappingProvider;
}

private IServiceProvider ServiceProvider { get; }

public IAutoObjectMappingProvider AutoObjectMappingProvider { get; }

public TDestination Map<TSource, TDestination>(TSource source)
{
if (source is null)
{
return default!;
}

var result = TryMapWithSpecificMapper<TSource, TDestination>(source);
return result ?? AutoObjectMappingProvider.Map<TSource, TDestination>(source);
}

public TDestination Map<TSource, TDestination>(TSource source, TDestination destination)
{
if (source is null)
{
return default!;
}

var result = TryMapWithSpecificMapper<TSource, TDestination>(source, destination);
return result ?? AutoObjectMappingProvider.Map<TSource, TDestination>(source, destination);
}

private TDestination? TryMapWithSpecificMapper<TSource, TDestination>(TSource source, TDestination destination)
{
var mapper = FindSpecificMapper<TSource, TDestination>();
return mapper is null ? default : mapper.Map(source, destination);
}

private TDestination? TryMapWithSpecificMapper<TSource, TDestination>(TSource source)
{
var mapper = FindSpecificMapper<TSource, TDestination>();
return mapper is null ? default : mapper.Map(source);
}

private IObjectMapper<TSource, TDestination>? FindSpecificMapper<TSource, TDestination>()
{
using var scope = ServiceProvider.CreateScope();
return scope.ServiceProvider.GetService<IObjectMapper<TSource, TDestination>>();
}
}