Skip to content

Instantly share code, notes, and snippets.

@unseensenpai
Created March 28, 2024 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unseensenpai/edbcb657ec2f73a51723a9cc0b9f95e5 to your computer and use it in GitHub Desktop.
Save unseensenpai/edbcb657ec2f73a51723a9cc0b9f95e5 to your computer and use it in GitHub Desktop.
Automapper Registration Extension
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