This repository has been archived by the owner on Apr 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
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 #2 from HenkMollema/conventions
Merge branch 'conventions' into 'master'
- Loading branch information
Showing
22 changed files
with
741 additions
and
111 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.
Binary file not shown.
142 changes: 142 additions & 0 deletions
142
src/Dapper.FluentMap/Configuration/FluentConventionConfiguration.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 |
---|---|---|
@@ -0,0 +1,142 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Dapper.FluentMap.Conventions; | ||
using Dapper.FluentMap.Mapping; | ||
using Dapper.FluentMap.Utils; | ||
|
||
namespace Dapper.FluentMap.Configuration | ||
{ | ||
/// <summary> | ||
/// Defines methods for configuring conventions. | ||
/// </summary> | ||
public class FluentConventionConfiguration | ||
{ | ||
private readonly Convention _convention; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FluentConventionConfiguration"/> class, | ||
/// allowing configuration of conventions. | ||
/// </summary> | ||
/// <param name="convention">The convention.</param> | ||
public FluentConventionConfiguration(Convention convention) | ||
{ | ||
_convention = convention; | ||
} | ||
|
||
/// <summary> | ||
/// Configures the current covention for the specified entity type. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the entity.</typeparam> | ||
/// <returns>The current instance of <see cref="T:Dapper.FluentMap.Conventions.FluentMapConventionConfiguration"/>.</returns> | ||
public FluentConventionConfiguration ForEntity<T>() | ||
{ | ||
Type type = typeof (T); | ||
MapProperties(type); | ||
|
||
FluentMapper.TypeConventions.AddOrUpdate(type, _convention); | ||
FluentMapper.AddConventionTypeMap<T>(); | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Configures the current convention for all the entities in current assembly filtered by the specified namespaces. | ||
/// </summary> | ||
/// <param name="namespaces">An array of namespaces which filter the types in the current assembly. This parameter is optional.</param> | ||
/// <returns>The current instance of <see cref="T:Dapper.FluentMap.Conventions.FluentMapConventionConfiguration"/>.</returns> | ||
public FluentConventionConfiguration ForEntitiesInCurrentAssembly(params string[] namespaces) | ||
{ | ||
foreach (var type in Assembly.GetCallingAssembly() | ||
.GetExportedTypes() | ||
.Where(type => namespaces.Length == 0 || namespaces.Any(n => type.Namespace == n))) | ||
{ | ||
MapProperties(type); | ||
FluentMapper.TypeConventions.AddOrUpdate(type, _convention); | ||
FluentMapper.AddConventionTypeMap(type); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Configures the current convention for all entities in the specified assembly filtered by the specified namespaces. | ||
/// </summary> | ||
/// <param name="assembly">The assembly to scan for entities.</param> | ||
/// <param name="namespaces">An array of namespaces which filter the types in <paramref name="assembly"/>. This parameter is optional.</param> | ||
/// <returns>The current instance of <see cref="T:Dapper.FluentMap.Conventions.FluentMapConventionConfiguration"/>.</returns> | ||
public FluentConventionConfiguration ForEntitiesInAssembly(Assembly assembly, params string[] namespaces) | ||
{ | ||
foreach (var type in assembly.GetExportedTypes().Where(t => namespaces.Any(n => n.Contains(t.Namespace)))) | ||
{ | ||
MapProperties(type); | ||
FluentMapper.TypeConventions.AddOrUpdate(type, _convention); | ||
FluentMapper.AddConventionTypeMap(type); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
private void MapProperties(Type type) | ||
{ | ||
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); | ||
|
||
foreach (var property in properties) | ||
{ | ||
// Find the convention configurations for the convetion with either none or matching property predicates. | ||
foreach (var config in _convention.ConventionConfigurations | ||
.Where(c => c.PropertyPredicates.Count <= 0 || | ||
c.PropertyPredicates.All(e => e(property)))) | ||
{ | ||
if (!string.IsNullOrEmpty(config.Config.ColumnName)) | ||
{ | ||
AddConventionPropertyMap(property, config.Config.ColumnName); | ||
break; | ||
} | ||
|
||
if (!string.IsNullOrEmpty(config.Config.Prefix)) | ||
{ | ||
AddConventionPropertyMap(property, config.Config.Prefix + property.Name); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void AddConventionPropertyMap(PropertyInfo property, string columnName) | ||
{ | ||
var map = new PropertyMap(property, columnName); | ||
_convention.PropertyMaps.Add(map); | ||
} | ||
|
||
#region EditorBrowsableStates | ||
/// <inheritdoc/> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public override string ToString() | ||
{ | ||
return base.ToString(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public override bool Equals(object obj) | ||
{ | ||
return base.Equals(obj); | ||
} | ||
|
||
/// <inheritdoc/> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public override int GetHashCode() | ||
{ | ||
return base.GetHashCode(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public new Type GetType() | ||
{ | ||
return base.GetType(); | ||
} | ||
#endregion | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/Dapper.FluentMap/Configuration/FluentMapConfiguration.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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using Dapper.FluentMap.Conventions; | ||
using Dapper.FluentMap.Mapping; | ||
|
||
namespace Dapper.FluentMap.Configuration | ||
{ | ||
/// <summary> | ||
/// Defines methods for configuring Dapper.FluentMap. | ||
/// </summary> | ||
public class FluentMapConfiguration | ||
{ | ||
/// <summary> | ||
/// Adds the specified <see cref="T:Dapper.FluentMap.Mapping.EntityMap"/> to the configuration of Dapper.FluentMap. | ||
/// </summary> | ||
/// <typeparam name="TEntity">The type argument of the entity.</typeparam> | ||
/// <param name="mapper">An instance of the EntityMap classs containing the entity mapping configuration.</param> | ||
public void AddMap<TEntity>(EntityMap<TEntity> mapper) where TEntity : class | ||
{ | ||
FluentMapper.EntityMappers.Add(typeof (TEntity), mapper); | ||
FluentMapper.AddTypeMap<TEntity>(); | ||
} | ||
|
||
/// <summary> | ||
/// Adds the specified <see cref="T:Dapper.FluentMap.Conventions.Convention"/> to the configuration of Dapper.FluentMap. | ||
/// </summary> | ||
/// <param name="convention">An instance of the <see cref="T:Dapper.FluentMap.Conventions.Convention"/> class.</param> | ||
/// <returns> | ||
/// An instance of <see cref="T:Dapper.FluentMap.Conventions.FluentMapConventionConfiguration"/> | ||
/// which allows configuration of the convention. | ||
/// </returns> | ||
public FluentConventionConfiguration AddConvention(Convention convention) | ||
{ | ||
return new FluentConventionConfiguration(convention); | ||
} | ||
|
||
#region EditorBrowsableStates | ||
/// <inheritdoc/> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public override string ToString() | ||
{ | ||
return base.ToString(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public override bool Equals(object obj) | ||
{ | ||
return base.Equals(obj); | ||
} | ||
|
||
/// <inheritdoc/> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public override int GetHashCode() | ||
{ | ||
return base.GetHashCode(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public new Type GetType() | ||
{ | ||
return base.GetType(); | ||
} | ||
#endregion | ||
} | ||
} |
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,83 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using Dapper.FluentMap.Mapping; | ||
|
||
namespace Dapper.FluentMap.Conventions | ||
{ | ||
/// <summary> | ||
/// Represents a convention for mapping entity properties to column names. | ||
/// </summary> | ||
public abstract class Convention | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="T:Dapper.FluentMap.Conventions.Convention"/> class. | ||
/// </summary> | ||
protected Convention() | ||
{ | ||
ConventionConfigurations = new List<PropertyConventionConfiguration>(); | ||
PropertyMaps = new List<PropertyMap>(); | ||
} | ||
|
||
internal IList<PropertyConventionConfiguration> ConventionConfigurations { get; private set; } | ||
|
||
internal IList<PropertyMap> PropertyMaps { get; private set; } | ||
|
||
/// <summary> | ||
/// Configures a convention that applies on all properties of the entity. | ||
/// </summary> | ||
/// <returns>A configuration object for the convention.</returns> | ||
protected PropertyConventionConfiguration Properties() | ||
{ | ||
var config = new PropertyConventionConfiguration(); | ||
ConventionConfigurations.Add(config); | ||
|
||
return config; | ||
} | ||
|
||
/// <summary> | ||
/// Configures a convention that applies on all the properties of a specified type of the entity. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the properties that the convention will apply to.</typeparam> | ||
/// <returns>A configuration object for the convention.</returns> | ||
protected PropertyConventionConfiguration Properties<T>() | ||
{ | ||
// Get the underlying type for a nullale type. (int? -> int) | ||
Type underlyingType = Nullable.GetUnderlyingType(typeof (T)) ?? typeof (T); | ||
var config = new PropertyConventionConfiguration().Where(p => p.PropertyType == underlyingType); | ||
ConventionConfigurations.Add(config); | ||
|
||
return config; | ||
} | ||
|
||
#region EditorBrowsableStates | ||
/// <inheritdoc/> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public override string ToString() | ||
{ | ||
return base.ToString(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public override bool Equals(object obj) | ||
{ | ||
return base.Equals(obj); | ||
} | ||
|
||
/// <inheritdoc/> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public override int GetHashCode() | ||
{ | ||
return base.GetHashCode(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public new Type GetType() | ||
{ | ||
return base.GetType(); | ||
} | ||
#endregion | ||
} | ||
} |
Oops, something went wrong.