Moved object mapper code from current Abp. Will make it working.

pull/96/head
Halil İbrahim Kalkan 8 years ago
parent f5fa92287c
commit 957ff741e6

@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26228.9
VisualStudioVersion = 15.0.26430.6
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{5DF0E140-0513-4D0D-BE2E-3D4D85CD70E6}"
EndProject
@ -106,13 +106,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.Identity.Web", "sr
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.Identity.HttpApi.Client", "src\Volo.Abp.Identity.HttpApi.Client\Volo.Abp.Identity.HttpApi.Client.csproj", "{00B240B6-EC44-461A-9578-EF4F1BE9C688}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Autofac", "src\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj", "{CECE1288-B5A1-4A6B-BEE0-331861F94983}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.Autofac", "src\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj", "{CECE1288-B5A1-4A6B-BEE0-331861F94983}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Castle.Core", "src\Volo.Abp.Castle.Core\Volo.Abp.Castle.Core.csproj", "{053F7446-0545-482E-9F29-9C96B926966C}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.Castle.Core", "src\Volo.Abp.Castle.Core\Volo.Abp.Castle.Core.csproj", "{053F7446-0545-482E-9F29-9C96B926966C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Autofac.Tests", "test\Volo.Abp.Autofac.Tests\Volo.Abp.Autofac.Tests.csproj", "{D8BE64D2-BD83-40F5-9783-D7FDDF668C45}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.Autofac.Tests", "test\Volo.Abp.Autofac.Tests\Volo.Abp.Autofac.Tests.csproj", "{D8BE64D2-BD83-40F5-9783-D7FDDF668C45}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Castle.Core.Tests", "test\Volo.Abp.Castle.Core.Tests\Volo.Abp.Castle.Core.Tests.csproj", "{CE12E5C2-7B3E-4637-B6A3-274BB5C3DE16}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.Castle.Core.Tests", "test\Volo.Abp.Castle.Core.Tests\Volo.Abp.Castle.Core.Tests.csproj", "{CE12E5C2-7B3E-4637-B6A3-274BB5C3DE16}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.AutoMapper", "src\Volo.Abp.AutoMapper\Volo.Abp.AutoMapper.csproj", "{D2F3594F-E2B9-4338-A022-F00C4E9A14C3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -276,6 +278,10 @@ Global
{CE12E5C2-7B3E-4637-B6A3-274BB5C3DE16}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CE12E5C2-7B3E-4637-B6A3-274BB5C3DE16}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CE12E5C2-7B3E-4637-B6A3-274BB5C3DE16}.Release|Any CPU.Build.0 = Release|Any CPU
{D2F3594F-E2B9-4338-A022-F00C4E9A14C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D2F3594F-E2B9-4338-A022-F00C4E9A14C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D2F3594F-E2B9-4338-A022-F00C4E9A14C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D2F3594F-E2B9-4338-A022-F00C4E9A14C3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -329,5 +335,6 @@ Global
{053F7446-0545-482E-9F29-9C96B926966C} = {4C753F64-0C93-4D65-96C2-A40893AFC1E8}
{D8BE64D2-BD83-40F5-9783-D7FDDF668C45} = {37087D1B-3693-4E96-983D-A69F210BDE53}
{CE12E5C2-7B3E-4637-B6A3-274BB5C3DE16} = {37087D1B-3693-4E96-983D-A69F210BDE53}
{D2F3594F-E2B9-4338-A022-F00C4E9A14C3} = {4C753F64-0C93-4D65-96C2-A40893AFC1E8}
EndGlobalSection
EndGlobal

@ -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…
Cancel
Save