Permalink
Browse files

Added support for property-less Id mappings.

  • Loading branch information...
jagregory committed Aug 28, 2009
1 parent 587fa69 commit 89c7525e8db68ca5e558ffb80fd16983eacf8b03
@@ -1,4 +1,5 @@
using System.Linq;
+using FluentNHibernate.MappingModel;
using FluentNHibernate.MappingModel.ClassBased;
using FluentNHibernate.MappingModel.Identity;
using FluentNHibernate.Testing.DomainModel;
@@ -199,6 +200,21 @@ public void IdShouldSetIdPropertyOnModel()
.ModelShouldMatch(x => x.Id.ShouldBeOfType<IdMapping>());
}
+ [Test]
+ public void IdWithoutPropertyShouldSetIdPropertyOnModel()
+ {
+ ClassMap<PropertyTarget>()
+ .Mapping(m => m.Id<int>("id"))
+ .ModelShouldMatch(m =>
+ {
+ var id = m.Id as IdMapping;
+
+ id.ShouldNotBeNull();
+ id.Type.ShouldEqual(new TypeReference(typeof(int)));
+ id.Columns.ShouldContain(x => x.Name == "id");
+ });
+ }
+
[Test]
public void CompositeIdShouldSetIdPropertyOnModel()
{
@@ -198,6 +198,18 @@ public virtual IdentityPart Id(Expression<Func<T, object>> expression, string co
return part;
}
+ public virtual IdentityPart Id<TColumn>(string column)
+ {
+ var part = new IdentityPart(typeof(T), typeof(TColumn), column);
+
+ if (column != null)
+ part.Column(column);
+
+ id = part;
+
+ return part;
+ }
+
[Obsolete("Inline definitions of subclasses are depreciated. Please create a derived class from SubclassMap in the same way you do with ClassMap.")]
public virtual void JoinedSubClass<TSubclass>(string keyColumn, Action<JoinedSubClassPart<TSubclass>> action) where TSubclass : T
{
@@ -1,42 +1,59 @@
using System;
using System.Collections.Generic;
-using System.Diagnostics;
using System.Reflection;
using FluentNHibernate.Mapping.Providers;
using FluentNHibernate.MappingModel;
using FluentNHibernate.MappingModel.Identity;
+using System.Diagnostics;
namespace FluentNHibernate.Mapping
{
public class IdentityPart : IIdentityMappingProvider
{
private readonly AttributeStore<ColumnMapping> columnAttributes = new AttributeStore<ColumnMapping>();
private readonly IList<string> columns = new List<string>();
- private readonly PropertyInfo property;
+ private readonly PropertyInfo property;
private readonly Type entityType;
private readonly AccessStrategyBuilder<IdentityPart> access;
private readonly AttributeStore<IdMapping> attributes = new AttributeStore<IdMapping>();
+ private readonly Type identityType;
private bool nextBool = true;
+ private readonly string columnName;
public IdentityPart(Type entity, PropertyInfo property)
- {
+ {
this.property = property;
entityType = entity;
+ this.identityType = property.PropertyType;
+ this.columnName = property.Name;
access = new AccessStrategyBuilder<IdentityPart>(this, value => attributes.Set(x => x.Access, value));
GeneratedBy = new IdentityGenerationStrategyBuilder<IdentityPart>(this, property.PropertyType, entity);
SetDefaultGenerator();
- }
+ }
+
+ public IdentityPart(Type entity, Type identityType, string columnName)
+ {
+ this.property = null;
+ this.entityType = entity;
+ this.identityType = identityType;
+ this.columnName = columnName;
+
+ access = new AccessStrategyBuilder<IdentityPart>(this, value => attributes.Set(x => x.Access, value));
+ GeneratedBy = new IdentityGenerationStrategyBuilder<IdentityPart>(this, this.identityType, entity);
+
+ SetDefaultGenerator();
+ }
private void SetDefaultGenerator()
{
var generatorMapping = new GeneratorMapping();
- var defaultGenerator = new GeneratorBuilder(generatorMapping, property.PropertyType);
+ var defaultGenerator = new GeneratorBuilder(generatorMapping, identityType);
- if (property.PropertyType == typeof(Guid))
+ if (identityType == typeof(Guid))
defaultGenerator.GuidComb();
- else if (property.PropertyType == typeof(int) || property.PropertyType == typeof(long))
+ else if (identityType == typeof(int) || identityType == typeof(long))
defaultGenerator.Identity();
else
defaultGenerator.Assigned();
@@ -54,13 +71,16 @@ IdMapping IIdentityMappingProvider.GetIdentityMapping()
if (columns.Count > 0)
{
foreach (var column in columns)
- mapping.AddColumn(new ColumnMapping(columnAttributes.CloneInner()) {Name = column});
+ mapping.AddColumn(new ColumnMapping(columnAttributes.CloneInner()) { Name = column });
}
else
- mapping.AddDefaultColumn(new ColumnMapping(columnAttributes.CloneInner()) { Name = property.Name });
+ mapping.AddDefaultColumn(new ColumnMapping(columnAttributes.CloneInner()) { Name = columnName });
- mapping.Name = property.Name;
- mapping.SetDefaultValue("Type", new TypeReference(property.PropertyType));
+ if (property != null)
+ {
+ mapping.Name = columnName;
+ }
+ mapping.SetDefaultValue("Type", new TypeReference(identityType));
if (GeneratedBy.IsDirty)
mapping.Generator = GeneratedBy.GetGeneratorMapping();
@@ -74,9 +94,9 @@ IdMapping IIdentityMappingProvider.GetIdentityMapping()
/// Set the access and naming strategy for this identity.
/// </summary>
public AccessStrategyBuilder<IdentityPart> Access
- {
- get { return access; }
- }
+ {
+ get { return access; }
+ }
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public IdentityPart Not
@@ -189,4 +209,4 @@ public IdentityPart CustomType(string type)
return this;
}
}
-}
+}

0 comments on commit 89c7525

Please sign in to comment.