-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
574decf
commit 19905bf
Showing
7 changed files
with
132 additions
and
98 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
196 changes: 98 additions & 98 deletions
196
Threenine.Map/MapConfigurationFactory.cs → src/MapConfigurationFactory.cs
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 |
---|---|---|
@@ -1,99 +1,99 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using AutoMapper; | ||
|
||
namespace Threenine.Map | ||
{ | ||
public class MapConfigurationFactory | ||
{ | ||
public static void Scan<TType>(Func<AssemblyName, bool> assemblyFilter = null) | ||
{ | ||
var target = typeof(TType).Assembly; | ||
|
||
bool LoadAllFilter(AssemblyName x) => true; | ||
|
||
var assembliesToLoad = target.GetReferencedAssemblies() | ||
.Where(assemblyFilter ?? LoadAllFilter) | ||
.Select(Assembly.Load) | ||
.ToList(); | ||
|
||
assembliesToLoad.Add(target); | ||
|
||
LoadMapsFromAssemblies(assembliesToLoad.ToArray()); | ||
} | ||
|
||
public static void LoadMapsFromAssemblies(params Assembly[] assemblies) | ||
{ | ||
var types = assemblies.SelectMany(a => a.GetExportedTypes()).ToArray(); | ||
LoadAllMappings(types); | ||
} | ||
|
||
|
||
public static void LoadAllMappings(IList<Type> types) | ||
{ | ||
Mapper.Initialize( | ||
cfg => | ||
{ | ||
LoadStandardMappings(cfg, types); | ||
LoadCustomMappings(cfg, types); | ||
}); | ||
} | ||
|
||
|
||
public static void LoadCustomMappings(IMapperConfigurationExpression config, IList<Type> types) | ||
{ | ||
var instancesToMap = (from t in types | ||
from i in t.GetInterfaces() | ||
where typeof(ICustomMap).IsAssignableFrom(t) && | ||
!t.IsAbstract && | ||
!t.IsInterface | ||
select (ICustomMap) Activator.CreateInstance(t)).ToArray(); | ||
|
||
|
||
foreach (var map in instancesToMap) | ||
{ | ||
map.CustomMap(config); | ||
} | ||
} | ||
|
||
public static void LoadStandardMappings(IMapperConfigurationExpression config, IList<Type> types) | ||
{ | ||
var mapsFrom = (from t in types | ||
from i in t.GetInterfaces() | ||
where i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMapFrom<>) && | ||
!t.IsAbstract && | ||
!t.IsInterface | ||
select new | ||
{ | ||
Source = i.GetGenericArguments()[0], | ||
Destination = t | ||
}).ToArray(); | ||
|
||
|
||
foreach (var map in mapsFrom) | ||
{ | ||
config.CreateMap(map.Source, map.Destination); | ||
} | ||
|
||
|
||
var mapsTo = (from t in types | ||
from i in t.GetInterfaces() | ||
where i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMapTo<>) && | ||
!t.IsAbstract && | ||
!t.IsInterface | ||
select new | ||
{ | ||
Source = i.GetGenericArguments()[0], | ||
Destination = t | ||
}).ToArray(); | ||
|
||
|
||
foreach (var map in mapsTo) | ||
{ | ||
config.CreateMap(map.Source, map.Destination); | ||
} | ||
} | ||
} | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using AutoMapper; | ||
|
||
namespace Threenine.Map | ||
{ | ||
public class MapConfigurationFactory | ||
{ | ||
public static void Scan<TType>(Func<AssemblyName, bool> assemblyFilter = null) | ||
{ | ||
var target = typeof(TType).Assembly; | ||
|
||
bool LoadAllFilter(AssemblyName x) => true; | ||
|
||
var assembliesToLoad = target.GetReferencedAssemblies() | ||
.Where(assemblyFilter ?? LoadAllFilter) | ||
.Select(Assembly.Load) | ||
.ToList(); | ||
|
||
assembliesToLoad.Add(target); | ||
|
||
LoadMapsFromAssemblies(assembliesToLoad.ToArray()); | ||
} | ||
|
||
public static void LoadMapsFromAssemblies(params Assembly[] assemblies) | ||
{ | ||
var types = assemblies.SelectMany(a => a.GetExportedTypes()).ToArray(); | ||
LoadAllMappings(types); | ||
} | ||
|
||
|
||
public static void LoadAllMappings(IList<Type> types) | ||
{ | ||
Mapper.Initialize( | ||
cfg => | ||
{ | ||
LoadStandardMappings(cfg, types); | ||
LoadCustomMappings(cfg, types); | ||
}); | ||
} | ||
|
||
|
||
public static void LoadCustomMappings(IMapperConfigurationExpression config, IList<Type> types) | ||
{ | ||
var instancesToMap = (from t in types | ||
from i in t.GetInterfaces() | ||
where typeof(ICustomMap).IsAssignableFrom(t) && | ||
!t.IsAbstract && | ||
!t.IsInterface | ||
select (ICustomMap) Activator.CreateInstance(t)).ToArray(); | ||
|
||
|
||
foreach (var map in instancesToMap) | ||
{ | ||
map.CustomMap(config); | ||
} | ||
} | ||
|
||
public static void LoadStandardMappings(IMapperConfigurationExpression config, IList<Type> types) | ||
{ | ||
var mapsFrom = (from t in types | ||
from i in t.GetInterfaces() | ||
where i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMapFrom<>) && | ||
!t.IsAbstract && | ||
!t.IsInterface | ||
select new | ||
{ | ||
Source = i.GetGenericArguments()[0], | ||
Destination = t | ||
}).ToArray(); | ||
|
||
|
||
foreach (var map in mapsFrom) | ||
{ | ||
config.CreateMap(map.Source, map.Destination); | ||
} | ||
|
||
|
||
var mapsTo = (from t in types | ||
from i in t.GetInterfaces() | ||
where i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMapTo<>) && | ||
!t.IsAbstract && | ||
!t.IsInterface | ||
select new | ||
{ | ||
Source = i.GetGenericArguments()[0], | ||
Destination = t | ||
}).ToArray(); | ||
|
||
|
||
foreach (var map in mapsTo) | ||
{ | ||
config.CreateMap(map.Source, map.Destination); | ||
} | ||
} | ||
} | ||
} |
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
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,27 @@ | ||
====================================================================================== | ||
threenine.co.uk - Threenine.Map | ||
====================================================================================== | ||
|
||
Thank you for downloading our package! | ||
|
||
For more details regarding the package check out the package website https://threenine.github.io/Threenine.Map/ | ||
|
||
Documentation | ||
============= | ||
|
||
To help you to make the most use of the package we have detailed documentation available on Read The Docs | ||
check out http://threeninemap.readthedocs.io/en/latest/Getting-started.html | ||
|
||
|
||
Bugs & Feature Requests | ||
======================= | ||
|
||
If you experience any issues or would like some addtional functionality please raise an issue on Github | ||
https://github.com/threenine/Threenine.Map/issues | ||
|
||
|
||
Source Code | ||
=========== | ||
|
||
All source code for the project is available on Github | ||
https://github.com/threenine/Threenine.Map/tree/master/Threenine.Map |