mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-11-28 17:32:44 +08:00
Update automapping convention for record table name (prefix table name with module name)
Also remove nhibernate reference from a few modules, as it is not needed anymore. --HG-- extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4046128
This commit is contained in:
@@ -9,6 +9,7 @@ using FluentNHibernate.Cfg.Db;
|
||||
using NHibernate;
|
||||
using NHibernate.Tool.hbm2ddl;
|
||||
using Orchard.Data;
|
||||
using Orchard.Environment;
|
||||
|
||||
namespace Orchard.Tests {
|
||||
public static class DataUtility {
|
||||
@@ -17,7 +18,7 @@ namespace Orchard.Tests {
|
||||
//var persistenceModel = AutoMap.Source(new Types(types))
|
||||
// .Alterations(alt => AddAlterations(alt, types))
|
||||
// .Conventions.AddFromAssemblyOf<DataModule>();
|
||||
var persistenceModel = HackSessionLocator.CreatePersistenceModel(types);
|
||||
var persistenceModel = HackSessionLocator.CreatePersistenceModel(types.Select(t => new RecordDescriptor { Prefix = "Test", Type = t }));
|
||||
|
||||
return Fluently.Configure()
|
||||
.Database(SQLiteConfiguration.Standard.UsingFile(fileName).ShowSql())
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace Orchard.Tests.Environment {
|
||||
{ExportedTypes = new[] {typeof (GammaRecord), typeof (DeltaRecord), typeof (Delta)}}
|
||||
});
|
||||
var strategy = new DefaultCompositionStrategy(extensionManager.Object);
|
||||
var recordTypes = strategy.GetRecordTypes();
|
||||
var recordTypes = strategy.GetRecordDescriptors();
|
||||
|
||||
Assert.That(recordTypes.Count(), Is.Not.EqualTo(0));
|
||||
Assert.That(recordTypes, Has.Some.EqualTo(typeof(DeltaRecord)));
|
||||
|
||||
@@ -89,8 +89,8 @@ namespace Orchard.Tests.Environment {
|
||||
};
|
||||
}
|
||||
|
||||
public IEnumerable<Type> GetRecordTypes() {
|
||||
return Enumerable.Empty<Type>();
|
||||
public IEnumerable<RecordDescriptor> GetRecordDescriptors() {
|
||||
return Enumerable.Empty<RecordDescriptor>();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -65,8 +65,7 @@ namespace Orchard.Blogs.Controllers {
|
||||
_services.ContentManager.Create(model.Blog.Item.ContentItem);
|
||||
|
||||
//TEMP: (erikpo) ensure information has committed for this record
|
||||
var session = _sessionLocator.For(typeof(BlogRecord));
|
||||
session.Flush();
|
||||
_services.ContentManager.Flush();
|
||||
|
||||
return Redirect(Url.BlogForAdmin(model.Blog.Item.As<RoutableAspect>().Slug));
|
||||
}
|
||||
|
||||
@@ -39,10 +39,6 @@
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\..\..\lib\autofac\Autofac.Integration.Web.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="NHibernate, Version=2.1.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\..\..\lib\fluentnhibernate\NHibernate.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.ComponentModel.DataAnnotations">
|
||||
|
||||
@@ -31,10 +31,6 @@
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="FluentNHibernate, Version=1.0.0.593, Culture=neutral, PublicKeyToken=8aa435e3cb308880, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\..\..\lib\fluentnhibernate\FluentNHibernate.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="ICSharpCode.SharpZipLib, Version=0.85.5.452, Culture=neutral, PublicKeyToken=1b03e6acf1164f73, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\..\..\lib\sharpziplib\ICSharpCode.SharpZipLib.dll</HintPath>
|
||||
|
||||
@@ -31,14 +31,6 @@
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="FluentNHibernate, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8aa435e3cb308880, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\..\..\lib\fluentnhibernate\FluentNHibernate.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="NHibernate, Version=2.1.2.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\..\..\lib\fluentnhibernate\NHibernate.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.ComponentModel.DataAnnotations">
|
||||
|
||||
@@ -31,9 +31,9 @@
|
||||
Set default transaction timeout to 30 minutes so that interactive debugging
|
||||
is easier (default timeout is less than one minute)
|
||||
-->
|
||||
<!--<system.transactions>
|
||||
<system.transactions>
|
||||
<defaultSettings timeout="00:30:00" />
|
||||
</system.transactions>-->
|
||||
</system.transactions>
|
||||
|
||||
<system.web>
|
||||
|
||||
|
||||
@@ -8,33 +8,34 @@ using System.Reflection.Emit;
|
||||
using FluentNHibernate.Automapping;
|
||||
using FluentNHibernate.Automapping.Alterations;
|
||||
using JetBrains.Annotations;
|
||||
using Orchard.Environment;
|
||||
|
||||
namespace Orchard.ContentManagement.Records {
|
||||
class ContentItemAlteration : IAutoMappingAlteration {
|
||||
private readonly IEnumerable<Type> _recordTypes;
|
||||
private readonly IEnumerable<RecordDescriptor> _recordDescriptors;
|
||||
|
||||
[UsedImplicitly]
|
||||
public ContentItemAlteration() {
|
||||
_recordTypes = Enumerable.Empty<Type>();
|
||||
_recordDescriptors = Enumerable.Empty<RecordDescriptor>();
|
||||
}
|
||||
|
||||
public ContentItemAlteration(IEnumerable<Type> recordTypes) {
|
||||
_recordTypes = recordTypes;
|
||||
public ContentItemAlteration(IEnumerable<RecordDescriptor> recordDescriptors) {
|
||||
_recordDescriptors = recordDescriptors;
|
||||
}
|
||||
|
||||
public void Alter(AutoPersistenceModel model) {
|
||||
|
||||
model.Override<ContentItemRecord>(mapping => {
|
||||
foreach (var recordType in _recordTypes.Where(Utility.IsPartRecord)) {
|
||||
var type = typeof(Alteration<,>).MakeGenericType(typeof(ContentItemRecord), recordType);
|
||||
foreach (var descriptor in _recordDescriptors.Where(d => Utility.IsPartRecord(d.Type))) {
|
||||
var type = typeof(Alteration<,>).MakeGenericType(typeof(ContentItemRecord), descriptor.Type);
|
||||
var alteration = (IAlteration<ContentItemRecord>)Activator.CreateInstance(type);
|
||||
alteration.Override(mapping);
|
||||
}
|
||||
});
|
||||
|
||||
model.Override<ContentItemVersionRecord>(mapping => {
|
||||
foreach (var recordType in _recordTypes.Where(Utility.IsPartVersionRecord)) {
|
||||
var type = typeof(Alteration<,>).MakeGenericType(typeof(ContentItemVersionRecord), recordType);
|
||||
foreach (var descriptor in _recordDescriptors.Where(d => Utility.IsPartVersionRecord(d.Type))) {
|
||||
var type = typeof(Alteration<,>).MakeGenericType(typeof(ContentItemVersionRecord), descriptor.Type);
|
||||
var alteration = (IAlteration<ContentItemVersionRecord>)Activator.CreateInstance(type);
|
||||
alteration.Override(mapping);
|
||||
}
|
||||
|
||||
22
src/Orchard/Data/Conventions/RecordTableNameConvention.cs
Normal file
22
src/Orchard/Data/Conventions/RecordTableNameConvention.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using FluentNHibernate.Conventions;
|
||||
using FluentNHibernate.Conventions.Instances;
|
||||
using Orchard.Environment;
|
||||
|
||||
namespace Orchard.Data.Conventions {
|
||||
public class RecordTableNameConvention : IClassConvention {
|
||||
private readonly IEnumerable<RecordDescriptor> _descriptors;
|
||||
|
||||
public RecordTableNameConvention(IEnumerable<RecordDescriptor> descriptors) {
|
||||
_descriptors = descriptors;
|
||||
}
|
||||
|
||||
public void Apply(IClassInstance instance) {
|
||||
var desc = _descriptors.Where(d => d.Type == instance.EntityType).SingleOrDefault();
|
||||
if (desc != null) {
|
||||
instance.Table(desc.Prefix + "_" + desc.Type.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,8 +7,10 @@ using FluentNHibernate.Automapping;
|
||||
using FluentNHibernate.Automapping.Alterations;
|
||||
using FluentNHibernate.Cfg;
|
||||
using FluentNHibernate.Cfg.Db;
|
||||
using FluentNHibernate.Conventions.Helpers;
|
||||
using NHibernate;
|
||||
using NHibernate.Tool.hbm2ddl;
|
||||
using Orchard.Data.Conventions;
|
||||
using Orchard.Environment;
|
||||
using Orchard.ContentManagement.Records;
|
||||
|
||||
@@ -33,16 +35,16 @@ namespace Orchard.Data {
|
||||
|
||||
var database = SQLiteConfiguration.Standard.UsingFile(hackPath);
|
||||
|
||||
var recordTypes = _compositionStrategy.GetRecordTypes();
|
||||
var recordDescriptors = _compositionStrategy.GetRecordDescriptors();
|
||||
|
||||
return _sessionFactory ??
|
||||
Interlocked.CompareExchange(
|
||||
ref _sessionFactory,
|
||||
BuildSessionFactory(database, recordTypes), null) ?? _sessionFactory;
|
||||
BuildSessionFactory(database, recordDescriptors), null) ?? _sessionFactory;
|
||||
|
||||
}
|
||||
|
||||
private static ISessionFactory BuildSessionFactory(IPersistenceConfigurer database, IEnumerable<Type> recordTypes) {
|
||||
private static ISessionFactory BuildSessionFactory(IPersistenceConfigurer database, IEnumerable<RecordDescriptor> recordTypes) {
|
||||
return Fluently.Configure()
|
||||
.Database(database)
|
||||
.Mappings(m => m.AutoMappings.Add(CreatePersistenceModel(recordTypes)))
|
||||
@@ -50,15 +52,20 @@ namespace Orchard.Data {
|
||||
.BuildSessionFactory();
|
||||
}
|
||||
|
||||
public static AutoPersistenceModel CreatePersistenceModel(IEnumerable<Type> recordTypes) {
|
||||
return AutoMap.Source(new TypeSource(recordTypes))
|
||||
public static AutoPersistenceModel CreatePersistenceModel(IEnumerable<RecordDescriptor> recordDescriptors) {
|
||||
var types = recordDescriptors.Select(d => d.Type);
|
||||
return AutoMap.Source(new TypeSource(types))
|
||||
// Ensure that namespaces of types are never auto-imported, so that
|
||||
// identical type names from different namespaces can be mapped without ambiguity
|
||||
.Conventions.Setup(x => x.Add(AutoImport.Never()))
|
||||
.Conventions.Add(new RecordTableNameConvention(recordDescriptors))
|
||||
.Alterations(alt => {
|
||||
foreach (var recordAssembly in recordTypes.Select(x => x.Assembly).Distinct()) {
|
||||
alt.Add(new AutoMappingOverrideAlteration(recordAssembly));
|
||||
}
|
||||
alt.AddFromAssemblyOf<DataModule>();
|
||||
alt.Add(new ContentItemAlteration(recordTypes));
|
||||
})
|
||||
foreach (var recordAssembly in recordDescriptors.Select(x => x.Type.Assembly).Distinct()) {
|
||||
alt.Add(new AutoMappingOverrideAlteration(recordAssembly));
|
||||
}
|
||||
alt.AddFromAssemblyOf<DataModule>();
|
||||
alt.Add(new ContentItemAlteration(recordDescriptors));
|
||||
})
|
||||
.Conventions.AddFromAssemblyOf<DataModule>();
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ using Autofac;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Records;
|
||||
using Orchard.Extensions;
|
||||
using Orchard.Utility;
|
||||
|
||||
namespace Orchard.Environment {
|
||||
//TEMP: This will be replaced by packaging system
|
||||
@@ -13,7 +14,12 @@ namespace Orchard.Environment {
|
||||
public interface ICompositionStrategy {
|
||||
IEnumerable<Type> GetModuleTypes();
|
||||
IEnumerable<Type> GetDependencyTypes();
|
||||
IEnumerable<Type> GetRecordTypes();
|
||||
IEnumerable<RecordDescriptor> GetRecordDescriptors();
|
||||
}
|
||||
|
||||
public class RecordDescriptor {
|
||||
public Type Type { get; set; }
|
||||
public string Prefix { get; set; }
|
||||
}
|
||||
|
||||
public class DefaultCompositionStrategy : ICompositionStrategy {
|
||||
@@ -39,17 +45,28 @@ namespace Orchard.Environment {
|
||||
return modules;
|
||||
}
|
||||
|
||||
public IEnumerable<Type> GetRecordTypes() {
|
||||
var types = _extensionManager.ActiveExtensions().SelectMany(x => x.ExportedTypes);
|
||||
var coreRecords = new[] {
|
||||
typeof (ContentTypeRecord),
|
||||
typeof (ContentItemRecord),
|
||||
typeof (ContentItemVersionRecord),
|
||||
};
|
||||
var recordTypes = types.Where(IsRecordType).Concat(coreRecords);
|
||||
return recordTypes;
|
||||
}
|
||||
public IEnumerable<RecordDescriptor> GetRecordDescriptors() {
|
||||
var descriptors = new List<RecordDescriptor>{
|
||||
new RecordDescriptor { Prefix = "Core", Type = typeof (ContentTypeRecord)},
|
||||
new RecordDescriptor { Prefix = "Core", Type = typeof (ContentItemRecord)},
|
||||
new RecordDescriptor { Prefix = "Core", Type = typeof (ContentItemVersionRecord)},
|
||||
};
|
||||
|
||||
foreach (var extension in _extensionManager.ActiveExtensions()) {
|
||||
var prefix = extension.Descriptor.Name
|
||||
.Replace("Orchard.", "")
|
||||
.Replace(".", "_");
|
||||
|
||||
var recordDescriptors = extension
|
||||
.ExportedTypes
|
||||
.Where(IsRecordType)
|
||||
.Select(type => new RecordDescriptor { Prefix = prefix, Type = type });
|
||||
|
||||
descriptors.AddRange(recordDescriptors);
|
||||
}
|
||||
|
||||
return descriptors.ToReadOnlyCollection();
|
||||
}
|
||||
|
||||
private static bool IsRecordType(Type type) {
|
||||
return ((type.Namespace ?? "").EndsWith(".Models") || (type.Namespace ?? "").EndsWith(".Records")) &&
|
||||
|
||||
@@ -132,6 +132,7 @@
|
||||
<Compile Include="ContentManagement\Handlers\PublishContentContext.cs" />
|
||||
<Compile Include="ContentManagement\Handlers\RemoveContentContext.cs" />
|
||||
<Compile Include="ContentManagement\Handlers\VersionContentContext.cs" />
|
||||
<Compile Include="Data\Conventions\RecordTableNameConvention.cs" />
|
||||
<Compile Include="Extensions\AreaFolders.cs" />
|
||||
<Compile Include="Extensions\ExtensionFolders.cs" />
|
||||
<Compile Include="Extensions\Loaders\AreaExtensionLoader.cs" />
|
||||
|
||||
Reference in New Issue
Block a user