Skip to content

Commit

Permalink
Added support for property-less Id mappings.
Browse files Browse the repository at this point in the history
  • Loading branch information
jagregory committed Aug 28, 2009
1 parent 587fa69 commit 89c7525
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 15 deletions.
@@ -1,4 +1,5 @@
using System.Linq;
using FluentNHibernate.MappingModel;
using FluentNHibernate.MappingModel.ClassBased;
using FluentNHibernate.MappingModel.Identity;
using FluentNHibernate.Testing.DomainModel;
Expand Down Expand Up @@ -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()
{
Expand Down
12 changes: 12 additions & 0 deletions src/FluentNHibernate/Mapping/ClassMap.cs
Expand Up @@ -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
{
Expand Down
50 changes: 35 additions & 15 deletions src/FluentNHibernate/Mapping/IdentityPart.cs
@@ -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();
Expand All @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -189,4 +209,4 @@ public IdentityPart CustomType(string type)
return this;
}
}
}
}

0 comments on commit 89c7525

Please sign in to comment.