diff --git a/AutobusApi.Application/Common/Mappings/IMapFrom.cs b/AutobusApi.Application/Common/Mappings/IMapFrom.cs new file mode 100644 index 0000000..c79ab81 --- /dev/null +++ b/AutobusApi.Application/Common/Mappings/IMapFrom.cs @@ -0,0 +1,8 @@ +using AutoMapper; + +namespace AutobusApi.Application.Common.Mappings; + +public interface IMapFrom +{ + void Mapping(Profile profile) => profile.CreateMap(typeof(T), GetType()); +} diff --git a/AutobusApi.Application/Common/Mappings/MappingProfile.cs b/AutobusApi.Application/Common/Mappings/MappingProfile.cs new file mode 100644 index 0000000..03c184e --- /dev/null +++ b/AutobusApi.Application/Common/Mappings/MappingProfile.cs @@ -0,0 +1,35 @@ +using System.Reflection; +using AutoMapper; + +namespace AutobusApi.Application.Common.Mappings; + +public class MappingProfile : Profile +{ + public MappingProfile() + { + ApplyMappingsFromAssembly(Assembly.GetExecutingAssembly()); + } + + private void ApplyMappingsFromAssembly(Assembly assembly) + { + var types = assembly.GetExportedTypes() + .Where(t => t.GetInterfaces() + .Any(i => + i.IsGenericType && + i.GetGenericTypeDefinition() == typeof(IMapFrom<>) + ) + ) + .ToList(); + + foreach (var type in types) + { + var instance = Activator.CreateInstance(type); + + var methodInfo = + type.GetMethod("Mapping") ?? + type.GetInterface("IMapFrom`1")?.GetMethod("Mapping"); + + methodInfo?.Invoke(instance, new object[] { this }); + } + } +} diff --git a/AutobusApi.Application/DependencyInjection.cs b/AutobusApi.Application/DependencyInjection.cs index 7896730..6e875f9 100644 --- a/AutobusApi.Application/DependencyInjection.cs +++ b/AutobusApi.Application/DependencyInjection.cs @@ -12,6 +12,8 @@ public static class DependencyInjection { services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly()); + services.AddAutoMapper(Assembly.GetExecutingAssembly()); + services.AddMediatR(configuration => { configuration.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()); diff --git a/AutobusApi.UnitTests/Common/Mappings/MappingTests.cs b/AutobusApi.UnitTests/Common/Mappings/MappingTests.cs new file mode 100644 index 0000000..73cf8c1 --- /dev/null +++ b/AutobusApi.UnitTests/Common/Mappings/MappingTests.cs @@ -0,0 +1,31 @@ +using AutobusApi.Application.RouteSearch; +using AutoMapper; + +namespace AutobusApi.UnitTests.Common.Mappings; + +public class MappingTests : IClassFixture +{ + private readonly IConfigurationProvider _configuration; + private readonly IMapper _mapper; + + public MappingTests(MappingTestsFixture fixure) + { + _configuration = fixure.ConfigurationProvider; + _mapper = fixure.Mapper; + } + + [Fact] + public void ShouldHaveValidConfiguraion() + { + _configuration.AssertConfigurationIsValid(); + } + + [Theory] + // [InlineData(typeof(), typeof())] + public void ShouldSupportMappingFromSourceDoDestination(Type source, Type destination) + { + var instance = Activator.CreateInstance(source); + + _mapper.Map(instance, source, destination); + } +} diff --git a/AutobusApi.UnitTests/Common/Mappings/MappingTestsFixture.cs b/AutobusApi.UnitTests/Common/Mappings/MappingTestsFixture.cs new file mode 100644 index 0000000..e6ec9d1 --- /dev/null +++ b/AutobusApi.UnitTests/Common/Mappings/MappingTestsFixture.cs @@ -0,0 +1,18 @@ +using AutobusApi.Application.Common.Mappings; +using AutoMapper; + +namespace AutobusApi.UnitTests.Common.Mappings; + +public class MappingTestsFixture +{ + public IConfigurationProvider ConfigurationProvider; + + public IMapper Mapper; + + public MappingTestsFixture() + { + ConfigurationProvider = new MapperConfiguration(options => options.AddProfile()); + + Mapper = ConfigurationProvider.CreateMapper(); + } +}