diff --git a/docs/.vuepress/configs/sidebar.ts b/docs/.vuepress/configs/sidebar.ts index 418b25fc..0708d95d 100644 --- a/docs/.vuepress/configs/sidebar.ts +++ b/docs/.vuepress/configs/sidebar.ts @@ -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', diff --git a/docs/guide/dependency-injection.md b/docs/guide/dependency-injection.md new file mode 100644 index 00000000..13ecdd92 --- /dev/null +++ b/docs/guide/dependency-injection.md @@ -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`, where `T` represents the type you want to map. Configure your mappings within these profile classes. + +Example: + +``` csharp +public class WeatherForecastGridifyMapper : GridifyMapper +{ + 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` interfaces into your services or controllers. + +Example: + +:::: code-group +::: code-group-item Extensions + ``` csharp +public class WeatherForecastController : ControllerBase +{ + private readonly IGridifyMapper _mapper; + + public WeatherForecastController(IGridifyMapper mapper) + { + _mapper = mapper; + } + + [HttpGet(Name = "GetWeatherForecast")] + public Paging Get([FromQuery] GridifyQuery query) + { + IQueryable 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 _mapper; + + public WeatherForecastController(IGridifyMapper mapper) + { + _mapper = mapper; + } + + [HttpGet(Name = "GetWeatherForecast")] + public IEnumerable Get([FromQuery] GridifyQuery query) + { + var result = GetWeatherForecasts(); + + var queryBuilder = new QueryBuilder() + .UseCustomMapper(_mapper) + .AddQuery(query); + + return queryBuilder.Build(result); + } +} +``` +::: + +