-
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 #11 from clay-one/develop
Develop
- Loading branch information
Showing
57 changed files
with
4,464 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace hydrogen.General.Collections | ||
{ | ||
public static class CollectionExtensions | ||
{ | ||
public static void AddAll<T>(this ICollection<T> target, IEnumerable<T> items) | ||
{ | ||
foreach (var item in items) | ||
{ | ||
target.Add(item); | ||
} | ||
} | ||
|
||
public static void AddAll<T>(this ISet<T> target, IEnumerable<T> items) | ||
{ | ||
foreach (var item in items) | ||
{ | ||
target.Add(item); | ||
} | ||
} | ||
|
||
public static void AddIfNotContains<T>(this ICollection<T> target, T item) | ||
{ | ||
if (!target.Contains(item)) | ||
target.Add(item); | ||
} | ||
|
||
public static void SetItemExistance<T>(this ICollection<T> target, T item, bool existance) | ||
{ | ||
if (existance && !target.Contains(item)) | ||
target.Add(item); | ||
else | ||
target.Remove(item); | ||
} | ||
} | ||
} |
Oops, something went wrong.