mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Clean code
This commit is contained in:
22
src/Orchard/Data/DefaultSessionConfigurationEvents.cs
Normal file
22
src/Orchard/Data/DefaultSessionConfigurationEvents.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using FluentNHibernate.Automapping;
|
||||
using FluentNHibernate.Cfg;
|
||||
|
||||
namespace Orchard.Data {
|
||||
/// <summary>
|
||||
/// Base class for session configuration
|
||||
/// </summary>
|
||||
public class DefaultSessionConfigurationEvents : SessionConfigurationEventsWithParameters {
|
||||
/// <summary>
|
||||
/// Called when an empty fluent configuration object has been created,
|
||||
/// before applying any default Orchard config settings (alterations, conventions etc.).
|
||||
/// </summary>
|
||||
/// <param name="cfg">Empty fluent NH configuration object.</param>
|
||||
/// <param name="defaultModel">Default persistence model that is about to be used.</param>
|
||||
public override void Created(FluentConfiguration cfg, AutoPersistenceModel defaultModel) {
|
||||
defaultModel.OverrideAll(map => {
|
||||
map.IgnoreProperties(x => x.MemberInfo.IsDefined(typeof(DoNotMapAttribute), false));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
19
src/Orchard/Data/DoNotMapAttribute.cs
Normal file
19
src/Orchard/Data/DoNotMapAttribute.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
|
||||
namespace Orchard.Data {
|
||||
/// <summary>
|
||||
/// Mark a property to be excluded from NHibernate mapping
|
||||
/// </summary>
|
||||
public class DoNotMapAttribute : Attribute {
|
||||
}
|
||||
}
|
||||
|
||||
//usage:
|
||||
//
|
||||
//public class PersonRecord {
|
||||
// public virtual int Id { get; set; }
|
||||
// public virtual string FirstName { get; set; }
|
||||
// public virtual string LastName { get; set; }
|
||||
// [DoNotMap]
|
||||
// public virtual string FullName { get {return Fullname+", "+LastName;} }
|
||||
//}
|
||||
@@ -0,0 +1,86 @@
|
||||
using FluentNHibernate.Automapping;
|
||||
using FluentNHibernate.Cfg;
|
||||
using NHibernate.Cfg;
|
||||
using Orchard.Utility;
|
||||
using Orchard.Data.Providers;
|
||||
|
||||
namespace Orchard.Data {
|
||||
/// <summary>
|
||||
/// Add ability for the configuration event handler be aware of parameters
|
||||
/// </summary>
|
||||
/// <param name="parameters"></param>
|
||||
public interface ISessionConfigurationEventsWithParameters : ISessionConfigurationEvents {
|
||||
SessionFactoryParameters Parameters { set; get; }
|
||||
}
|
||||
}
|
||||
|
||||
// usage sample
|
||||
//using System;
|
||||
//using System.Collections.Generic;
|
||||
//using System.Linq;
|
||||
//using FluentNHibernate.Automapping;
|
||||
//using FluentNHibernate.Cfg;
|
||||
//using FluentNHibernate.Conventions;
|
||||
//using FluentNHibernate.Conventions.Instances;
|
||||
//using NHibernate.Cfg;
|
||||
//using Orchard.Data;
|
||||
//using Orchard.Environment.ShellBuilders.Models;
|
||||
//using Orchard.Utility;
|
||||
|
||||
//namespace usage_example {
|
||||
|
||||
// public class PersistenceConfiguration : ISessionConfigurationEventsWithParameters {
|
||||
// Orchard.Data.Providers.SessionFactoryParameters _parameters;
|
||||
|
||||
// public PersistenceConfiguration() {
|
||||
// }
|
||||
|
||||
// public void SetParameters(Orchard.Data.Providers.SessionFactoryParameters parameters) {
|
||||
// _parameters = parameters;
|
||||
// }
|
||||
|
||||
// public void Created(FluentConfiguration cfg, AutoPersistenceModel defaultModel) {
|
||||
// Dictionary<Type, RecordBlueprint> descriptors = _parameters.RecordDescriptors.ToDictionary(d => d.Type);
|
||||
// defaultModel.Conventions.Add(new IbnJoinedSubclassConvention(descriptors));
|
||||
// defaultModel.OverrideAll(map => {
|
||||
// map.IgnoreProperties(x => x.MemberInfo.IsDefined(typeof(DoNotMapAttribute), false));
|
||||
// });
|
||||
// }
|
||||
|
||||
// public void Prepared(FluentConfiguration cfg) {
|
||||
// }
|
||||
|
||||
// public void Building(Configuration cfg) {
|
||||
// }
|
||||
|
||||
// public void Finished(Configuration cfg) {
|
||||
// }
|
||||
|
||||
// public void ComputingHash(Hash hash) {
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// public class IbnJoinedSubclassConvention : IJoinedSubclassConvention {
|
||||
// private readonly Dictionary<Type, RecordBlueprint> _descriptors;
|
||||
|
||||
// public IbnJoinedSubclassConvention(Dictionary<Type, RecordBlueprint> descriptors) {
|
||||
// _descriptors = descriptors;
|
||||
// }
|
||||
|
||||
// public void Apply(IJoinedSubclassInstance instance) {
|
||||
// if (instance.EntityType.FullName.StartsWith("Ibn")) {
|
||||
// instance.Key.Column("Id");
|
||||
// RecordBlueprint desc;
|
||||
// if (_descriptors.TryGetValue(instance.EntityType, out desc)) {
|
||||
// instance.Table(desc.TableName);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// public class DoNotMapAttribute : Attribute {
|
||||
// }
|
||||
|
||||
//}
|
||||
@@ -38,18 +38,15 @@ namespace Orchard.Data.Providers {
|
||||
|
||||
var config = Fluently.Configure();
|
||||
|
||||
// not working for me.. (BN)
|
||||
//parameters.Configurers.OfType<ISessionConfigurationEventsWithParameters>()
|
||||
// .Invoke(c => c.Parameters=parameters, Logger);
|
||||
|
||||
foreach (var c in parameters.Configurers.OfType<ISessionConfigurationEventsWithParameters>())
|
||||
foreach (var c in parameters.Configurers.OfType<ISessionConfigurationEventsWithParameters>()) {
|
||||
c.Parameters = parameters;
|
||||
}
|
||||
|
||||
parameters.Configurers.Invoke(c => c.Created(config, persistenceModel), Logger);
|
||||
|
||||
config = config.Database(database)
|
||||
.Mappings(m => m.AutoMappings.Add(persistenceModel))
|
||||
.ExposeConfiguration(cfg => {
|
||||
.ExposeConfiguration(cfg => {
|
||||
cfg
|
||||
.SetProperty(NHibernate.Cfg.Environment.FormatSql, Boolean.FalseString)
|
||||
.SetProperty(NHibernate.Cfg.Environment.GenerateStatistics, Boolean.FalseString)
|
||||
@@ -65,16 +62,16 @@ namespace Orchard.Data.Providers {
|
||||
.SetProperty(NHibernate.Cfg.Environment.BatchSize, "256")
|
||||
;
|
||||
|
||||
cfg.EventListeners.LoadEventListeners = new ILoadEventListener[] {new OrchardLoadEventListener()};
|
||||
cfg.EventListeners.LoadEventListeners = new ILoadEventListener[] { new OrchardLoadEventListener() };
|
||||
cfg.EventListeners.PostLoadEventListeners = new IPostLoadEventListener[0];
|
||||
cfg.EventListeners.PreLoadEventListeners = new IPreLoadEventListener[0];
|
||||
|
||||
|
||||
// don't enable PrepareSql by default as it breaks on SqlCe
|
||||
// this can be done per driver by overriding AlterConfiguration
|
||||
AlterConfiguration(cfg);
|
||||
|
||||
parameters.Configurers.Invoke(c => c.Building(cfg), Logger);
|
||||
|
||||
|
||||
})
|
||||
;
|
||||
|
||||
@@ -84,11 +81,11 @@ namespace Orchard.Data.Providers {
|
||||
}
|
||||
|
||||
protected virtual void AlterConfiguration(Configuration config) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static AutoPersistenceModel CreatePersistenceModel(ICollection<RecordBlueprint> recordDescriptors) {
|
||||
if(recordDescriptors == null) {
|
||||
if (recordDescriptors == null) {
|
||||
throw new ArgumentNullException("recordDescriptors");
|
||||
}
|
||||
|
||||
@@ -116,7 +113,7 @@ namespace Orchard.Data.Providers {
|
||||
public TypeSource(IEnumerable<RecordBlueprint> recordDescriptors) { _recordDescriptors = recordDescriptors; }
|
||||
|
||||
public IEnumerable<Type> GetTypes() { return _recordDescriptors.Select(descriptor => descriptor.Type); }
|
||||
|
||||
|
||||
public void LogSource(IDiagnosticLogger logger) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
@@ -135,8 +132,7 @@ namespace Orchard.Data.Providers {
|
||||
if (@event.InstanceToLoad != null) {
|
||||
entityPersister = source.GetEntityPersister(null, @event.InstanceToLoad);
|
||||
@event.EntityClassName = @event.InstanceToLoad.GetType().FullName;
|
||||
}
|
||||
else
|
||||
} else
|
||||
entityPersister = source.Factory.GetEntityPersister(@event.EntityClassName);
|
||||
if (entityPersister == null)
|
||||
throw new HibernateException("Unable to locate persister: " + @event.EntityClassName);
|
||||
@@ -161,11 +157,9 @@ namespace Orchard.Data.Providers {
|
||||
|
||||
if (loadType.IsNakedEntityReturned) {
|
||||
@event.Result = Load(@event, entityPersister, keyToLoad, loadType);
|
||||
}
|
||||
else if (@event.LockMode == LockMode.None) {
|
||||
} else if (@event.LockMode == LockMode.None) {
|
||||
@event.Result = ProxyOrLoad(@event, entityPersister, keyToLoad, loadType);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
@event.Result = LockAndLoad(@event, entityPersister, keyToLoad, loadType, source);
|
||||
}
|
||||
}
|
||||
|
||||
76
src/Orchard/Data/SessionConfigurationEventsWithParameters.cs
Normal file
76
src/Orchard/Data/SessionConfigurationEventsWithParameters.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using FluentNHibernate.Automapping;
|
||||
using FluentNHibernate.Cfg;
|
||||
using NHibernate.Cfg;
|
||||
using Orchard.Data.Providers;
|
||||
using Orchard.Environment.ShellBuilders.Models;
|
||||
using Orchard.Utility;
|
||||
|
||||
namespace Orchard.Data
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class for session configuration
|
||||
/// </summary>
|
||||
public abstract class SessionConfigurationEventsWithParameters : ISessionConfigurationEventsWithParameters
|
||||
{
|
||||
/// <summary>
|
||||
/// The parameters are set before any of the functions are called.
|
||||
/// </summary>
|
||||
public SessionFactoryParameters Parameters { set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the BlueprintDescriptors - translating from type to DB Table names
|
||||
/// </summary>
|
||||
public Dictionary<Type, RecordBlueprint> BlueprintDescriptors {
|
||||
get {
|
||||
if (_descriptors == null)
|
||||
_descriptors = Parameters.RecordDescriptors.ToDictionary(d => d.Type);
|
||||
return _descriptors;
|
||||
}
|
||||
}
|
||||
protected Dictionary<Type, RecordBlueprint> _descriptors = null;
|
||||
|
||||
/// <summary>
|
||||
/// Called when an empty fluent configuration object has been created,
|
||||
/// before applying any default Orchard config settings (alterations, conventions etc.).
|
||||
/// </summary>
|
||||
/// <param name="cfg">Empty fluent NH configuration object.</param>
|
||||
/// <param name="defaultModel">Default persistence model that is about to be used.</param>
|
||||
public virtual void Created(FluentConfiguration cfg, AutoPersistenceModel defaultModel) { }
|
||||
|
||||
/// <summary>
|
||||
/// Called when fluent configuration has been prepared but not yet built.
|
||||
/// </summary>
|
||||
/// <param name="cfg">Prepared fluent NH configuration object.</param>
|
||||
public virtual void Prepared(FluentConfiguration cfg) { }
|
||||
|
||||
/// <summary>
|
||||
/// Called when raw NHibernate configuration is being built, after applying all customizations.
|
||||
/// Allows applying final alterations to the raw NH configuration.
|
||||
/// </summary>
|
||||
/// <param name="cfg">Raw NH configuration object being processed.</param>
|
||||
public virtual void Building(Configuration cfg) { }
|
||||
|
||||
/// <summary>
|
||||
/// Called when NHibernate configuration has been built or read from cache storage (mappings.bin file by default).
|
||||
/// </summary>
|
||||
/// <param name="cfg">Final, raw NH configuration object.</param>
|
||||
public virtual void Finished(Configuration cfg) { }
|
||||
|
||||
/// <summary>
|
||||
/// Called when configuration hash is being computed. If hash changes, configuration will be rebuilt and stored in mappings.bin.
|
||||
/// This method allows to alter the default hash to take into account custom configuration changes.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// It's a developer responsibility to make sure hash is correctly updated when config needs to be rebuilt.
|
||||
/// Otherwise the cached configuration (mappings.bin file) will be used as long as default Orchard configuration
|
||||
/// is unchanged or until the file is manually removed.
|
||||
/// </remarks>
|
||||
/// <param name="hash">Current hash object</param>
|
||||
public virtual void ComputingHash(Hash hash) { }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -181,7 +181,7 @@
|
||||
<Compile Include="Data\Bags\SArray.cs" />
|
||||
<Compile Include="Data\Conventions\UtcDateTimeConvention.cs" />
|
||||
<Compile Include="Data\DefaultSessionConfigurationEvents.cs" />
|
||||
<Compile Include="Data\DoNotMapAttribute.cs" />
|
||||
<Compile Include="Data\DoNotMapAttribute.cs" />
|
||||
<Compile Include="Data\FetchRequest.cs" />
|
||||
<Compile Include="Data\SessionConfigurationEventsWithParameters.cs" />
|
||||
<Compile Include="Data\ISessionConfigurationEventsWithParameters.cs" />
|
||||
|
||||
Reference in New Issue
Block a user