Commit 843f7316 authored by boncho vylkov's avatar boncho vylkov

implement automapper configs

parent 961dd1b9
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace MvcCoreTemplate.Web.Infrastructure.Mapping
{
public static class AutoMapperConfig
{
public static void RegisterMappings(params Assembly[] assemblies)
{
var types = assemblies.SelectMany(a => a.GetExportedTypes()).ToList();
Mapper.Initialize(configuration =>
{
RegisterStandardFromMappings(configuration, types);
RegisterStandardToMappings(configuration, types);
RegisterCustomMaps(configuration, types);
});
}
private static void RegisterStandardFromMappings(IProfileExpression configuration, IEnumerable<Type> types)
{
var maps = GetFromMaps(types);
CreateMappings(configuration, maps);
}
private static void RegisterStandardToMappings(IProfileExpression configuration, IEnumerable<Type> types)
{
var maps = GetToMaps(types);
CreateMappings(configuration, maps);
}
private static void RegisterCustomMaps(IMapperConfigurationExpression configuration, IEnumerable<Type> types)
{
var maps = GetCustomMappings(types);
CreateMappings(configuration, maps);
}
private static IEnumerable<IHaveCustomMappings> GetCustomMappings(IEnumerable<Type> types)
{
var customMaps = from t in types
from i in t.GetTypeInfo().GetInterfaces()
where typeof(IHaveCustomMappings).GetTypeInfo().IsAssignableFrom(t) &&
!t.GetTypeInfo().IsAbstract &&
!t.GetTypeInfo().IsInterface
select (IHaveCustomMappings)Activator.CreateInstance(t);
return customMaps;
}
private static IEnumerable<TypesMap> GetFromMaps(IEnumerable<Type> types)
{
var fromMaps = from t in types
from i in t.GetTypeInfo().GetInterfaces()
where i.GetTypeInfo().IsGenericType &&
i.GetGenericTypeDefinition() == typeof(IMapFrom<>) &&
!t.GetTypeInfo().IsAbstract &&
!t.GetTypeInfo().IsInterface
select new TypesMap
{
Source = i.GetTypeInfo().GetGenericArguments()[0],
Destination = t
};
return fromMaps;
}
private static IEnumerable<TypesMap> GetToMaps(IEnumerable<Type> types)
{
var toMaps = from t in types
from i in t.GetTypeInfo().GetInterfaces()
where i.GetTypeInfo().IsGenericType &&
i.GetTypeInfo().GetGenericTypeDefinition() == typeof(IMapTo<>) &&
!t.GetTypeInfo().IsAbstract &&
!t.GetTypeInfo().IsInterface
select new TypesMap
{
Source = t,
Destination = i.GetTypeInfo().GetGenericArguments()[0]
};
return toMaps;
}
private static void CreateMappings(IProfileExpression configuration, IEnumerable<TypesMap> maps)
{
foreach (var map in maps)
{
configuration.CreateMap(map.Source, map.Destination);
}
}
private static void CreateMappings(IMapperConfigurationExpression configuration, IEnumerable<IHaveCustomMappings> maps)
{
foreach (var map in maps)
{
map.CreateMappings(configuration);
}
}
}
}
using AutoMapper;
namespace MvcCoreTemplate.Web.Infrastructure.Mapping
{
public interface IHaveCustomMappings
{
void CreateMappings(IMapperConfigurationExpression configuration);
}
}
namespace MvcCoreTemplate.Web.Infrastructure.Mapping
{
public interface IMapFrom<T>
{
}
}
namespace MvcCoreTemplate.Web.Infrastructure.Mapping
{
public interface IMapTo<T>
{
}
}
namespace MvcCoreTemplate.Web.Infrastructure.Mapping
{
using System;
using System.Linq;
using System.Linq.Expressions;
using AutoMapper.QueryableExtensions;
public static class QueryableExtensions
{
public static IQueryable<TDestination> To<TDestination>(
this IQueryable source,
params Expression<Func<TDestination, object>>[] membersToExpand)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
return source.ProjectTo(membersToExpand);
}
public static IQueryable<TDestination> To<TDestination>(
this IQueryable source,
object parameters)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
return source.ProjectTo<TDestination>(parameters);
}
}
}
namespace MvcCoreTemplate.Web.Infrastructure.Mapping
{
using System;
internal class TypesMap
{
public Type Source { get; set; }
public Type Destination { get; set; }
}
}
......@@ -9,6 +9,9 @@ using MvcCoreTemplate.Models;
using MvcCoreTemplate.Data;
using MvcCoreTemplate.Services.Web;
using MvcCoreTemplate.Services.Data;
using MvcCoreTemplate.Web.Infrastructure.Mapping;
using MvcCoreTemplate.Web.Models.AccountViewModels;
using System.Reflection;
namespace MvcCoreTemplate.Web
{
......@@ -55,6 +58,11 @@ namespace MvcCoreTemplate.Web
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
AutoMapperConfig.RegisterMappings(
typeof(LoginViewModel).GetTypeInfo().Assembly,
typeof(LoginViewModel).GetTypeInfo().Assembly);
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment