Skip to content

Commit

Permalink
Merge pull request #10 from clay-one/feat-adding-auto-mapper
Browse files Browse the repository at this point in the history
Added Auto Mapper to Hydrogen
  • Loading branch information
shadi-mahm authored Oct 28, 2018
2 parents 444cdb4 + b30a3b3 commit 7394ab7
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/AutoMapper/AutoMapper.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>hydrogen.AutoMapper</RootNamespace>
<AssemblyName>hydrogen.AutoMapper</AssemblyName>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="7.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\General\General.csproj" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions src/AutoMapper/AutoMapperConfigAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace hydrogen.AutoMapper
{
/// <summary>
/// Specifies that a type has AutoMapper configuration
/// </summary>
/// <remarks>
/// Any type that is decorated by this attribute, should have a public, static method called "ConfigureAutoMapper"
/// that doesn't take any arguments, and should return void. This method will be called during AutoMapper configuration,
/// by AutoMapperConfigurator, prior to validation of mappings.
/// </remarks>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = false)]
public class AutoMapperConfigAttribute : Attribute
{
}
}
40 changes: 40 additions & 0 deletions src/AutoMapper/AutoMapperConfigurationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Linq.Expressions;
using AutoMapper;

namespace hydrogen.AutoMapper
{
public static class AutoMapperConfigurationExtensions
{
public static IMappingExpression<TSource, TDestination> IgnoreAll<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)
{
expression.ForAllMembers(opt => opt.Ignore());
return expression;
}

public static IMappingExpression<TSource, TDestination> IgnoreUnmappedProperties<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)
{
var typeMap = Mapper.Configuration.FindTypeMapFor<TSource, TDestination>();
if (typeMap != null)
{
foreach (var unmappedPropertyName in typeMap.GetUnmappedPropertyNames())
{
expression.ForMember(unmappedPropertyName, opt => opt.Ignore());
}
}

return expression;
}

public static IMappingExpression<TSource, TDestination> Ignore<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression, Expression<Func<TDestination, object>> destinationMember)
{
return expression.ForMember(destinationMember, opt => opt.Ignore());
}

public static IMappingExpression<TSource, TDestination> IgnoreSource<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression, Expression<Func<TSource, object>> sourceMember)
{
return expression.ForSourceMember(sourceMember, opt => opt.Ignore());
}

}
}
41 changes: 41 additions & 0 deletions src/AutoMapper/AutoMapperConfigurator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Linq;
using System.Reflection;
using hydrogen.General.Collections;

namespace hydrogen.AutoMapper
{
public static class AutoMapperConfigurator
{
public static void Scan(Assembly assembly)
{
assembly.GetTypes()
.Where(t => t.GetCustomAttributes(typeof(AutoMapperConfigAttribute)).Any())
.OrderBy(GetDistanceFromObject)
.ForEach(type =>
{
var method = type.GetMethod("ConfigureAutoMapper", new Type[0]);
if (method == null || !method.IsStatic || method.ReturnType != typeof(void) || method.GetParameters().Any())
throw new InvalidOperationException(
"Type " + type.FullName + " is decorated with [AutoMapperConfigAttribute] but does not contain a public static method with the signature of 'void ConfigureAutoMapper()'");

method.Invoke(null, null);
});
}

private static int GetDistanceFromObject(Type t)
{
var result = 0;
while (!(typeof(object) == t))
{
if (t == null)
return -1;

result++;
t = t.BaseType;
}

return result;
}
}
}
8 changes: 7 additions & 1 deletion src/hydrogen.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27703.2035
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "General", "General\General.csproj", "{2436344A-7627-4F64-81FA-E9B5AB6FB844}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "General", "General\General.csproj", "{2436344A-7627-4F64-81FA-E9B5AB6FB844}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoMapper", "AutoMapper\AutoMapper.csproj", "{4280757D-3D3D-4DBD-9F9C-7F89F1027452}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,6 +17,10 @@ Global
{2436344A-7627-4F64-81FA-E9B5AB6FB844}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2436344A-7627-4F64-81FA-E9B5AB6FB844}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2436344A-7627-4F64-81FA-E9B5AB6FB844}.Release|Any CPU.Build.0 = Release|Any CPU
{4280757D-3D3D-4DBD-9F9C-7F89F1027452}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4280757D-3D3D-4DBD-9F9C-7F89F1027452}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4280757D-3D3D-4DBD-9F9C-7F89F1027452}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4280757D-3D3D-4DBD-9F9C-7F89F1027452}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 7394ab7

Please sign in to comment.