Skip to content

Commit

Permalink
docs: add DI documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
alirezanet committed Sep 20, 2023
1 parent ff4e0de commit 7e7dd9d
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/.vuepress/configs/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const sidebar: SidebarConfig = {
{
text: 'Advanced',
children: [
'/guide/dependency-injection.md',
'/guide/compile.md',
'/guide/entity-framework.md',
'/guide/autoMapper.md',
Expand Down
107 changes: 107 additions & 0 deletions docs/guide/dependency-injection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Dependency Injection

Gridify offers a powerful feature that enables you to streamline data mapping and configurations in your application by integrating with the Dependency Injection (DI) container. By registering your mapping profiles with DI, you can achieve cleaner, more maintainable code and improved separation of concerns. This section provides an overview of how to register your GridifyMapper configurations with DI.

## Register GridifyMapper with DI

Registering Gridify mapping with DI is a straightforward process. You'll define mapping profiles for your models and then register them in the DI container. Follow these steps to get started:

### 1. Define Mapping Profiles

Create mapping profiles by inheriting from `GridifyMapper<T>`, where `T` represents the type you want to map. Configure your mappings within these profile classes.

Example:

``` csharp
public class WeatherForecastGridifyMapper : GridifyMapper<WeatherForecast>
{
public WeatherForecastGridifyMapper()
{
// Define your mappings here
AddMap("summary", q => q.Summary);
AddMap("temp", q => q.TemperatureC);

// optionally you can customize the configuration for each mapper
Configuration.CaseSensitive = false;
Configuration.AllowNullSearch = true;
Configuration.IgnoreNotMappedFields = true;

}
}
```

### 2. Register Mapping Profiles

Utilize the `AddGridifyMappers` extension method available on the IServiceCollection to scan your assembly and register all mapping profiles.

Example:

``` csharp
using Gridify; // Make sure to include the necessary namespace
// ...
public void ConfigureServices(IServiceCollection services)
{
// Other service registrations
services.AddGridifyMappers(typeof(Program).Assembly);
}
```

### 3. Inject and Use Mappers

Once you've registered the mapping profiles, you can inject the corresponding `IGridifyMapper<T>` interfaces into your services or controllers.

Example:

:::: code-group
::: code-group-item Extensions
``` csharp
public class WeatherForecastController : ControllerBase
{
private readonly IGridifyMapper<WeatherForecast> _mapper;

public WeatherForecastController(IGridifyMapper<WeatherForecast> mapper)
{
_mapper = mapper;
}

[HttpGet(Name = "GetWeatherForecast")]
public Paging<WeatherForecast> Get([FromQuery] GridifyQuery query)
{
IQueryable<WeatherForecast> result = GetWeatherForecasts();

// You can pass the mapper to the GridifyExtension
return result.Gridify(query, _mapper);
}
}
```
:::

::: code-group-item QueryBuilder
``` csharp
public class WeatherForecastController : ControllerBase
{
private readonly IGridifyMapper<WeatherForecast> _mapper;

public WeatherForecastController(IGridifyMapper<WeatherForecast> mapper)
{
_mapper = mapper;
}

[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get([FromQuery] GridifyQuery query)
{
var result = GetWeatherForecasts();

var queryBuilder = new QueryBuilder<WeatherForecast>()
.UseCustomMapper(_mapper)
.AddQuery(query);

return queryBuilder.Build(result);
}
}
```
:::


0 comments on commit 7e7dd9d

Please sign in to comment.