Skip to content

Commit

Permalink
edit readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa fbh committed May 23, 2024
1 parent 9d932b4 commit 306e65a
Showing 1 changed file with 51 additions and 21 deletions.
72 changes: 51 additions & 21 deletions Source/BSN.Commons.AutoMapper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppServiceViewMapperProfile>());
```

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.
### 3.How to use in your service?
in your application code, execute the mappings:
```
var productDto = mapper.Map<ProductDto>(product);
var orderDto = mapper.Map<OrderDto>(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<TDomain> to PaginationMetadata.
- #### GenericIEnumerableToCollectionViewModelConverter:
A generic custom type converter that converts IEnumerable<TDomain> to CollectionViewModel<TViewModel>

The PagedEntityCollectionToMetaDataConverter is a custom type converter that converts PagedEntityCollection<TDomain> 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<Product>(products, 1, 10, 100);
var paginationMetadata = mapper.Map<PaginationMetadata>(pagedEntities);
```

The GenericIEnumerableToCollectionViewModelConverter is a generic custom type converter that converts IEnumerable<TDomain> to CollectionViewModel<TViewModel>. 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<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);
```

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.

0 comments on commit 306e65a

Please sign in to comment.