-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--------- Co-authored-by: mostafa fbh <mostafafbh@yahoo.com>
- Loading branch information
Showing
11 changed files
with
384 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
Source/BSN.Commons.AutoMapper/BSN.Commons.AutoMapper.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net8.0</TargetFrameworks> | ||
<AssemblyVersion>1.15.0</AssemblyVersion> | ||
<FileVersion>1.15.0</FileVersion> | ||
<Authors>BSN Developers</Authors> | ||
<Company>BSN Company</Company> | ||
<Description>AutoMapper Helpers for using AutoMapper in enterprise application.</Description> | ||
<Copyright>BSN Co 2019-2024</Copyright> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<PackageProjectUrl>https://github.com/BSVN/Commons</PackageProjectUrl> | ||
<RepositoryUrl>https://github.com/BSVN/Commons.git</RepositoryUrl> | ||
<RepositoryType>git</RepositoryType> | ||
<PackageReleaseNotes>Please see CHANGELOG.md</PackageReleaseNotes> | ||
<Version>1.15.0</Version> | ||
<ProduceReferenceAssembly>True</ProduceReferenceAssembly> | ||
<GenerateDocumentationFile>True</GenerateDocumentationFile> | ||
<Title>BSN.Commons.AutoMapper</Title> | ||
<PackageReadmeFile>README.md</PackageReadmeFile> | ||
<IncludeSymbols>True</IncludeSymbols> | ||
<SymbolPackageFormat>snupkg</SymbolPackageFormat> | ||
<PackageIcon>BSN.jpg</PackageIcon> | ||
<PackageTags>BSN;Commons;AutoMapper</PackageTags> | ||
<!-- Optional: Publish the repository URL in the built .nupkg (in the NuSpec <Repository> element) --> | ||
<PublishRepositoryUrl>true</PublishRepositoryUrl> | ||
<!-- Optional: Embed source files that are not tracked by the source control manager in the PDB --> | ||
<EmbedUntrackedSources>true</EmbedUntrackedSources> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AutoMapper" Version="13.0.1" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\BSN.Commons.PresentationInfrastructure\BSN.Commons.PresentationInfrastructure.csproj" /> | ||
<ProjectReference Include="..\BSN.Commons\BSN.Commons.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Changelog | ||
|
||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), | ||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). | ||
|
||
## [1.15.0] - 2024-06-29 | ||
Fixed issue with mapping between domain and view models. | ||
|
||
### Added | ||
- README.md and CHANGELOG.md | ||
- Added support for AutoMapper 13.0.1 | ||
- Added predefined conversions for mapping `BSN.Commons.PresentationInfrastructure` Models. | ||
|
||
### Fixed | ||
- Resolve some missing elements in nupkg. | ||
|
||
### Changed | ||
- Update documentation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<TDomain> : ITypeConverter<PagedEntityCollection<TDomain>, PaginationMetadata> | ||
{ | ||
public PaginationMetadata Convert(PagedEntityCollection<TDomain> source, PaginationMetadata destination, ResolutionContext context) | ||
{ | ||
return new PaginationMetadata() | ||
{ | ||
Page = source.CurrentPage, | ||
PageCount = source.PageSize, | ||
PageSize = source.PageSize, | ||
RecordCount = source.RecordCount | ||
}; | ||
} | ||
} | ||
|
||
private class GenericIEnumerableToCollectionViewModelConverter<TDomain, TViewModel> : ITypeConverter<IEnumerable<TDomain>, CollectionViewModel<TViewModel>> | ||
{ | ||
public CollectionViewModel<TViewModel> Convert(IEnumerable<TDomain> source, CollectionViewModel<TViewModel> destination, ResolutionContext context) | ||
{ | ||
return new CollectionViewModel<TViewModel> | ||
{ | ||
Items = context.Mapper.Map<IEnumerable<TViewModel>>(source) | ||
}; | ||
} | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
Source/BSN.Commons.AutoMapper/Extensions/IServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<IMapperConfigurationExpression> 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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# BSN.Commons.AutoMapper | ||
|
||
This package contains some facilities for using AutoMapper in Enterprise Applications. | ||
|
||
AutoMapper is a popular library for mapping objects from one Model to Another. It simplifies the process of mapping complex objects and reduces the amount of code needed to perform these mappings stuffs. | ||
|
||
**BSN.Commons.Automapper** is a package that provides some predefined mappings for **BSN.Commons.PresentationInfrastructure** Models. | ||
This helps **BSN.Commons** users to skip writing required mapping to dealing with these Models. | ||
|
||
### 1. Installation | ||
To use this package, you need to first install it on your web api or presentation layer. You can do this by running the following command (in package manager console): | ||
``` | ||
Install-Package BSN.Commons.AutoMapper | ||
``` | ||
|
||
### 2. Add Your required mapping profiles | ||
To use these predefined mapping profiles and injecting your preferred profiles you just need to add following line in the `ServiceCollection`: | ||
``` | ||
services.AddAutoMapper(config => config.AddProfile<YourMappingProfile>()); | ||
``` | ||
or | ||
``` | ||
services.AddAutoMapper(config => | ||
{ | ||
config.AddProfile<YourFirstMappingProfile>(); | ||
config.AddProfile<YourSecondMappingProfile>(); | ||
}); | ||
``` | ||
|
||
### 3. Predefined mapping profile: | ||
|
||
Provided built in mapping profile contains following mappings: | ||
|
||
#### PagedEntityCollectionToMetaDataConverter: | ||
A default converter which converts `PagedEntityCollection<TDomain>` to `PaginationMetadata`. | ||
#### GenericIEnumerableToCollectionViewModelConverter: | ||
A generic default converter which converts `IEnumerable<TDomain>` to `CollectionViewModel<TViewModel>` | ||
|
||
### 4. Example Usage | ||
|
||
#### PagedEntityCollectionToMetaDataConverter | ||
``` | ||
var pagedEntities = new PagedEntityCollection<Product>(products, 1, 10, 100); | ||
var paginationMetadata = mapper.Map<PaginationMetadata>(pagedEntities); | ||
``` | ||
|
||
#### GenericIEnumerableToCollectionViewModelConverter | ||
``` | ||
var products = new List<Product> | ||
{ | ||
new Product { Id = 1, Name = "Product 1" }, | ||
new Product { Id = 2, Name = "Product 2" }, | ||
new Product { Id = 3, Name = "Product 3" } | ||
}; | ||
var collectionViewModel = mapper.Map<CollectionViewModel<ProductViewModel>>(products); | ||
``` | ||
|
||
`BSN.Commons.AutoMapper` is Copyright © 2024 BSN and other contributors under the BSN license. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using AutoMapper; | ||
|
||
namespace BSN.Commons.AutoMapper.Tests | ||
{ | ||
public abstract class AutoMapperTestBase | ||
{ | ||
protected readonly IMapper _mapper; | ||
|
||
protected AutoMapperTestBase() | ||
{ | ||
var configuration = new MapperConfiguration(cfg => | ||
{ | ||
cfg.AddProfile<CommonMapperProfile>(); | ||
}); | ||
|
||
_mapper = configuration.CreateMapper(); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
Test/BSN.Commons.AutoMapper.Tests/BSN.Commons.AutoMapper.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AutoMapper" Version="13.0.1" /> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="Moq" Version="4.20.70" /> | ||
<PackageReference Include="NUnit" Version="3.14.0" /> | ||
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" /> | ||
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\Source\BSN.Commons.AutoMapper\BSN.Commons.AutoMapper.csproj" /> | ||
<ProjectReference Include="..\..\Source\BSN.Commons.PresentationInfrastructure\BSN.Commons.PresentationInfrastructure.csproj" /> | ||
<ProjectReference Include="..\..\Source\BSN.Commons\BSN.Commons.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="NUnit.Framework" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.