diff --git a/src/NHibernate.Test/MappingByCode/GeneratorTests.cs b/src/NHibernate.Test/MappingByCode/GeneratorTests.cs new file mode 100644 index 00000000000..5182bc3282b --- /dev/null +++ b/src/NHibernate.Test/MappingByCode/GeneratorTests.cs @@ -0,0 +1,57 @@ +using System.Linq; +using NHibernate.Id; +using NHibernate.Mapping.ByCode; +using NHibernate.Mapping.ByCode.Impl; +using NUnit.Framework; +using SharpTestsEx; + +namespace NHibernate.Test.MappingByCode +{ + public class A { public int Id { get; set; } } + + public class B { public int Id { get; set; } } + + public class C { public int Id { get; set; } } + + public class D { public int Id { get; set; } } + + public class E { public int Id { get; set; } } + + public class F { public int Id { get; set; } } + + public class G { public int Id { get; set; } } + + public class H { public int Id { get; set; } } + + public class I { public int Id { get; set; } } + + public class GeneratorTests + { + [Test] + public void TestGenerators() + { + var mapper = new ModelMapper(); + + mapper.Class(e => { e.Id(c => c.Id, c => c.Generator(Generators.Counter)); }); + mapper.Class(e => { e.Id(c => c.Id, c => c.Generator(Generators.UUIDHex)); }); + mapper.Class(e => { e.Id(c => c.Id, c => c.Generator(Generators.UUIDString)); }); + mapper.Class(e => { e.Id(c => c.Id, c => c.Generator(Generators.Increment)); }); + mapper.Class(e => { e.Id(c => c.Id, c => c.Generator(Generators.Select)); }); + mapper.Class(e => { e.Id(c => c.Id, c => c.Generator(Generators.SequenceHiLo)); }); + mapper.Class(e => { e.Id(c => c.Id, c => c.Generator(Generators.SequenceIdentity)); }); + mapper.Class(e => { e.Id(c => c.Id, c => c.Generator(Generators.Table)); }); + mapper.Class(e => { e.Id(c => c.Id, c => c.Generator(Generators.TriggerIdentity)); }); + + var hbmMapping = mapper.CompileMappingForAllExplicitlyAddedEntities(); + Assert.AreEqual(hbmMapping.RootClasses.Single(x => x.Name == typeof(A).Name).Id.generator.@class, Generators.Counter.Class); + Assert.AreEqual(hbmMapping.RootClasses.Single(x => x.Name == typeof(B).Name).Id.generator.@class, Generators.UUIDHex.Class); + Assert.AreEqual(hbmMapping.RootClasses.Single(x => x.Name == typeof(C).Name).Id.generator.@class, Generators.UUIDString.Class); + Assert.AreEqual(hbmMapping.RootClasses.Single(x => x.Name == typeof(D).Name).Id.generator.@class, Generators.Increment.Class); + Assert.AreEqual(hbmMapping.RootClasses.Single(x => x.Name == typeof(E).Name).Id.generator.@class, Generators.Select.Class); + Assert.AreEqual(hbmMapping.RootClasses.Single(x => x.Name == typeof(F).Name).Id.generator.@class, Generators.SequenceHiLo.Class); + Assert.AreEqual(hbmMapping.RootClasses.Single(x => x.Name == typeof(G).Name).Id.generator.@class, Generators.SequenceIdentity.Class); + Assert.AreEqual(hbmMapping.RootClasses.Single(x => x.Name == typeof(H).Name).Id.generator.@class, Generators.Table.Class); + Assert.AreEqual(hbmMapping.RootClasses.Single(x => x.Name == typeof(I).Name).Id.generator.@class, Generators.TriggerIdentity.Class); + } + } +} \ No newline at end of file diff --git a/src/NHibernate.Test/NHibernate.Test.csproj b/src/NHibernate.Test/NHibernate.Test.csproj index 962022b5487..ad6d1b9ca10 100644 --- a/src/NHibernate.Test/NHibernate.Test.csproj +++ b/src/NHibernate.Test/NHibernate.Test.csproj @@ -590,6 +590,7 @@ + diff --git a/src/NHibernate/Mapping/ByCode/Generators.cs b/src/NHibernate/Mapping/ByCode/Generators.cs index 20b0f06f53c..74b47b117e6 100644 --- a/src/NHibernate/Mapping/ByCode/Generators.cs +++ b/src/NHibernate/Mapping/ByCode/Generators.cs @@ -17,6 +17,16 @@ static Generators() Assigned = new AssignedGeneratorDef(); EnhancedSequence = new EnhancedSequenceGeneratorDef(); EnhancedTable = new EnhancedTableGeneratorDef(); + Counter = new CounterGeneratorDef(); + Increment = new IncrementGeneratorDef(); + NativeGuid = new NativeGuidGeneratorDef(); + Select = new SelectGeneratorDef(); + SequenceHiLo = new SequenceHiLoGeneratorDef(); + SequenceIdentity = new SequenceIdentityGeneratorDef(); + Table = new TableGeneratorDef(); + TriggerIdentity = new TriggerIdentityGeneratorDef(); + UUIDHex = new UUIDHexGeneratorDef(); + UUIDString = new UUIDStringGeneratorDef(); } public static IGeneratorDef Assigned { get; private set; } @@ -28,6 +38,16 @@ static Generators() public static IGeneratorDef Identity { get; private set; } public static IGeneratorDef EnhancedSequence { get; private set; } public static IGeneratorDef EnhancedTable { get; private set; } + public static IGeneratorDef Counter { get; private set; } + public static IGeneratorDef Increment { get; private set; } + public static IGeneratorDef NativeGuid { get; private set; } + public static IGeneratorDef Select { get; private set; } + public static IGeneratorDef SequenceHiLo { get; private set; } + public static IGeneratorDef Table { get; private set; } + public static IGeneratorDef TriggerIdentity { get; private set; } + public static IGeneratorDef SequenceIdentity { get; private set; } + public static IGeneratorDef UUIDHex { get; private set; } + public static IGeneratorDef UUIDString { get; private set; } public static IGeneratorDef Foreign(Expression> property) { @@ -40,6 +60,303 @@ public static IGeneratorDef Foreign(MemberInfo property) } } + public class UUIDStringGeneratorDef : IGeneratorDef + { + #region Implementation of IGeneratorDef + + public string Class + { + get { return "uuid.string"; } + } + + public object Params + { + get { return null; } + } + + public System.Type DefaultReturnType + { + get { return typeof(string); } + } + + public bool SupportedAsCollectionElementId + { + get { return false; } + } + + #endregion + } + + public class UUIDHexGeneratorDef : IGeneratorDef + { + #region Implementation of IGeneratorDef + + public string Class + { + get { return "uuid.hex"; } + } + + public object Params + { + get { return null; } + } + + public System.Type DefaultReturnType + { + get { return typeof(Guid); } + } + + public bool SupportedAsCollectionElementId + { + get { return false; } + } + + #endregion + } + + public class TriggerIdentityGeneratorDef : IGeneratorDef + { + #region Implementation of IGeneratorDef + + public string Class + { + get { return "trigger-identity"; } + } + + public object Params + { + get { return null; } + } + + public System.Type DefaultReturnType + { + get { return typeof(int); } + } + + public bool SupportedAsCollectionElementId + { + get { return false; } + } + + #endregion + } + + public class TableHiLoGeneratorDef : IGeneratorDef + { + #region Implementation of IGeneratorDef + + public string Class + { + get { return "table-hilo"; } + } + + public object Params + { + get { return null; } + } + + public System.Type DefaultReturnType + { + get { return typeof(int); } + } + + public bool SupportedAsCollectionElementId + { + get { return false; } + } + + #endregion + } + + public class TableGeneratorDef : IGeneratorDef + { + #region Implementation of IGeneratorDef + + public string Class + { + get { return "table"; } + } + + public object Params + { + get { return null; } + } + + public System.Type DefaultReturnType + { + get { return typeof(int); } + } + + public bool SupportedAsCollectionElementId + { + get { return false; } + } + + #endregion + } + + public class SequenceIdentityGeneratorDef : IGeneratorDef + { + #region Implementation of IGeneratorDef + + public string Class + { + get { return "sequence-identity"; } + } + + public object Params + { + get { return null; } + } + + public System.Type DefaultReturnType + { + get { return typeof(int); } + } + + public bool SupportedAsCollectionElementId + { + get { return false; } + } + + #endregion + } + + public class SequenceHiLoGeneratorDef : IGeneratorDef + { + #region Implementation of IGeneratorDef + + public string Class + { + get { return "seqhilo"; } + } + + public object Params + { + get { return null; } + } + + public System.Type DefaultReturnType + { + get { return typeof(int); } + } + + public bool SupportedAsCollectionElementId + { + get { return false; } + } + + #endregion + } + + public class SelectGeneratorDef : IGeneratorDef + { + #region Implementation of IGeneratorDef + + public string Class + { + get { return "select"; } + } + + public object Params + { + get { return null; } + } + + public System.Type DefaultReturnType + { + get { return null; } + } + + public bool SupportedAsCollectionElementId + { + get { return false; } + } + + #endregion + } + + public class NativeGuidGeneratorDef : IGeneratorDef + { + #region Implementation of IGeneratorDef + + public string Class + { + get { return "guid.native"; } + } + + public object Params + { + get { return null; } + } + + public System.Type DefaultReturnType + { + get { return typeof(Guid); } + } + + public bool SupportedAsCollectionElementId + { + get { return true; } + } + + #endregion + } + + public class IncrementGeneratorDef : IGeneratorDef + { + #region Implementation of IGeneratorDef + + public string Class + { + get { return "increment"; } + } + + public object Params + { + get { return null; } + } + + public System.Type DefaultReturnType + { + get { return typeof(long); } + } + + public bool SupportedAsCollectionElementId + { + get { return false; } + } + + #endregion + } + + public class CounterGeneratorDef : IGeneratorDef + { + #region Implementation of IGeneratorDef + + public string Class + { + get { return "counter"; } + } + + public object Params + { + get { return null; } + } + + public System.Type DefaultReturnType + { + get { return typeof(short); } + } + + public bool SupportedAsCollectionElementId + { + get { return false; } + } + + #endregion + } + public class AssignedGeneratorDef : IGeneratorDef { #region Implementation of IGeneratorDef