Created
March 28, 2024 11:44
-
-
Save unseensenpai/edbcb657ec2f73a51723a9cc0b9f95e5 to your computer and use it in GitHub Desktop.
Automapper Registration Extension
This file contains hidden or 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
using AutoMapper; | |
using System.Reflection; | |
namespace Microsoft.Extensions.DependencyInjection | |
{ | |
public static class MapperBuilderExtensions | |
{ | |
public static IServiceCollection AddAutoMapperModule<T>(this IServiceCollection services) where T : Profile | |
{ | |
List<Type> profiles = []; | |
List<string> projectDLLs = [.. Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "DLLPREFIXIFEXIST.*.dll", SearchOption.TopDirectoryOnly)]; | |
projectDLLs.ForEach(dll => profiles.AddRange(Assembly.LoadFrom(Path.GetFullPath(dll)).GetTypes().Where(x => x.BaseType == typeof(T)).ToList())); | |
services.AddAutoMapper(profileAssemblyMarkerTypes: [.. profiles]); | |
return services; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment