Files
Orchard/src/Orchard.Tests/DataUtility.cs
Lombiq 5406b17601 Converting all files stored as CRLF in git to be stored as LF
LF is the git default and all new files are stored as such. Old files from the hg to git conversion however were moved over as CRLF.
2015-12-13 22:21:02 +01:00

84 lines
3.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using FluentNHibernate;
using FluentNHibernate.Automapping;
using FluentNHibernate.Automapping.Alterations;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Diagnostics;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
using Orchard.Data;
using Orchard.Data.Providers;
using Orchard.Environment.ShellBuilders.Models;
using MsSqlCeConfiguration = Orchard.Data.Providers.MsSqlCeConfiguration;
namespace Orchard.Tests {
public static class DataUtility {
public static ISessionFactory CreateSessionFactory(string fileName, params Type[] types) {
//var persistenceModel = AutoMap.Source(new Types(types))
// .Alterations(alt => AddAlterations(alt, types))
// .Conventions.AddFromAssemblyOf<DataModule>();
var persistenceModel = AbstractDataServicesProvider.CreatePersistenceModel(types.Select(t => new RecordBlueprint { TableName = "Test_" + t.Name, Type = t }).ToList());
var persistenceConfigurer = new SqlCeDataServicesProvider(fileName).GetPersistenceConfigurer(true/*createDatabase*/);
((MsSqlCeConfiguration)persistenceConfigurer).ShowSql();
return Fluently.Configure()
.Database(persistenceConfigurer)
.Mappings(m => m.AutoMappings.Add(persistenceModel))
.ExposeConfiguration(c => {
// This is to work around what looks to be an issue in the NHibernate driver:
// When inserting a row with IDENTITY column, the "SELET @@IDENTITY" statement
// is issued as a separate command. By default, it is also issued in a separate
// connection, which is not supported (returns NULL).
c.SetProperty("connection.release_mode", "on_close");
new SchemaExport(c).Create(false /*script*/, true /*export*/);
})
.BuildSessionFactory();
}
private static void AddAlterations(AutoMappingAlterationCollection alterations, IEnumerable<Type> types) {
foreach (var assembly in types.Select(t => t.Assembly).Distinct()) {
alterations.Add(new AutoMappingOverrideAlteration(assembly));
alterations.AddFromAssembly(assembly);
}
alterations.AddFromAssemblyOf<DataModule>();
}
public static ISessionFactory CreateSessionFactory(params Type[] types) {
return CreateSessionFactory(
string.Join(".", types.Reverse().Select(type => type.FullName)),
types);
}
#region Nested type: Types
private class Types : ITypeSource {
private readonly IEnumerable<Type> _types;
public Types(params Type[] types) {
_types = types;
}
#region ITypeSource Members
public IEnumerable<Type> GetTypes() {
return _types;
}
public void LogSource(IDiagnosticLogger logger) {
throw new NotImplementedException();
}
public string GetIdentifier() {
throw new NotImplementedException();
}
#endregion
}
#endregion
}
}