38 lines
1.2 KiB
C#
38 lines
1.2 KiB
C#
using Microsoft.Extensions.DependencyInjection;
|
|
using Raven.Client.Documents;
|
|
using System;
|
|
using zero.Core.Options;
|
|
|
|
namespace zero.Core.Mapper
|
|
{
|
|
public static class MapperServiceCollectionExtensions
|
|
{
|
|
public static IServiceCollection AddMapper<T>(this IServiceCollection services) where T : class, IMapper
|
|
{
|
|
return services.AddSingleton<IMapper, T>();
|
|
}
|
|
|
|
public static IServiceCollection AddMapper<T>(this IServiceCollection services, Action<T> options) where T : class, IMapper, new()
|
|
{
|
|
return services.AddSingleton<IMapper, T>(factory =>
|
|
{
|
|
T mapper = new T();
|
|
options(mapper);
|
|
return mapper;
|
|
});
|
|
}
|
|
|
|
public static IServiceCollection AddMapper(this IServiceCollection services) => services.AddMapper<DefaultMapper>();
|
|
|
|
public static IServiceCollection AddMapper(this IServiceCollection services, Action<DefaultMapper> options)
|
|
{
|
|
return services.AddSingleton<IMapper, DefaultMapper>(factory =>
|
|
{
|
|
DefaultMapper mapper = new DefaultMapper(factory.GetService<IDocumentStore>(), factory.GetService<IZeroOptions>());
|
|
options(mapper);
|
|
return mapper;
|
|
});
|
|
}
|
|
}
|
|
}
|