From f34e4f16abf4c87bd868d0e1486b39ba638b9b75 Mon Sep 17 00:00:00 2001 From: Reza Noei Date: Mon, 6 May 2024 14:29:57 +0330 Subject: [PATCH] Initial Commit. --- BSN.Commons.sln | 11 ++++- .../BSN.Commons.AutoMapper.csproj | 19 +++++++++ .../CommonsMappingProfile.cs | 40 +++++++++++++++++++ .../IServiceCollectionExtensions.cs | 27 +++++++++++++ 4 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 Source/BSN.Commons.AutoMapper/BSN.Commons.AutoMapper.csproj create mode 100644 Source/BSN.Commons.AutoMapper/CommonsMappingProfile.cs create mode 100644 Source/BSN.Commons.AutoMapper/Extensions/IServiceCollectionExtensions.cs diff --git a/BSN.Commons.sln b/BSN.Commons.sln index dffa831..5cbd6a2 100644 --- a/BSN.Commons.sln +++ b/BSN.Commons.sln @@ -43,9 +43,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BSN.Commons.Orm.EntityFrame EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BSN.Commons.Users", "Source\BSN.Commons.Users\BSN.Commons.Users.csproj", "{213ABCEF-7E9A-4CE5-A3EF-289C9781344D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BSN.Commons.Orm.Redis", "Source\BSN.Commons.Orm.Redis\BSN.Commons.Orm.Redis.csproj", "{1A1586E8-46EB-43AC-91EC-F6EDCA5689A9}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BSN.Commons.Orm.Redis", "Source\BSN.Commons.Orm.Redis\BSN.Commons.Orm.Redis.csproj", "{1A1586E8-46EB-43AC-91EC-F6EDCA5689A9}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BSN.Commons.Orm.Redis.Tests", "Test\BSN.Commons.Orm.Redis.Tests\BSN.Commons.Orm.Redis.Tests.csproj", "{2D1DB295-5181-48D7-8EC0-1147ED2DAD4A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BSN.Commons.Orm.Redis.Tests", "Test\BSN.Commons.Orm.Redis.Tests\BSN.Commons.Orm.Redis.Tests.csproj", "{2D1DB295-5181-48D7-8EC0-1147ED2DAD4A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BSN.Commons.AutoMapper", "Source\BSN.Commons.AutoMapper\BSN.Commons.AutoMapper.csproj", "{279E7016-E2E9-430E-82A3-C9037FE0E08E}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -93,6 +95,10 @@ Global {2D1DB295-5181-48D7-8EC0-1147ED2DAD4A}.Debug|Any CPU.Build.0 = Debug|Any CPU {2D1DB295-5181-48D7-8EC0-1147ED2DAD4A}.Release|Any CPU.ActiveCfg = Release|Any CPU {2D1DB295-5181-48D7-8EC0-1147ED2DAD4A}.Release|Any CPU.Build.0 = Release|Any CPU + {279E7016-E2E9-430E-82A3-C9037FE0E08E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {279E7016-E2E9-430E-82A3-C9037FE0E08E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {279E7016-E2E9-430E-82A3-C9037FE0E08E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {279E7016-E2E9-430E-82A3-C9037FE0E08E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -108,6 +114,7 @@ Global {213ABCEF-7E9A-4CE5-A3EF-289C9781344D} = {DC377ADC-CC9D-4785-81BE-726DBF5F3096} {1A1586E8-46EB-43AC-91EC-F6EDCA5689A9} = {DC377ADC-CC9D-4785-81BE-726DBF5F3096} {2D1DB295-5181-48D7-8EC0-1147ED2DAD4A} = {5C6BA7B5-832A-495A-AF5E-C2A74F6A1EF9} + {279E7016-E2E9-430E-82A3-C9037FE0E08E} = {DC377ADC-CC9D-4785-81BE-726DBF5F3096} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {BCAF76D3-AA3C-4D0F-8D10-34065F8FED09} diff --git a/Source/BSN.Commons.AutoMapper/BSN.Commons.AutoMapper.csproj b/Source/BSN.Commons.AutoMapper/BSN.Commons.AutoMapper.csproj new file mode 100644 index 0000000..67be444 --- /dev/null +++ b/Source/BSN.Commons.AutoMapper/BSN.Commons.AutoMapper.csproj @@ -0,0 +1,19 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + diff --git a/Source/BSN.Commons.AutoMapper/CommonsMappingProfile.cs b/Source/BSN.Commons.AutoMapper/CommonsMappingProfile.cs new file mode 100644 index 0000000..09209bd --- /dev/null +++ b/Source/BSN.Commons.AutoMapper/CommonsMappingProfile.cs @@ -0,0 +1,40 @@ +using AutoMapper; +using BSN.Commons.Responses; + +namespace BSN.Commons.AutoMapper +{ + public class CommonMapperProfile : Profile + { + public CommonMapperProfile() + { + CreateMap(typeof(PagedEntityCollection<>), typeof(PaginationMetadata)).ConvertUsing(typeof(PagedEntityCollectionToMetaDataConverter<>)); + + CreateMap(typeof(IEnumerable<>), typeof(CollectionViewModel<>)).ConvertUsing(typeof(GenericIEnumerableToCollectionViewModelConverter<,>)); + } + + private class PagedEntityCollectionToMetaDataConverter : ITypeConverter, PaginationMetadata> + { + public PaginationMetadata Convert(PagedEntityCollection source, PaginationMetadata destination, ResolutionContext context) + { + return new PaginationMetadata() + { + Page = source.CurrentPage, + PageCount = source.PageSize, + PageSize = source.PageSize, + RecordCount = source.RecordCount + }; + } + } + + private class GenericIEnumerableToCollectionViewModelConverter : ITypeConverter, CollectionViewModel> + { + public CollectionViewModel Convert(IEnumerable source, CollectionViewModel destination, ResolutionContext context) + { + return new CollectionViewModel + { + Items = context.Mapper.Map>(source) + }; + } + } + } +} diff --git a/Source/BSN.Commons.AutoMapper/Extensions/IServiceCollectionExtensions.cs b/Source/BSN.Commons.AutoMapper/Extensions/IServiceCollectionExtensions.cs new file mode 100644 index 0000000..b70d44c --- /dev/null +++ b/Source/BSN.Commons.AutoMapper/Extensions/IServiceCollectionExtensions.cs @@ -0,0 +1,27 @@ +using AutoMapper; +using Microsoft.Extensions.DependencyInjection; + +namespace BSN.Commons.AutoMapper.Extensions +{ + public static class IServiceCollectionExtensions + { + public static IServiceCollection AddAutoMapper(this IServiceCollection services, Action configure) + { + var mappingConfig = new MapperConfiguration(config => + { + MapperConfigurationExpression mapperConfigurationExpression = new MapperConfigurationExpression(); + configure(mapperConfigurationExpression); + + configure(config); + + config.AddProfile(new CommonMapperProfile()); + }); + + IMapper mapper = mappingConfig.CreateMapper(); + + services.AddSingleton(mapper); + + return services; + } + } +}