Skip to content

Commit

Permalink
Merge pull request #11 from clay-one/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
shadi-mahm authored Oct 28, 2018
2 parents d8150cc + 7394ab7 commit ac39cea
Show file tree
Hide file tree
Showing 57 changed files with 4,464 additions and 2 deletions.
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;
}
}
}
37 changes: 37 additions & 0 deletions src/General/Collections/CollectionExtensions.cs
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);
}
}
}
Loading

0 comments on commit ac39cea

Please sign in to comment.