From 306e65a4e8de004f9316dbb725af89f346520987 Mon Sep 17 00:00:00 2001 From: mostafa fbh Date: Thu, 23 May 2024 08:09:53 +0330 Subject: [PATCH] edit readme. --- Source/BSN.Commons.AutoMapper/README.md | 72 +++++++++++++++++-------- 1 file changed, 51 insertions(+), 21 deletions(-) diff --git a/Source/BSN.Commons.AutoMapper/README.md b/Source/BSN.Commons.AutoMapper/README.md index 9b4490e..d9cb623 100644 --- a/Source/BSN.Commons.AutoMapper/README.md +++ b/Source/BSN.Commons.AutoMapper/README.md @@ -2,32 +2,62 @@ ITNOA -This package contains some facilities for using AutoMapper in Enterprise Way +This package contains some facilities for using AutoMapper in Enterprise way AutoMapper is a popular library for mapping objects from one type to another. It simplifies the process of mapping complex objects and reduces the amount of code needed to perform these mappings. -To use this package, you need to first install it in your client service. You can do this by running the following command: +### 1. Install required Package +To use this package, you need to first add it in your client service. You can do this by running the following command: +``` - Install-Package BSN.Commons.AutoMapper +``` +### 2. Add required services Once you've installed the package, you need to add the following code to your Startup.cs file: +``` - services.AddAutoMapper(config => config.AddProfile()); +``` -1.AppServiceViewMapperProfile: This profile maps objects related to App Services, including: -- AppService to AppServiceViewModel -- AppServiceDto to AppServiceViewModel -- AppServiceViewModel to AppService -- AppServiceViewModel to AppServiceDto - -2.UserViewMapperProfile: This profile maps objects related to Users, including: -- User to UserViewModel -- UserDto to UserViewModel -- UserViewModel to User -- UserViewModel to UserDto - -3.RoleViewMapperProfile: This profile maps objects related to Roles, including: -- Role to RoleViewModel -- RoleDto to RoleViewModel -- RoleViewModel to Role -- RoleViewModel to RoleDto - -These profiles cover some of the most common use cases for mapping objects in enterprise applications. However, if you need to add support for additional complex types, you can create your own profile and add it to the configuration in the Startup.cs file. \ No newline at end of file +### 3.How to use in your service? +in your application code, execute the mappings: +``` +var productDto = mapper.Map(product); +var orderDto = mapper.Map(order); +``` + +### 4.Where CommonsMappingProfile for example is: + +The CommonMapperProfile class is a profile for AutoMapper that defines custom type conversions. It contains the following: + +- #### PagedEntityCollectionToMetaDataConverter: +A custom type converter that converts PagedEntityCollection to PaginationMetadata. +- #### GenericIEnumerableToCollectionViewModelConverter: +A generic custom type converter that converts IEnumerable to CollectionViewModel + +The PagedEntityCollectionToMetaDataConverter is a custom type converter that converts PagedEntityCollection to PaginationMetadata. It has a generic type parameter TDomain that represents the type of the domain entities in the PagedEntityCollection +Here's an example of how to use this custom type converter: + +#### PagedEntityCollectionToMetaDataConverter +``` +var pagedEntities = new PagedEntityCollection(products, 1, 10, 100); +var paginationMetadata = mapper.Map(pagedEntities); +``` + +The GenericIEnumerableToCollectionViewModelConverter is a generic custom type converter that converts IEnumerable to CollectionViewModel. It has two generic type parameters: TDomain represents the type of the domain entities in the IEnumerable, and TViewModel represents the type of the view models that will be created from the domain entities. +Here's an example of how to use this custom type converter: + +#### GenericIEnumerableToCollectionViewModelConverter +``` +var products = new List +{ + 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>(products); +``` + +These profiles cover some of the most common use cases for mapping objects in enterprise applications. However, if you need to add support for additional complex types, you can create your own profile and add it to the configuration in the Startup.cs file. + +BSN.Commons.AutoMapper is Copyright © 2024 Bsn and other contributors under the Bsn license. \ No newline at end of file