-
Notifications
You must be signed in to change notification settings - Fork 935
Add in ByCode support of all type mappings on Id #1848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,45 @@ | ||
using System; | ||
using NHibernate.Mapping.ByCode.Impl; | ||
using NHibernate.Type; | ||
using NHibernate.Util; | ||
|
||
namespace NHibernate.Mapping.ByCode | ||
{ | ||
// 6.0 TODO: move into IIdMapper, | ||
// and probably add an ITypeMapper for mutualizing the four Type methods with IElementMapper and IPropertyMapper | ||
// (Note that there is no IKeyPropertyMapper to be concerned with, the KeyPropertyMapper use IPropertyMapper | ||
// directly instead.) | ||
public static class IdMapperExtensions | ||
{ | ||
public static void Type<TPersistentType>(this IIdMapper idMapper) where TPersistentType : IIdentifierType | ||
{ | ||
ReflectHelper | ||
.CastOrThrow<IdMapper>(idMapper, "generic Type method") | ||
.Type<TPersistentType>(); | ||
} | ||
|
||
public static void Type<TPersistentType>(this IIdMapper idMapper, object parameters) where TPersistentType : IIdentifierType | ||
{ | ||
ReflectHelper | ||
.CastOrThrow<IdMapper>(idMapper, "generic parameterized Type method") | ||
.Type<TPersistentType>(parameters); | ||
} | ||
|
||
public static void Type(this IIdMapper idMapper, System.Type persistentType, object parameters) | ||
{ | ||
ReflectHelper | ||
.CastOrThrow<IdMapper>(idMapper, "parameterized Type method") | ||
.Type(persistentType, parameters); | ||
} | ||
} | ||
|
||
public interface IIdMapper : IAccessorPropertyMapper, IColumnsMapper | ||
{ | ||
void Generator(IGeneratorDef generator); | ||
void Generator(IGeneratorDef generator, Action<IGeneratorMapper> generatorMapping); | ||
|
||
void Type(IIdentifierType persistentType); | ||
//void Type<TPersistentType>() where TPersistentType : IIdentifierType; | ||
//void Type<TPersistentType>(object parameters) where TPersistentType : IIdentifierType; | ||
//void Type(System.System.Type persistentType, object parameters); | ||
//void Column(Action<IColumnMapper> columnMapper); | ||
//void Columns(params Action<IColumnMapper>[] columnMapper); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The column methods are not lacking, they are here through inherited |
||
void UnsavedValue(object value); | ||
void Length(int length); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
using System.Reflection; | ||
using NHibernate.Cfg.MappingSchema; | ||
using NHibernate.Type; | ||
using NHibernate.UserTypes; | ||
|
||
namespace NHibernate.Mapping.ByCode.Impl | ||
{ | ||
|
@@ -66,6 +67,48 @@ public void Type(IIdentifierType persistentType) | |
} | ||
} | ||
|
||
public void Type<TPersistentType>() | ||
{ | ||
Type(typeof (TPersistentType), null); | ||
} | ||
|
||
public void Type<TPersistentType>(object parameters) | ||
{ | ||
Type(typeof (TPersistentType), parameters); | ||
} | ||
|
||
public void Type(System.Type persistentType, object parameters) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In a(!) future I would like to have a method which accepts |
||
{ | ||
if (persistentType == null) | ||
{ | ||
throw new ArgumentNullException(nameof(persistentType)); | ||
} | ||
if (!typeof (IUserType).IsAssignableFrom(persistentType) && !typeof (IType).IsAssignableFrom(persistentType) && !typeof (ICompositeUserType).IsAssignableFrom(persistentType)) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(persistentType), "Expected type implementing IUserType, ICompositeUserType or IType."); | ||
} | ||
if (parameters != null) | ||
{ | ||
hbmId.type1 = null; | ||
var hbmType = new HbmType | ||
{ | ||
name = persistentType.AssemblyQualifiedName, | ||
param = (from pi in parameters.GetType().GetProperties() | ||
let pname = pi.Name | ||
let pvalue = pi.GetValue(parameters, null) | ||
select | ||
new HbmParam {name = pname, Text = new[] {ReferenceEquals(pvalue, null) ? "null" : pvalue.ToString()}}) | ||
.ToArray() | ||
}; | ||
hbmId.type = hbmType; | ||
} | ||
else | ||
{ | ||
hbmId.type1 = persistentType.AssemblyQualifiedName; | ||
hbmId.type = null; | ||
} | ||
} | ||
|
||
public void UnsavedValue(object value) | ||
{ | ||
hbmId.unsavedvalue = value != null ? value.ToString() : "null"; | ||
|
@@ -182,4 +225,4 @@ public void Access(System.Type accessorType) {} | |
|
||
#endregion | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it the only method that needs to be added to interface? I think it's better to keep other methods as extensions and inline them.