Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace FashionNova.Web.Infrastructure
- {
- using System.Linq;
- using Fashionista.Application;
- using FashionNova.Services.Common;
- using FluentValidation.AspNetCore;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.DependencyInjection;
- public static class ServiceCollectionExtensions
- {
- public static IServiceCollection AddConventionalService(
- this IServiceCollection services)
- {
- var serviceInterfaceType = typeof(IService);
- var singletonServiceInterfaceType = typeof(ISingletonService);
- var scopedServiceInterfaceType = typeof(IScopedService);
- var types = serviceInterfaceType
- .Assembly
- .GetExportedTypes()
- .Where(s => s.IsClass && !s.IsAbstract)
- .Select(s => new
- {
- Service = s.GetInterface($"I{s.Name}"),
- Implementation = s,
- })
- .Where(s => s.Service != null);
- foreach (var type in types)
- {
- if (serviceInterfaceType.IsAssignableFrom(type.Service))
- {
- services.AddTransient(type.Service, type.Implementation);
- }
- else if (singletonServiceInterfaceType.IsAssignableFrom(type.Service))
- {
- services.AddSingleton(type.Service, type.Implementation);
- }
- else if (scopedServiceInterfaceType.IsAssignableFrom(type.Service))
- {
- services.AddScoped(type.Service, type.Implementation);
- }
- }
- return services;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement