Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
Added column and key property resolver. Issue #12.
Browse files Browse the repository at this point in the history
  • Loading branch information
henkmollema committed Jun 24, 2014
1 parent 1282626 commit 9e6c0b2
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/Dapper.FluentMap.Dommel/Dapper.FluentMap.Dommel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
<Compile Include="Mapping\DommelEntityMap.cs" />
<Compile Include="Mapping\DommelPropertyMap.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Resolvers\DommelColumnNameResolver.cs" />
<Compile Include="Resolvers\DommelKeyPropertyResolver.cs" />
<Compile Include="Resolvers\DommelTableNameResolver.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
using Dapper.FluentMap.Configuration;
using Dapper.FluentMap.Dommel.Resolvers;

using DommelMapper = Dommel.Dommel;

namespace Dapper.FluentMap.Dommel
{
public static class FluentMapConfigurationExtensions
{
public static FluentMapConfiguration ForDommel(this FluentMapConfiguration config)
{
DommelMapper.SetColumnNameResolver(new DommelColumnNameResolver());
DommelMapper.SetKeyPropertyResolver(new DommelKeyPropertyResolver());
DommelMapper.SetTableNameResolver(new DommelTableNameResolver());
return config;
}
}
Expand Down
39 changes: 39 additions & 0 deletions src/Dapper.FluentMap.Dommel/Resolvers/DommelColumnNameResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Linq;
using System.Reflection;

using Dapper.FluentMap.Dommel.Mapping;

using DommelMapper = Dommel.Dommel;

namespace Dapper.FluentMap.Dommel.Resolvers
{
public class DommelColumnNameResolver : DommelMapper.IColumnNameResolver
{
public string ResolveColumnName(PropertyInfo propertyInfo)
{
var entityMap = FluentMapper.EntityMappers[propertyInfo.DeclaringType] as DommelEntityMap;

if (entityMap == null)
{
// todo: exception, null, fallback resolver or type.Name?
throw new Exception(string.Format("Could not find the mapping for type '{0}'.", propertyInfo.DeclaringType.FullName));
}

var propertyMaps = entityMap.PropertyMaps.Where(m => m.PropertyInfo.Name == propertyInfo.Name).ToList();

if (propertyMaps.Count == 0)
{
return null;
// todo
}

if (propertyMaps.Count > 1)
{
// todo
}

return propertyMaps[0].ColumnName;
}
}
}
43 changes: 43 additions & 0 deletions src/Dapper.FluentMap.Dommel/Resolvers/DommelKeyPropertyResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Linq;
using System.Reflection;

using Dapper.FluentMap.Dommel.Mapping;

using DommelMapper = Dommel.Dommel;

namespace Dapper.FluentMap.Dommel.Resolvers
{
public class DommelKeyPropertyResolver : DommelMapper.IKeyPropertyResolver
{
public PropertyInfo ResolveKeyProperty(Type type)
{
var entityMap = FluentMapper.EntityMappers[type] as DommelEntityMap;
if (entityMap == null)
{
// todo: exception, null, fallback resolver or type.Name?
throw new Exception(string.Format("Could not find the mapping for type '{0}'.", type.FullName));
}

var keyPropertyMaps = entityMap.PropertyMaps.OfType<DommelPropertyMap>().Where(e => e.Key).ToList();
if (keyPropertyMaps.Count == 0)
{
// todo: exception, null or fallback?
throw new Exception(string.Format("Could not find the key property for type '{0}'.", type.FullName));
}

if (keyPropertyMaps.Count > 1)
{
// todo: exception, null or fallback?
string msg = string.Format("Found multiple key properties on type '{0}'. This is not yet supported. The following key properties were found:{1}{2}",
type.FullName,
Environment.NewLine,
string.Join(Environment.NewLine, keyPropertyMaps.Select(t => t.PropertyInfo.Name)));

throw new Exception(msg);
}

return keyPropertyMaps[0].PropertyInfo;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System;

using Dapper.FluentMap.Dommel.Mapping;

using DommelMapper = Dommel.Dommel;

namespace Dapper.FluentMap.Dommel.Resolvers
{
public class DommelTableNameResolver : global::Dommel.Dommel.ITableNameResolver
public class DommelTableNameResolver : DommelMapper.ITableNameResolver
{
public string ResolveTableName(Type type)
{
var mapping = FluentMapper.EntityMappers[type] as DommelEntityMap;
DommelEntityMap mapping = FluentMapper.EntityMappers[type] as DommelEntityMap;
if (mapping == null)
{
// todo: exception, null, fallback resolver or type.Name?
Expand Down
4 changes: 2 additions & 2 deletions src/Dapper.FluentMap/Mapping/PropertyMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal PropertyMap(PropertyInfo info, string columnName)
/// <summary>
/// Gets the name of the column in the data store.
/// </summary>
internal string ColumnName { get; private set; }
public string ColumnName { get; private set; }

/// <summary>
/// Gets or sets a value indicating whether column name mapping should be case sensitive.
Expand All @@ -51,7 +51,7 @@ internal PropertyMap(PropertyInfo info, string columnName)
/// <summary>
/// Gets the <see cref="T:System.Reflection.PropertyInfo"/> object for the current property.
/// </summary>
internal PropertyInfo PropertyInfo { get; private set; }
public PropertyInfo PropertyInfo { get; private set; }

/// <summary>
/// Maps the current property to the specified column name.
Expand Down

0 comments on commit 9e6c0b2

Please sign in to comment.