Skip to content

Commit

Permalink
Update README.md Service Collection Documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jassus213 committed Sep 8, 2023
1 parent b27da11 commit 373435f
Showing 1 changed file with 58 additions and 17 deletions.
75 changes: 58 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# EntityMapper Documentation
![Static Badge](https://img.shields.io/badge/latest_version-2.2.1-blue) ![Static Badge](https://img.shields.io/badge/license-MIT-green)
![Static Badge](https://img.shields.io/badge/latest_version-2.2.2-blue) ![Static Badge](https://img.shields.io/badge/license-MIT-green)
## Introducion
EntityMapper is a powerful and flexible object-to-object mapping library for .NET applications. It simplifies the process of mapping one object's properties to another, allowing you to focus on writing clean and maintainable code.
This documentation provides a detailed guide on how to use EntityMapper in your .NET projects. It covers the basic setup, configuration, mapping, and integration with the `ServiceCollection`.

## Installation
## <a id="installcli"/> Using .NET CLI
## Using .NET CLI
To install EntityMapper using the .NET CLI, open your command-line interface and navigate to your project's directory.
Run the following command to add the EntityMapper package to your project:
```c#
dotnet add package Entity.Mapper.Asp --version 1.0.0
```
## <a id="installnugetpackage"/> Using NuGet Package Manager Console
## Using NuGet Package Manager Console
If you prefer to use Visual Studio and its integrated development environment, you can install EntityMapper using the NuGet Package Manager Console.
Open Visual Studio.
Go To `"Tools" > "NuGet Package Manager" > "Package Manager Console."`
Expand All @@ -20,7 +20,7 @@ In the Package Manager Console, run the following command to install EntityMappe
NuGet\Install-Package Entity.Mapper.Asp -Version 1.0.0
```
This command will install the latest version of EntityMapper into your project.
## <a id="installmsbuild"/> Using PackageReference (MSBuild)
## Using PackageReference (MSBuild)
In some cases, you may want to integrate EntityMapper into your project using MSBuild. This method is useful when you need more control over how EntityMapper is incorporated into your build process.
Open your project file (e.g., .csproj) in a text editor or Visual Studio or Rider By F4.
Add a PackageReference element for EntityMapper in the ItemGroup section:
Expand Down Expand Up @@ -52,7 +52,7 @@ EntityMapper traditionally uses a global configuration to define how objects of

To address this issue, EntityMapper introduced Disposable Mapper Configurations. This feature allows you to define and configure a mapping for a specific use case, and once that mapping is no longer needed, it is automatically disposed of. This can be particularly beneficial for scenarios where you want to minimize memory consumption, such as handling infrequent or specialized mappings.
```c#
entityMapper.AddDisposableMapperConfiguration<User, UserDto>((user) => new UserDto()
entityMapper.AddDisposableConfiguration<User, UserDto>((user) => new UserDto()
{
Id = user.Id,
Name = user.Name
Expand All @@ -62,7 +62,7 @@ In this code snippet, we define a mapping from the User class to the UserDto cla
## Asynchronous Configuration
EntityMapper provides the capability to define asynchronous mapping functions, allowing you to perform asynchronous operations during the mapping process. This is achieved by using asynchronous delegates (lambda expressions) in the `AddAsyncMapperConfiguration` method. Let's look at an example of using asynchronous configuration:
```c#
entityMapper.AddAsyncMapperConfiguration<User, UserDto>(async (user) =>
entityMapper.AddAsyncConfiguration<User, UserDto>(async (user) =>
{
// Perform an asynchronous operation (e.g., delay)
await Task.Delay(500);
Expand All @@ -86,7 +86,7 @@ var user = new User()
Name = "Test Name"
};

entityMapper.AddABidirectionalMapperConfiguration<User, UserDto>((user) => new UserDto()
entityMapper.AddBidirectionalMapping<User, UserDto>((user) => new UserDto()
{
Id = user.Id,
Name = user.Name
Expand All @@ -109,18 +109,59 @@ var userDto = EntityMapper.Map<User, UserDto>(user);
```
In this example, we map a User object to a UserDto object, and EntityMapper takes care of copying the properties according to the configuration.

## ServiceCollection Integration
EntityMapper can be integrated with the ServiceCollection in ASP.NET Core applications to enable dependency injection of mappers. Here's how to set it up:
## Extending ServiceCollection with EntityMapper Configuration
In many .NET applications, configuring object mapping using a custom mapper, like entityMapper, is a common task. This custom mapper simplifies the process of mapping objects from one type to another, reducing boilerplate code. To make this process even more convenient and flexible, we can extend the ServiceCollection in ASP.NET Core applications with a custom extension method, UseMapper(). This method provides access to a configurator object that allows the addition of various mapping configurations. In this chapter, we'll explore how to use this extension and the four types of mapping configurations it supports.
### Using `UseMapper()` Extension
To streamline the configuration of entity mappings within an ASP.NET Core application, we can extend the ServiceCollection class with the UseMapper() extension method. This method returns an IMapperConfigurator object, which enables us to add different types of mapping configurations.

Here's how to use the UseMapper() method:
```c#
var serviceCollection = new ServiceCollection();
var entityMapper = serviceCollection.UseMapper();
entityMapper.AddMapperConfiguration<User, UserDto>((user) => new UserDto()
{
Id = user.Id,
Name = user.Name
});
services.UseMapper();
```
This line of code adds the entity mapper to the DI container and returns the IMapperConfigurator interface, which we can use to configure mappings.

## Adding Mapping Configurations
### 1. Regular Mapping Configuration
The `AddMapperConfiguration<TSource, TDestination>()` method allows you to add a standard mapping configuration. This configuration is suitable for most mapping scenarios where you want to define how properties of one type map to another.
```c#
services.UseMapper()
.AddMapperConfiguration<User, UserDto>((user) => new UserDto()
{
Id = user.Id,
Name = user.Name
});
```
### 2. Disposable Mapping Configuration
The `AddDisposableMapperConfiguration<TSource, TDestination>()` method adds a mapping configuration that is automatically removed from the list of configurations after its initial use. This is useful for scenarios where you want to optimize memory usage by disposing of configurations that are only needed temporarily.
```c#
services.UseMapper()
.AddDisposableMapperConfiguration<User, UserDto>((user) => new UserDto()
{
Id = user.Id,
Name = user.Name
});
```
### 3. Async Mapping Configuration
For scenarios where asynchronous mapping is required, the `AddAsyncMapperConfiguration<TSource, TDestination>()` method allows you to specify an asynchronous mapping configuration.
```c#
services.UseMapper()
.AddAsyncMapperConfiguration<User, UserDto>(async (user) =>
{
// Perform asynchronous mapping logic here
return await SomeAsyncMethod(user);
});
```
This code configures EntityMapper to work with the ServiceCollection. It allows you to inject the mapper into your services, making it easy to use EntityMapper within your application's services and controllers.
### 4. Bidirectional Mapping Configuration
The `AddABidirectionalMapperConfiguration<TSource, TDestination>()` method is a special method that adds both the specified configuration and its reverse (Map Back) configuration. This is useful when you need to map objects bidirectionally.
```c#
services.UseMapper()
.AddABidirectionalMapperConfiguration<User, UserDto>((user) => new UserDto()
{
Id = user.Id,
Name = user.Name
});
```

## Conclusion
EntityMapper is a powerful tool for simplifying object-to-object mapping in your .NET applications. By following this documentation, you can quickly set up and use EntityMapper to streamline your code and improve maintainability. It's worth noting that EntityMapper is extensively tested, with a test suite that achieves
`100% code coverage` to ensure its reliability and correctness in various scenarios. This comprehensive testing ensures that EntityMapper works seamlessly in your projects, providing confidence in its behavior and robustness.

0 comments on commit 373435f

Please sign in to comment.