-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from clay-one/feat-adding-auto-mapper
Added Auto Mapper to Hydrogen
- Loading branch information
Showing
5 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters