mirror of https://github.com/abpframework/abp.git
14 changed files with 408 additions and 5 deletions
@ -0,0 +1,23 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>netstandard1.6</TargetFramework> |
||||
|
<AssemblyName>Volo.Abp.AutoMapper</AssemblyName> |
||||
|
<PackageId>Volo.Abp.AutoMapper</PackageId> |
||||
|
<NetStandardImplicitPackageVersion>1.6.1</NetStandardImplicitPackageVersion> |
||||
|
<PackageTargetFallback>$(PackageTargetFallback);dnxcore50</PackageTargetFallback> |
||||
|
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> |
||||
|
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> |
||||
|
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> |
||||
|
<RootNamespace /> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\Volo.Abp\Volo.Abp.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<PackageReference Include="AutoMapper" Version="6.1.1" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,19 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using AutoMapper; |
||||
|
|
||||
|
namespace Volo.Abp.AutoMapper |
||||
|
{ |
||||
|
public class AbpAutoMapperConfiguration : IAbpAutoMapperConfiguration |
||||
|
{ |
||||
|
public List<Action<IMapperConfigurationExpression>> Configurators { get; } |
||||
|
|
||||
|
public bool UseStaticMapper { get; set; } |
||||
|
|
||||
|
public AbpAutoMapperConfiguration() |
||||
|
{ |
||||
|
UseStaticMapper = true; |
||||
|
Configurators = new List<Action<IMapperConfigurationExpression>>(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,98 @@ |
|||||
|
using System; |
||||
|
using AutoMapper; |
||||
|
using Microsoft.Extensions.Logging; |
||||
|
using Volo.Abp.Modularity; |
||||
|
|
||||
|
namespace Volo.Abp.AutoMapper |
||||
|
{ |
||||
|
public class AbpAutoMapperModule : AbpModule |
||||
|
{ |
||||
|
//private readonly ITypeFinder _typeFinder;
|
||||
|
|
||||
|
//private static volatile bool _createdMappingsBefore;
|
||||
|
//private static readonly object SyncObj = new object();
|
||||
|
|
||||
|
//public AbpAutoMapperModule(ITypeFinder typeFinder)
|
||||
|
//{
|
||||
|
// _typeFinder = typeFinder;
|
||||
|
//}
|
||||
|
|
||||
|
//public override void PreInitialize()
|
||||
|
//{
|
||||
|
// IocManager.Register<IAbpAutoMapperConfiguration, AbpAutoMapperConfiguration>();
|
||||
|
|
||||
|
// Configuration.ReplaceService<ObjectMapping.IObjectMapper, AutoMapperObjectMapper>();
|
||||
|
|
||||
|
// Configuration.Modules.AbpAutoMapper().Configurators.Add(CreateCoreMappings);
|
||||
|
//}
|
||||
|
|
||||
|
//public override void PostInitialize()
|
||||
|
//{
|
||||
|
// CreateMappings();
|
||||
|
//}
|
||||
|
|
||||
|
//private void CreateMappings()
|
||||
|
//{
|
||||
|
// lock (SyncObj)
|
||||
|
// {
|
||||
|
// Action<IMapperConfigurationExpression> configurer = configuration =>
|
||||
|
// {
|
||||
|
// FindAndAutoMapTypes(configuration);
|
||||
|
// foreach (var configurator in Configuration.Modules.AbpAutoMapper().Configurators)
|
||||
|
// {
|
||||
|
// configurator(configuration);
|
||||
|
// }
|
||||
|
// };
|
||||
|
|
||||
|
// if (Configuration.Modules.AbpAutoMapper().UseStaticMapper)
|
||||
|
// {
|
||||
|
// //We should prevent duplicate mapping in an application, since Mapper is static.
|
||||
|
// if (!_createdMappingsBefore)
|
||||
|
// {
|
||||
|
// Mapper.Initialize(configurer);
|
||||
|
// _createdMappingsBefore = true;
|
||||
|
// }
|
||||
|
|
||||
|
// IocManager.IocContainer.Register(
|
||||
|
// Component.For<IMapper>().Instance(Mapper.Instance).LifestyleSingleton()
|
||||
|
// );
|
||||
|
// }
|
||||
|
// else
|
||||
|
// {
|
||||
|
// var config = new MapperConfiguration(configurer);
|
||||
|
// IocManager.IocContainer.Register(
|
||||
|
// Component.For<IMapper>().Instance(config.CreateMapper()).LifestyleSingleton()
|
||||
|
// );
|
||||
|
// }
|
||||
|
// }
|
||||
|
//}
|
||||
|
|
||||
|
//private void FindAndAutoMapTypes(IMapperConfigurationExpression configuration)
|
||||
|
//{
|
||||
|
// var types = _typeFinder.Find(type =>
|
||||
|
// {
|
||||
|
// var typeInfo = type.GetTypeInfo();
|
||||
|
// return typeInfo.IsDefined(typeof(AutoMapAttribute)) ||
|
||||
|
// typeInfo.IsDefined(typeof(AutoMapFromAttribute)) ||
|
||||
|
// typeInfo.IsDefined(typeof(AutoMapToAttribute));
|
||||
|
// }
|
||||
|
// );
|
||||
|
|
||||
|
// Logger<>.DebugFormat("Found {0} classes define auto mapping attributes", types.Length);
|
||||
|
|
||||
|
// foreach (var type in types)
|
||||
|
// {
|
||||
|
// Logger<>.Debug(type.FullName);
|
||||
|
// configuration.CreateAutoAttributeMaps(type);
|
||||
|
// }
|
||||
|
//}
|
||||
|
|
||||
|
//private void CreateCoreMappings(IMapperConfigurationExpression configuration)
|
||||
|
//{
|
||||
|
// var localizationContext = IocManager.Resolve<ILocalizationContext>();
|
||||
|
|
||||
|
// configuration.CreateMap<ILocalizableString, string>().ConvertUsing(ls => ls?.Localize(localizationContext));
|
||||
|
// configuration.CreateMap<LocalizableString, string>().ConvertUsing(ls => ls == null ? null : localizationContext.LocalizationManager.GetString(ls));
|
||||
|
//}
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,29 @@ |
|||||
|
using System; |
||||
|
using AutoMapper; |
||||
|
using Volo.ExtensionMethods.Collections.Generic; |
||||
|
|
||||
|
namespace Volo.Abp.AutoMapper |
||||
|
{ |
||||
|
public class AutoMapAttribute : AutoMapAttributeBase |
||||
|
{ |
||||
|
public AutoMapAttribute(params Type[] targetTypes) |
||||
|
: base(targetTypes) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public override void CreateMap(IMapperConfigurationExpression configuration, Type type) |
||||
|
{ |
||||
|
if (TargetTypes.IsNullOrEmpty()) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
foreach (var targetType in TargetTypes) |
||||
|
{ |
||||
|
configuration.CreateMap(type, targetType, MemberList.Source); |
||||
|
configuration.CreateMap(targetType, type, MemberList.Destination); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,17 @@ |
|||||
|
using System; |
||||
|
using AutoMapper; |
||||
|
|
||||
|
namespace Volo.Abp.AutoMapper |
||||
|
{ |
||||
|
public abstract class AutoMapAttributeBase : Attribute |
||||
|
{ |
||||
|
public Type[] TargetTypes { get; } |
||||
|
|
||||
|
protected AutoMapAttributeBase(params Type[] targetTypes) |
||||
|
{ |
||||
|
TargetTypes = targetTypes; |
||||
|
} |
||||
|
|
||||
|
public abstract void CreateMap(IMapperConfigurationExpression configuration, Type type); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,32 @@ |
|||||
|
using AutoMapper; |
||||
|
|
||||
|
namespace Volo.Abp.AutoMapper |
||||
|
{ |
||||
|
public static class AutoMapExtensions |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Converts an object to another using AutoMapper library. Creates a new object of <typeparamref name="TDestination"/>.
|
||||
|
/// There must be a mapping between objects before calling this method.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TDestination">Type of the destination object</typeparam>
|
||||
|
/// <param name="source">Source object</param>
|
||||
|
public static TDestination MapTo<TDestination>(this object source) |
||||
|
{ |
||||
|
return Mapper.Map<TDestination>(source); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Execute a mapping from the source object to the existing destination object
|
||||
|
/// There must be a mapping between objects before calling this method.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TSource">Source type</typeparam>
|
||||
|
/// <typeparam name="TDestination">Destination type</typeparam>
|
||||
|
/// <param name="source">Source object</param>
|
||||
|
/// <param name="destination">Destination object</param>
|
||||
|
/// <returns></returns>
|
||||
|
public static TDestination MapTo<TSource, TDestination>(this TSource source, TDestination destination) |
||||
|
{ |
||||
|
return Mapper.Map(source, destination); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,36 @@ |
|||||
|
using System; |
||||
|
using AutoMapper; |
||||
|
using Volo.ExtensionMethods.Collections.Generic; |
||||
|
|
||||
|
namespace Volo.Abp.AutoMapper |
||||
|
{ |
||||
|
public class AutoMapFromAttribute : AutoMapAttributeBase |
||||
|
{ |
||||
|
public MemberList MemberList { get; set; } = MemberList.Destination; |
||||
|
|
||||
|
public AutoMapFromAttribute(params Type[] targetTypes) |
||||
|
: base(targetTypes) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public AutoMapFromAttribute(MemberList memberList, params Type[] targetTypes) |
||||
|
: this(targetTypes) |
||||
|
{ |
||||
|
MemberList = memberList; |
||||
|
} |
||||
|
|
||||
|
public override void CreateMap(IMapperConfigurationExpression configuration, Type type) |
||||
|
{ |
||||
|
if (TargetTypes.IsNullOrEmpty()) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
foreach (var targetType in TargetTypes) |
||||
|
{ |
||||
|
configuration.CreateMap(targetType, type, MemberList); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,36 @@ |
|||||
|
using System; |
||||
|
using AutoMapper; |
||||
|
using Volo.ExtensionMethods.Collections.Generic; |
||||
|
|
||||
|
namespace Volo.Abp.AutoMapper |
||||
|
{ |
||||
|
public class AutoMapToAttribute : AutoMapAttributeBase |
||||
|
{ |
||||
|
public MemberList MemberList { get; set; } = MemberList.Source; |
||||
|
|
||||
|
public AutoMapToAttribute(params Type[] targetTypes) |
||||
|
: base(targetTypes) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public AutoMapToAttribute(MemberList memberList, params Type[] targetTypes) |
||||
|
: this(targetTypes) |
||||
|
{ |
||||
|
MemberList = memberList; |
||||
|
} |
||||
|
|
||||
|
public override void CreateMap(IMapperConfigurationExpression configuration, Type type) |
||||
|
{ |
||||
|
if (TargetTypes.IsNullOrEmpty()) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
foreach (var targetType in TargetTypes) |
||||
|
{ |
||||
|
configuration.CreateMap(type, targetType, MemberList); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,17 @@ |
|||||
|
using System; |
||||
|
using System.Reflection; |
||||
|
using AutoMapper; |
||||
|
|
||||
|
namespace Volo.Abp.AutoMapper |
||||
|
{ |
||||
|
internal static class AutoMapperConfigurationExtensions |
||||
|
{ |
||||
|
public static void CreateAutoAttributeMaps(this IMapperConfigurationExpression configuration, Type type) |
||||
|
{ |
||||
|
foreach (var autoMapAttribute in type.GetTypeInfo().GetCustomAttributes<AutoMapAttributeBase>()) |
||||
|
{ |
||||
|
autoMapAttribute.CreateMap(configuration, type); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,24 @@ |
|||||
|
using AutoMapper; |
||||
|
|
||||
|
namespace Volo.Abp.AutoMapper |
||||
|
{ |
||||
|
public class AutoMapperObjectMapper : Volo.Abp.ObjectMapping.IObjectMapper |
||||
|
{ |
||||
|
private readonly IMapper _mapper; |
||||
|
|
||||
|
public AutoMapperObjectMapper(IMapper mapper) |
||||
|
{ |
||||
|
_mapper = mapper; |
||||
|
} |
||||
|
|
||||
|
public TDestination Map<TDestination>(object source) |
||||
|
{ |
||||
|
return _mapper.Map<TDestination>(source); |
||||
|
} |
||||
|
|
||||
|
public TDestination Map<TSource, TDestination>(TSource source, TDestination destination) |
||||
|
{ |
||||
|
return _mapper.Map(source, destination); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,17 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using AutoMapper; |
||||
|
|
||||
|
namespace Volo.Abp.AutoMapper |
||||
|
{ |
||||
|
public interface IAbpAutoMapperConfiguration |
||||
|
{ |
||||
|
List<Action<IMapperConfigurationExpression>> Configurators { get; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Use static <see cref="Mapper.Instance"/>.
|
||||
|
/// Default: true.
|
||||
|
/// </summary>
|
||||
|
bool UseStaticMapper { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,25 @@ |
|||||
|
namespace Volo.Abp.ObjectMapping |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Defines a simple interface to map objects.
|
||||
|
/// </summary>
|
||||
|
public interface IObjectMapper |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Converts an object to another. Creates a new object of <see cref="TDestination"/>.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TDestination">Type of the destination object</typeparam>
|
||||
|
/// <param name="source">Source object</param>
|
||||
|
TDestination Map<TDestination>(object source); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Execute a mapping from the source object to the existing destination object
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TSource">Source type</typeparam>
|
||||
|
/// <typeparam name="TDestination">Destination type</typeparam>
|
||||
|
/// <param name="source">Source object</param>
|
||||
|
/// <param name="destination">Destination object</param>
|
||||
|
/// <returns>Returns the same <see cref="destination"/> object after mapping operation</returns>
|
||||
|
TDestination Map<TSource, TDestination>(TSource source, TDestination destination); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,23 @@ |
|||||
|
using System; |
||||
|
using Volo.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.ObjectMapping |
||||
|
{ |
||||
|
public sealed class NotImplementedObjectMapper : IObjectMapper, ISingletonDependency |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Singleton instance.
|
||||
|
/// </summary>
|
||||
|
public static NotImplementedObjectMapper Instance { get; } = new NotImplementedObjectMapper(); |
||||
|
|
||||
|
public TDestination Map<TDestination>(object source) |
||||
|
{ |
||||
|
throw new NotImplementedException("Abp.ObjectMapping.IObjectMapper should be implemented in order to map objects."); |
||||
|
} |
||||
|
|
||||
|
public TDestination Map<TSource, TDestination>(TSource source, TDestination destination) |
||||
|
{ |
||||
|
throw new NotImplementedException("Abp.ObjectMapping.IObjectMapper should be implemented in order to map objects."); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue