Permalink
Browse files

More work on mapping :)

Signed-off-by: MarcusTheBold <marcus.bratton@gmail.com>
  • Loading branch information...
1 parent 4060ff6 commit a7bae9c1b71be0d4b6cc1465615db92d40639469 @MarcusTheBold MarcusTheBold committed Apr 5, 2011
Showing with 120 additions and 73 deletions.
  1. +14 −6 Siege.Provisions/Siege.Provisions.Tests/MappingTests/ConventionTests.cs
  2. +14 −6 Siege.Provisions/Siege.Provisions/Mapping/Conventions/ClassConvention.cs
  3. +6 −5 Siege.Provisions/Siege.Provisions/Mapping/Conventions/ComponentConvention.cs
  4. +0 −12 Siege.Provisions/Siege.Provisions/Mapping/Conventions/ForeignKeyConvention.cs
  5. +19 −0 Siege.Provisions/Siege.Provisions/Mapping/Conventions/Formatters/KeyFormatter.cs
  6. +5 −2 Siege.Provisions/Siege.Provisions/Mapping/Conventions/Handlers/EntityHandler.cs
  7. +5 −2 Siege.Provisions/Siege.Provisions/Mapping/Conventions/Handlers/IDHandler.cs
  8. +11 −3 Siege.Provisions/Siege.Provisions/Mapping/Conventions/Handlers/OneToManyHandler.cs
  9. +15 −0 Siege.Provisions/Siege.Provisions/Mapping/Conventions/Identifiers/GenericListIdentifier.cs
  10. +5 −4 Siege.Provisions/Siege.Provisions/Mapping/DomainMapping.cs
  11. +0 −7 Siege.Provisions/Siege.Provisions/Mapping/IComponentPropertyMapping.cs
  12. +4 −8 Siege.Provisions/Siege.Provisions/Mapping/PropertyMappings/ComponentMapping.cs
  13. +5 −2 Siege.Provisions/Siege.Provisions/Mapping/PropertyMappings/ForeignRelationshipMapping.cs
  14. +3 −1 Siege.Provisions/Siege.Provisions/Mapping/PropertyMappings/IdMapping.cs
  15. +6 −2 Siege.Provisions/Siege.Provisions/Mapping/PropertyMappings/PropertyMapping.cs
  16. +2 −2 Siege.Provisions/Siege.Provisions/Siege.Provisions.csproj
  17. +6 −11 Siege.Requisitions/Siege.Requisitions.UnitTests/MultiRegistrationTests.cs
@@ -21,7 +21,7 @@ public void ShouldAutoMap()
{
mapper.UseConvention(convention =>
{
- convention.WithSchema("test");
+ convention.WithSchema("siege");
convention.WithSuffix("s");
convention.IdentifyEntitiesWith<EntityIdentifier>();
@@ -30,11 +30,19 @@ public void ShouldAutoMap()
convention.ForComponents(component =>
{
- component.PrefixWith((type, propertyName) => type.Name + "_");
- component.SuffixWith((type, propertyName) => "_" + type.Name);
+ component.PrefixWith((parentType, objectType, propertyName) => parentType.Name);
+ component.SuffixWith((parentType, objectType, propertyName) => objectType.Name);
});
- convention.ForForeignKeys(key => {});
+ convention.ForPrimaryKeys(key =>
+ {
+ key.FormatAs(type => type.Name + "ID");
+ });
+
+ convention.ForForeignKeys(key =>
+ {
+ key.FormatAs(property => property.Name + "ID");
+ });
});
mapper.Add<Customer>();
@@ -44,7 +52,7 @@ public void ShouldAutoMap()
});
Assert.AreEqual("Customers", map.Mappings.For<Customer>().Table.Name);
- Assert.AreEqual("test", map.Mappings.For<Customer>().Table.Schema);
+ Assert.AreEqual("siege", map.Mappings.For<Customer>().Table.Schema);
}
[Test]
@@ -54,7 +62,7 @@ public void ShouldAllowOverriding()
{
mapper.UseConvention(convention =>
{
- convention.WithSchema("test");
+ convention.WithSchema("siege");
convention.WithSuffix("s");
});
@@ -1,13 +1,16 @@
using System;
using System.Collections.Generic;
using System.Reflection;
+using Siege.Provisions.Mapping.Conventions.Formatters;
using Siege.Provisions.Mapping.Conventions.Handlers;
using Siege.Provisions.Mapping.Conventions.Identifiers;
namespace Siege.Provisions.Mapping.Conventions
{
public class ClassConvention : IConvention
{
+ private readonly Formatter<Type> primaryKeyFormatter = new Formatter<Type>();
+ private readonly Formatter<PropertyInfo> foreignKeyFormatter = new Formatter<PropertyInfo>();
private readonly ComponentHandler componentHandler = new ComponentHandler();
private readonly List<IHandler> handlers = new List<IHandler>();
@@ -40,7 +43,7 @@ public void Map(Type type, DomainMapping mapper)
{
this.handlers.Add(componentHandler);
this.handlers.Add(new PropertyHandler());
- this.handlers.Add(new OneToManyHandler());
+ this.handlers.Add(new OneToManyHandler(foreignKeyFormatter));
foreach(var property in type.GetProperties())
{
@@ -65,12 +68,12 @@ public void ForComponents(Action<ComponentConvention> convention)
public void IdentifyEntitiesWith<TIdentifier>() where TIdentifier : class, IIdentifier<Type>, new()
{
- this.handlers.Add(new EntityHandler(new TIdentifier()));
+ this.handlers.Add(new EntityHandler(new TIdentifier(), foreignKeyFormatter));
}
public void IdentifyEntitiesWith(Predicate<Type> entityMatcher)
{
- this.handlers.Add(new EntityHandler(new InlineIdentifier<Type>(entityMatcher)));
+ this.handlers.Add(new EntityHandler(new InlineIdentifier<Type>(entityMatcher), foreignKeyFormatter));
}
public void IdentifyComponentsWith<TIdentifier>() where TIdentifier : class, IIdentifier<Type>, new()
@@ -85,17 +88,22 @@ public void IdentifyComponentsWith(Predicate<Type> componentMatcher)
public void IdentifyIDsWith(Predicate<PropertyInfo> idMatcher)
{
- this.handlers.Add(new IDHandler(new InlineIdentifier<PropertyInfo>(idMatcher)));
+ this.handlers.Add(new IDHandler(new InlineIdentifier<PropertyInfo>(idMatcher), primaryKeyFormatter));
}
public void IdentifyIDsWith<TIdentifier>() where TIdentifier : class, IIdentifier<PropertyInfo>, new()
{
- this.handlers.Add(new IDHandler(new TIdentifier()));
+ this.handlers.Add(new IDHandler(new TIdentifier(), primaryKeyFormatter));
}
- public void ForForeignKeys(Action<ForeignKeyConvention> foreignKey)
+ public void ForForeignKeys(Action<Formatter<PropertyInfo>> foreignKey)
{
+ foreignKey(foreignKeyFormatter);
+ }
+ public void ForPrimaryKeys(Action<Formatter<Type>> primaryKey)
+ {
+ primaryKey(primaryKeyFormatter);
}
}
}
@@ -6,21 +6,21 @@ namespace Siege.Provisions.Mapping.Conventions
public class ComponentConvention : IConvention
{
private readonly PropertyInfo property;
- protected Func<Type, string, string> prefix;
- protected Func<Type, string, string> suffix;
+ protected Func<Type, Type, string, string> prefix;
+ protected Func<Type, Type, string, string> suffix;
public ComponentConvention(PropertyInfo property)
{
this.property = property;
}
- public ComponentConvention PrefixWith(Func<Type, string, string> formatter)
+ public ComponentConvention PrefixWith(Func<Type, Type, string, string> formatter)
{
this.prefix = formatter;
return this;
}
- public ComponentConvention SuffixWith(Func<Type, string, string> suffix)
+ public ComponentConvention SuffixWith(Func<Type, Type, string, string> suffix)
{
this.suffix = suffix;
return this;
@@ -32,7 +32,8 @@ public void Map(Type type, DomainMapping mapper)
{
foreach (var typeProperty in this.property.PropertyType.GetProperties())
{
- subMapping.MapProperty(typeProperty);
+ string propertyName = this.prefix(type, property.PropertyType, typeProperty.Name) + typeProperty.Name + this.suffix(type, property.PropertyType, typeProperty.Name);
+ subMapping.MapProperty(typeProperty, propertyName);
}
});
}
@@ -1,12 +0,0 @@
-using System;
-
-namespace Siege.Provisions.Mapping.Conventions
-{
- public class ForeignKeyConvention : IConvention
- {
- public void Map(Type type, DomainMapping mapper)
- {
-
- }
- }
-}
@@ -0,0 +1,19 @@
+using System;
+
+namespace Siege.Provisions.Mapping.Conventions.Formatters
+{
+ public class Formatter<T>
+ {
+ private Func<T, string> formatter;
+
+ public void FormatAs(Func<T, string> formatter)
+ {
+ this.formatter = formatter;
+ }
+
+ public string Format(T property)
+ {
+ return this.formatter(property);
+ }
+ }
+}
@@ -1,16 +1,19 @@
using System;
using System.Reflection;
+using Siege.Provisions.Mapping.Conventions.Formatters;
using Siege.Provisions.Mapping.Conventions.Identifiers;
namespace Siege.Provisions.Mapping.Conventions.Handlers
{
public class EntityHandler : IHandler
{
private readonly IIdentifier<Type> entityIdentifier;
+ private readonly Formatter<PropertyInfo> foreignKeyFormatter;
- public EntityHandler(IIdentifier<Type> entityIdentifier)
+ public EntityHandler(IIdentifier<Type> entityIdentifier, Formatter<PropertyInfo> foreignKeyFormatter)
{
this.entityIdentifier = entityIdentifier;
+ this.foreignKeyFormatter = foreignKeyFormatter;
}
public bool CanHandle(PropertyInfo property)
@@ -20,7 +23,7 @@ public bool CanHandle(PropertyInfo property)
public void Handle(PropertyInfo property, Type type, DomainMapping mapper)
{
- mapper.MapForeignRelationship(property, property.PropertyType);
+ mapper.MapForeignRelationship(property, property.PropertyType, foreignKeyFormatter);
}
}
}
@@ -1,16 +1,19 @@
using System;
using System.Reflection;
+using Siege.Provisions.Mapping.Conventions.Formatters;
using Siege.Provisions.Mapping.Conventions.Identifiers;
namespace Siege.Provisions.Mapping.Conventions.Handlers
{
public class IDHandler : IHandler
{
private readonly IIdentifier<PropertyInfo> idIdentifier;
+ private readonly Formatter<Type> primaryKeyFormatter;
- public IDHandler(IIdentifier<PropertyInfo> idIdentifier)
+ public IDHandler(IIdentifier<PropertyInfo> idIdentifier, Formatter<Type> primaryKeyFormatter)
{
this.idIdentifier = idIdentifier;
+ this.primaryKeyFormatter = primaryKeyFormatter;
}
public bool CanHandle(PropertyInfo property)
@@ -20,7 +23,7 @@ public bool CanHandle(PropertyInfo property)
public void Handle(PropertyInfo property, Type type, DomainMapping mapper)
{
- mapper.MapID(property);
+ mapper.MapID(property, type, primaryKeyFormatter);
}
}
}
@@ -1,20 +1,28 @@
using System;
-using System.Collections;
using System.Linq;
using System.Reflection;
+using Siege.Provisions.Mapping.Conventions.Formatters;
+using Siege.Provisions.Mapping.Conventions.Identifiers;
namespace Siege.Provisions.Mapping.Conventions.Handlers
{
public class OneToManyHandler : IHandler
{
+ private readonly Formatter<PropertyInfo> foreignKeyFormatter;
+
+ public OneToManyHandler(Formatter<PropertyInfo> foreignKeyFormatter)
+ {
+ this.foreignKeyFormatter = foreignKeyFormatter;
+ }
+
public bool CanHandle(PropertyInfo property)
{
- return property.PropertyType.IsGenericType && property.PropertyType.GetInterfaces().Where(i => i == typeof (IEnumerable)).Count() > 0;
+ return new GenericListIdentifier().Matches(property.PropertyType);
}
public void Handle(PropertyInfo property, Type type, DomainMapping mapper)
{
- mapper.MapForeignRelationship(property, property.PropertyType.GetGenericArguments().First());
+ mapper.MapForeignRelationship(property, property.PropertyType.GetGenericArguments().First(), foreignKeyFormatter);
}
}
}
@@ -0,0 +1,15 @@
+using System;
+using System.Collections;
+using System.Linq;
+
+namespace Siege.Provisions.Mapping.Conventions.Identifiers
+{
+ public class GenericListIdentifier : IIdentifier<Type>
+ {
+ public bool Matches(Type type)
+ {
+ return type.IsGenericType &&
+ type.GetInterfaces().Where(i => i == typeof (IEnumerable)).Count() > 0;
+ }
+ }
+}
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
+using Siege.Provisions.Mapping.Conventions.Formatters;
using Siege.Provisions.Mapping.PropertyMappings;
namespace Siege.Provisions.Mapping
@@ -91,9 +92,9 @@ public DomainMapping MapComponent(PropertyInfo propertyInfo, Action<ComponentMap
return this;
}
- public DomainMapping MapID(PropertyInfo property)
+ public DomainMapping MapID(PropertyInfo property, Type type, Formatter<Type> keyFormatter)
{
- var id = new IdMapping(property);
+ var id = new IdMapping(property, type, keyFormatter);
this.subMappings.Add(id);
return this;
@@ -104,9 +105,9 @@ public void Map(Action<DomainMapping> mapping)
mapping(this);
}
- public DomainMapping MapForeignRelationship(PropertyInfo property, Type type)
+ public DomainMapping MapForeignRelationship(PropertyInfo property, Type type, Formatter<PropertyInfo> keyFormatter)
{
- var foreignRelationshipMapping = new ForeignRelationshipMapping(property, type);
+ var foreignRelationshipMapping = new ForeignRelationshipMapping(property, type, keyFormatter);
this.subMappings.Add(foreignRelationshipMapping);
return this;
@@ -1,7 +0,0 @@
-namespace Siege.Provisions.Mapping
-{
- public interface IComponentPropertyMapping : IElementMapping
- {
- string Name { get; }
- }
-}
@@ -5,7 +5,7 @@
namespace Siege.Provisions.Mapping.PropertyMappings
{
- public class ComponentMapping<TClass, TType> : ElementMapping<TClass, TType>, IComponentPropertyMapping
+ public class ComponentMapping<TClass, TType> : ElementMapping<TClass, TType>
{
private readonly List<IElementMapping> subMappings = new List<IElementMapping>();
private readonly string name;
@@ -36,22 +36,18 @@ public ComponentMapping(Expression<Func<TClass, TType>> expression) : base(expre
}
}
- public class ComponentMapping : ElementMapping, IComponentPropertyMapping
+ public class ComponentMapping : ElementMapping
{
public ComponentMapping(PropertyInfo property) : base(property)
{
- this.name = Property.Name;
}
private readonly List<IElementMapping> subMappings = new List<IElementMapping>();
- private readonly string name;
-
- public string Name { get { return name; } }
public List<IElementMapping> SubMappings { get { return subMappings; } }
- public ComponentMapping MapProperty(PropertyInfo property)
+ public ComponentMapping MapProperty(PropertyInfo property, string name)
{
- this.subMappings.Add(new PropertyMapping(property));
+ this.subMappings.Add(new PropertyMapping(property, name));
return this;
}
}
@@ -1,15 +1,18 @@
using System;
using System.Reflection;
+using Siege.Provisions.Mapping.Conventions.Formatters;
namespace Siege.Provisions.Mapping.PropertyMappings
{
- public class ForeignRelationshipMapping : ElementMapping
+ public class ForeignRelationshipMapping : PropertyMapping
{
private readonly Type type;
- public ForeignRelationshipMapping(PropertyInfo property, Type type) : base(property)
+ public ForeignRelationshipMapping(PropertyInfo property, Type type, Formatter<PropertyInfo> keyFormatter) : base(property)
{
this.type = type;
+
+ this.ColumnName = keyFormatter.Format(property);
}
}
}
Oops, something went wrong.

0 comments on commit a7bae9c

Please sign in to comment.