-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff4e0de
commit 7e7dd9d
Showing
2 changed files
with
108 additions
and
0 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
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); | ||
} | ||
} | ||
``` | ||
::: | ||
|
||
|