Files
mixtape/zero.Core/ServiceCollectionExtensions.cs
T

71 lines
2.6 KiB
C#
Raw Normal View History

2020-05-22 21:19:49 +02:00
using FluentValidation;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using System.Reflection;
using zero.Core.Api;
2020-05-25 16:15:33 +02:00
using zero.Core.Renderer;
namespace zero.Core.Entities
{
public static class ServiceCollectionExtensions
{
public static void AddZeroCoreServices(this IServiceCollection services)
{
2020-05-25 16:15:33 +02:00
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
services.AddAllByInterfaceTransient(typeof(IValidator), typeof(IValidator<>), assemblies, true);
services.AddAllByInterfaceTransient(typeof(IRenderer), typeof(IRenderer<>), assemblies);
2020-05-22 21:19:49 +02:00
services.AddTransient<IApplication, Application>();
2020-05-24 18:23:56 +02:00
services.AddTransient<ICountry, Country>();
2020-05-24 18:32:47 +02:00
services.AddTransient<ILanguage, Language>();
2020-05-24 18:40:30 +02:00
services.AddTransient<ITranslation, Translation>();
2020-05-25 11:27:23 +02:00
services.AddTransient<IPage, Page>();
services.AddTransient(typeof(IApplicationsApi<>), typeof(ApplicationsApi<>));
services.AddTransient(typeof(ICountriesApi<>), typeof(CountriesApi<>));
services.AddTransient(typeof(ILanguagesApi<>), typeof(LanguagesApi<>));
2020-05-24 20:52:09 +02:00
services.AddTransient(typeof(ITranslationsApi), typeof(TranslationsApi));
2020-05-24 18:40:30 +02:00
services.AddTransient(typeof(ITranslationsApi<>), typeof(TranslationsApi<>));
2020-05-24 19:05:59 +02:00
services.AddTransient(typeof(ITranslationsApiFacade), typeof(TranslationsApiFacade));
2020-05-25 11:27:23 +02:00
services.AddTransient(typeof(IPagesApi<>), typeof(PagesApi<>));
services.AddTransient(typeof(IPageTreeApi<>), typeof(PageTreeApi<>));
}
2020-05-22 21:19:49 +02:00
public static void AddAllByInterfaceTransient<TService, TImplementation>(this IServiceCollection services, Assembly[] assemblies)
{
services.AddAllByInterfaceTransient(typeof(TService), typeof(TImplementation), assemblies);
}
2020-05-25 16:15:33 +02:00
public static void AddAllByInterfaceTransient(this IServiceCollection services, Type serviceType, Type implementingType, Assembly[] assemblies, bool transient = false)
2020-05-22 21:19:49 +02:00
{
foreach (Assembly assembly in assemblies)
{
foreach (Type type in assembly.GetExportedTypes())
{
2020-05-25 16:15:33 +02:00
if (serviceType.IsAssignableFrom(type) && type.IsClass && !type.IsAbstract && !type.FullName.StartsWith("Fluent") && type.Name != "AbstractGenericRenderer")
2020-05-22 21:19:49 +02:00
{
Type service = type.GetInterfaces().FirstOrDefault(x => x.IsInterface && x.Name == implementingType.Name);
if (service != null)
{
2020-05-25 16:15:33 +02:00
if (transient)
{
services.AddTransient(service, type);
}
else
{
services.AddScoped(service, type);
}
2020-05-22 21:19:49 +02:00
}
}
}
}
}
}
}