Errors updated. Do not use old build.
This commit is contained in:
33
Tanshu.Accounts.Repository/Fluent/AllowNullConvention.cs
Normal file
33
Tanshu.Accounts.Repository/Fluent/AllowNullConvention.cs
Normal file
@ -0,0 +1,33 @@
|
||||
//using System;
|
||||
//using FluentNHibernate.Conventions;
|
||||
//using System.Reflection;
|
||||
//using FluentNHibernate;
|
||||
//using FluentNHibernate.Conventions.Instances;
|
||||
//using FluentNHibernate.Conventions.Inspections;
|
||||
//using FluentNHibernate.Conventions.AcceptanceCriteria;
|
||||
//using Tanshu.Accounts.Contracts;
|
||||
|
||||
//namespace Tanshu.Accounts.Conventions
|
||||
//{
|
||||
// public class AllowNullConvention : IPropertyConvention, IPropertyConventionAcceptance
|
||||
// {
|
||||
// public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
|
||||
// {
|
||||
// criteria.Expect(
|
||||
// x => !this.IsNullableProperty(x)
|
||||
// || x.Property.MemberInfo.GetCustomAttributes(typeof(AllowNullAttribute), true).Length > 0);
|
||||
// }
|
||||
|
||||
// public void Apply(IPropertyInstance instance)
|
||||
// {
|
||||
// instance.Nullable();
|
||||
// }
|
||||
|
||||
// private bool IsNullableProperty(IExposedThroughPropertyInspector target)
|
||||
// {
|
||||
// var type = target.Property.PropertyType;
|
||||
|
||||
// return type.Equals(typeof(string)) || (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)));
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
35
Tanshu.Accounts.Repository/Fluent/AttributeChecker.cs
Normal file
35
Tanshu.Accounts.Repository/Fluent/AttributeChecker.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using FluentNHibernate.Conventions.Inspections;
|
||||
using System.Reflection;
|
||||
using Tanshu.Accounts.Contracts;
|
||||
|
||||
namespace Tanshu.Accounts.SqlDAO
|
||||
{
|
||||
public static class AttributeChecker
|
||||
{
|
||||
public static bool Cascade(ICustomAttributeProvider provider)
|
||||
{
|
||||
return provider.GetCustomAttributes(typeof(CascadeAttribute), false).Length == 1;
|
||||
}
|
||||
public static bool Unique(ICustomAttributeProvider provider)
|
||||
{
|
||||
return provider.GetCustomAttributes(typeof(UniqueAttribute), false).Length == 1;
|
||||
}
|
||||
public static bool NotNull(ICustomAttributeProvider provider)
|
||||
{
|
||||
return provider.GetCustomAttributes(typeof(NotNullAttribute), false).Length == 1;
|
||||
}
|
||||
public static bool Formula(ICustomAttributeProvider provider)
|
||||
{
|
||||
return provider.GetCustomAttributes(typeof(FormulaAttribute), false).Length == 1;
|
||||
}
|
||||
public static string GetFormula(ICustomAttributeProvider provider)
|
||||
{
|
||||
var vv = (FormulaAttribute)provider.GetCustomAttributes(typeof(FormulaAttribute), false)[0];
|
||||
return vv.Formula;
|
||||
}
|
||||
}
|
||||
}
|
||||
35
Tanshu.Accounts.Repository/Fluent/CascadeConvention.cs
Normal file
35
Tanshu.Accounts.Repository/Fluent/CascadeConvention.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using FluentNHibernate.Conventions;
|
||||
using FluentNHibernate.Conventions.Instances;
|
||||
using Tanshu.Accounts.Contracts;
|
||||
using System.Reflection;
|
||||
using System.Diagnostics;
|
||||
using FluentNHibernate.Conventions.AcceptanceCriteria;
|
||||
using FluentNHibernate.Conventions.Inspections;
|
||||
|
||||
namespace Tanshu.Accounts.SqlDAO
|
||||
{
|
||||
public class CascadeConvention : IHasManyConvention, IReferenceConvention
|
||||
{
|
||||
public void Apply(IManyToOneInstance instance)
|
||||
{
|
||||
var property = instance.Property;
|
||||
if (!AttributeChecker.Cascade(property.MemberInfo)) return;
|
||||
Trace.TraceInformation("CascadeAll on {0}.{1}", property.DeclaringType.Name, property.Name);
|
||||
instance.Cascade.All();
|
||||
}
|
||||
|
||||
public void Apply(IOneToManyCollectionInstance instance)
|
||||
{
|
||||
var property = instance.Member;
|
||||
if (!AttributeChecker.Cascade(property)) return;
|
||||
Trace.TraceInformation("CascadeAllDeleteOrphan on {0}.{1}", property.DeclaringType.Name, property.Name);
|
||||
instance.Cascade.AllDeleteOrphan();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
24
Tanshu.Accounts.Repository/Fluent/ClassConvention.cs
Normal file
24
Tanshu.Accounts.Repository/Fluent/ClassConvention.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using FluentNHibernate.Conventions;
|
||||
using FluentNHibernate.Conventions.Instances;
|
||||
using FluentNHibernate.Conventions.Inspections;
|
||||
using FluentNHibernate.Conventions.AcceptanceCriteria;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Tanshu.Accounts.SqlDAO
|
||||
{
|
||||
public class ClassConvention : IClassConvention, IClassConventionAcceptance
|
||||
{
|
||||
public void Apply(IClassInstance instance)
|
||||
{
|
||||
string table = Inflector.Pluralize(instance.EntityType.Name);
|
||||
table = instance.EntityType.Namespace.Substring(instance.EntityType.Namespace.LastIndexOf('.') + 1) + "_" + table;
|
||||
Trace.TraceInformation(string.Format("Table for {0}: {1}", instance.EntityType.Name, table));
|
||||
instance.Table(table);
|
||||
}
|
||||
|
||||
public void Accept(IAcceptanceCriteria<IClassInspector> criteria)
|
||||
{
|
||||
criteria.Expect(x => !x.EntityType.IsEnum);
|
||||
}
|
||||
}
|
||||
}
|
||||
28
Tanshu.Accounts.Repository/Fluent/EnumConvention.cs
Normal file
28
Tanshu.Accounts.Repository/Fluent/EnumConvention.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using FluentNHibernate.Conventions;
|
||||
using System.Reflection;
|
||||
using FluentNHibernate;
|
||||
using FluentNHibernate.Conventions.Instances;
|
||||
using FluentNHibernate.Conventions.Inspections;
|
||||
using FluentNHibernate.Conventions.AcceptanceCriteria;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Tanshu.Accounts.Conventions
|
||||
{
|
||||
public class EnumConvention : IUserTypeConvention
|
||||
{
|
||||
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
|
||||
{
|
||||
criteria.Expect(x => (x.Property.PropertyType.IsEnum
|
||||
|| (x.Property.PropertyType.IsGenericType &&
|
||||
x.Property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) &&
|
||||
x.Property.PropertyType.GetGenericArguments()[0].IsEnum)));
|
||||
}
|
||||
|
||||
public void Apply(IPropertyInstance target)
|
||||
{
|
||||
Trace.TraceInformation(string.Format("Custom Enum for {0}.{1}", target.EntityType.Name, target.Property.Name));
|
||||
target.CustomType(target.Property.PropertyType);
|
||||
}
|
||||
}
|
||||
}
|
||||
1093
Tanshu.Accounts.Repository/Fluent/Fixtures.cs
Normal file
1093
Tanshu.Accounts.Repository/Fluent/Fixtures.cs
Normal file
File diff suppressed because it is too large
Load Diff
17
Tanshu.Accounts.Repository/Fluent/ForeignKeyConvention.cs
Normal file
17
Tanshu.Accounts.Repository/Fluent/ForeignKeyConvention.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using FluentNHibernate.Conventions;
|
||||
using System.Reflection;
|
||||
using FluentNHibernate;
|
||||
|
||||
namespace Tanshu.Accounts.Conventions
|
||||
{
|
||||
public class CustomForeignKeyConvention : ForeignKeyConvention
|
||||
{
|
||||
protected override string GetKeyName(Member property, Type type)
|
||||
{
|
||||
return property == null
|
||||
? type.Name + "ID"
|
||||
: property.Name + "ID";
|
||||
}
|
||||
}
|
||||
}
|
||||
29
Tanshu.Accounts.Repository/Fluent/FormulaConvention.cs
Normal file
29
Tanshu.Accounts.Repository/Fluent/FormulaConvention.cs
Normal file
@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using FluentNHibernate.Conventions;
|
||||
using System.Reflection;
|
||||
using FluentNHibernate;
|
||||
using FluentNHibernate.Conventions.Instances;
|
||||
using FluentNHibernate.Conventions.Inspections;
|
||||
using FluentNHibernate.Conventions.AcceptanceCriteria;
|
||||
using Tanshu.Accounts.Contracts;
|
||||
using Tanshu.Accounts.SqlDAO;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Tanshu.Accounts.Conventions
|
||||
{
|
||||
public class FormulaConvention : IPropertyConvention, IPropertyConventionAcceptance
|
||||
{
|
||||
public void Apply(IPropertyInstance instance)
|
||||
{
|
||||
var property = instance.Property;
|
||||
Trace.TraceInformation("Formula on {0}.{1}", property.DeclaringType.Name, property.Name);
|
||||
instance.Formula(AttributeChecker.GetFormula(property.MemberInfo));
|
||||
instance.Generated.Always();
|
||||
}
|
||||
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
|
||||
{
|
||||
criteria.Expect(x => AttributeChecker.Formula(x.Property.MemberInfo));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
243
Tanshu.Accounts.Repository/Fluent/Inflector.cs
Normal file
243
Tanshu.Accounts.Repository/Fluent/Inflector.cs
Normal file
@ -0,0 +1,243 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Tanshu.Accounts.SqlDAO
|
||||
{
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
/// <summary>
|
||||
/// The Inflector class transforms words from one
|
||||
/// form to another. For example, from singular to plural.
|
||||
/// </summary>
|
||||
public class Inflector
|
||||
{
|
||||
private static readonly List<Rule> plurals = new List<Rule>();
|
||||
|
||||
private static readonly List<Rule> singulars = new List<Rule>();
|
||||
|
||||
private static readonly List<string> uncountables = new List<string>();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes static members of the <see cref="Inflector"/> class.
|
||||
/// </summary>
|
||||
static Inflector()
|
||||
{
|
||||
AddPlural("$", "s");
|
||||
AddPlural("s$", "s");
|
||||
AddPlural("(ax|test)is$", "$1es");
|
||||
AddPlural("(octop|vir)us$", "$1i");
|
||||
AddPlural("(alias|status)$", "$1es");
|
||||
AddPlural("(bu)s$", "$1ses");
|
||||
AddPlural("(buffal|tomat)o$", "$1oes");
|
||||
AddPlural("([ti])um$", "$1a");
|
||||
AddPlural("sis$", "ses");
|
||||
AddPlural("(?:([^f])fe|([lr])f)$", "$1$2ves");
|
||||
AddPlural("(hive)$", "$1s");
|
||||
AddPlural("([^aeiouy]|qu)y$", "$1ies");
|
||||
AddPlural("(x|ch|ss|sh)$", "$1es");
|
||||
AddPlural("(matr|vert|ind)ix|ex$", "$1ices");
|
||||
AddPlural("([m|l])ouse$", "$1ice");
|
||||
AddPlural("^(ox)$", "$1en");
|
||||
AddPlural("(quiz)$", "$1zes");
|
||||
|
||||
AddSingular("s$", string.Empty);
|
||||
AddSingular("(n)ews$", "$1ews");
|
||||
AddSingular("([ti])a$", "$1um");
|
||||
AddSingular("((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$", "$1$2sis");
|
||||
AddSingular("(^analy)ses$", "$1sis");
|
||||
AddSingular("([^f])ves$", "$1fe");
|
||||
AddSingular("(hive)s$", "$1");
|
||||
AddSingular("(tive)s$", "$1");
|
||||
AddSingular("([lr])ves$", "$1f");
|
||||
AddSingular("([^aeiouy]|qu)ies$", "$1y");
|
||||
AddSingular("(s)eries$", "$1eries");
|
||||
AddSingular("(m)ovies$", "$1ovie");
|
||||
AddSingular("(x|ch|ss|sh)es$", "$1");
|
||||
AddSingular("([m|l])ice$", "$1ouse");
|
||||
AddSingular("(bus)es$", "$1");
|
||||
AddSingular("(o)es$", "$1");
|
||||
AddSingular("(shoe)s$", "$1");
|
||||
AddSingular("(cris|ax|test)es$", "$1is");
|
||||
AddSingular("(octop|vir)i$", "$1us");
|
||||
AddSingular("(alias|status)es$", "$1");
|
||||
AddSingular("^(ox)en", "$1");
|
||||
AddSingular("(vert|ind)ices$", "$1ex");
|
||||
AddSingular("(matr)ices$", "$1ix");
|
||||
AddSingular("(quiz)zes$", "$1");
|
||||
|
||||
AddIrregular("person", "people");
|
||||
AddIrregular("man", "men");
|
||||
AddIrregular("child", "children");
|
||||
AddIrregular("sex", "sexes");
|
||||
AddIrregular("move", "moves");
|
||||
|
||||
AddUncountable("equipment");
|
||||
AddUncountable("information");
|
||||
AddUncountable("rice");
|
||||
AddUncountable("money");
|
||||
AddUncountable("species");
|
||||
AddUncountable("series");
|
||||
AddUncountable("fish");
|
||||
AddUncountable("sheep");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Prevents a default instance of the <see cref="Inflector"/> class from being created.
|
||||
/// </summary>
|
||||
private Inflector()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Capitalizes a word.
|
||||
/// </summary>
|
||||
/// <param name="word">
|
||||
/// The word to be capitalized.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// <paramref name="word"/> capitalized.
|
||||
/// </returns>
|
||||
public static string Capitalize(string word)
|
||||
{
|
||||
return word.Substring(0, 1).ToUpper() + word.Substring(1).ToLower();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the plural of a word.
|
||||
/// </summary>
|
||||
/// <param name="word">
|
||||
/// The singular form
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The plural form of <paramref name="word"/>
|
||||
/// </returns>
|
||||
public static string Pluralize(string word)
|
||||
{
|
||||
return ApplyRules(plurals, word);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the singular of a word.
|
||||
/// </summary>
|
||||
/// <param name="word">
|
||||
/// The plural form
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The singular form of <paramref name="word"/>
|
||||
/// </returns>
|
||||
public static string Singularize(string word)
|
||||
{
|
||||
return ApplyRules(singulars, word);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the irregular.
|
||||
/// </summary>
|
||||
/// <param name="singular">The singular.</param>
|
||||
/// <param name="plural">The plural.</param>
|
||||
private static void AddIrregular(string singular, string plural)
|
||||
{
|
||||
AddPlural("(" + singular[0] + ")" + singular.Substring(1) + "$", "$1" + plural.Substring(1));
|
||||
AddSingular("(" + plural[0] + ")" + plural.Substring(1) + "$", "$1" + singular.Substring(1));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the plural.
|
||||
/// </summary>
|
||||
/// <param name="rule">The replacement rule.</param>
|
||||
/// <param name="replacement">The replacement.</param>
|
||||
private static void AddPlural(string rule, string replacement)
|
||||
{
|
||||
plurals.Add(new Rule(rule, replacement));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the singular.
|
||||
/// </summary>
|
||||
/// <param name="rule">The replacement rule.</param>
|
||||
/// <param name="replacement">The replacement.</param>
|
||||
private static void AddSingular(string rule, string replacement)
|
||||
{
|
||||
singulars.Add(new Rule(rule, replacement));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the uncountable.
|
||||
/// </summary>
|
||||
/// <param name="word">The uncountable word.</param>
|
||||
private static void AddUncountable(string word)
|
||||
{
|
||||
uncountables.Add(word.ToLower());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies the rules.
|
||||
/// </summary>
|
||||
/// <param name="rules">The replacement rules.</param>
|
||||
/// <param name="word">The word to be replaced.</param>
|
||||
/// <returns>String resulting from rules being applied.</returns>
|
||||
private static string ApplyRules(IList rules, string word)
|
||||
{
|
||||
string result = word;
|
||||
|
||||
if (!uncountables.Contains(word.ToLower()))
|
||||
{
|
||||
for (int i = rules.Count - 1; i >= 0; i--)
|
||||
{
|
||||
var rule = (Rule)rules[i];
|
||||
|
||||
if ((result = rule.Apply(word)) != null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replacement rule.
|
||||
/// </summary>
|
||||
private class Rule
|
||||
{
|
||||
private readonly Regex regex;
|
||||
|
||||
private readonly string replacement;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Rule"/> class.
|
||||
/// </summary>
|
||||
/// <param name="pattern">
|
||||
/// The pattern.
|
||||
/// </param>
|
||||
/// <param name="replacement">
|
||||
/// The replacement.
|
||||
/// </param>
|
||||
public Rule(string pattern, string replacement)
|
||||
{
|
||||
this.regex = new Regex(pattern, RegexOptions.IgnoreCase);
|
||||
this.replacement = replacement;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies the specified word.
|
||||
/// </summary>
|
||||
/// <param name="word">The word to be replaced.</param>
|
||||
/// <returns>Regex replaced word.</returns>
|
||||
public string Apply(string word)
|
||||
{
|
||||
if (!this.regex.IsMatch(word))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.regex.Replace(word, this.replacement);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
28
Tanshu.Accounts.Repository/Fluent/NotNullConvention.cs
Normal file
28
Tanshu.Accounts.Repository/Fluent/NotNullConvention.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using FluentNHibernate.Conventions;
|
||||
using System.Reflection;
|
||||
using FluentNHibernate;
|
||||
using FluentNHibernate.Conventions.Instances;
|
||||
using FluentNHibernate.Conventions.Inspections;
|
||||
using FluentNHibernate.Conventions.AcceptanceCriteria;
|
||||
using Tanshu.Accounts.Contracts;
|
||||
using Tanshu.Accounts.SqlDAO;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Tanshu.Accounts.Conventions
|
||||
{
|
||||
public class NotNullConvention : IPropertyConvention, IPropertyConventionAcceptance
|
||||
{
|
||||
public void Apply(IPropertyInstance instance)
|
||||
{
|
||||
var property = instance.Property;
|
||||
Trace.TraceInformation("Not Null on {0}.{1}", property.DeclaringType.Name, property.Name);
|
||||
instance.Not.Nullable();
|
||||
}
|
||||
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
|
||||
{
|
||||
criteria.Expect(x => AttributeChecker.NotNull(x.Property.MemberInfo));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
28
Tanshu.Accounts.Repository/Fluent/PrimaryKeyConvention.cs
Normal file
28
Tanshu.Accounts.Repository/Fluent/PrimaryKeyConvention.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using FluentNHibernate.Conventions;
|
||||
using FluentNHibernate.Conventions.Inspections;
|
||||
using FluentNHibernate.Conventions.Instances;
|
||||
using FluentNHibernate.Conventions.AcceptanceCriteria;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Tanshu.Accounts.Conventions
|
||||
{
|
||||
public class PrimaryKeyConvention : IIdConvention, IIdConventionAcceptance
|
||||
{
|
||||
public void Apply(IIdentityInstance instance)
|
||||
{
|
||||
Trace.TraceInformation("PK on {0}.{1}", instance.EntityType.Name, instance.EntityType.Name + "ID");
|
||||
instance.Column(instance.EntityType.Name + "ID");
|
||||
//instance.GeneratedBy.Assigned();
|
||||
//instance.Access.ReadOnlyPropertyThroughCamelCaseField(CamelCasePrefix.None);
|
||||
}
|
||||
|
||||
public void Accept(IAcceptanceCriteria<IIdentityInspector> criteria)
|
||||
{
|
||||
criteria.Expect(x => x.Name == x.EntityType.Name + "ID");
|
||||
}
|
||||
}
|
||||
}
|
||||
38
Tanshu.Accounts.Repository/Fluent/QueryStore.cs
Normal file
38
Tanshu.Accounts.Repository/Fluent/QueryStore.cs
Normal file
@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using FluentNHibernate.Conventions;
|
||||
using FluentNHibernate.Conventions.Instances;
|
||||
using NHibernate;
|
||||
using NHibernate.SqlCommand;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Tanshu.Accounts.SqlDAO
|
||||
{
|
||||
public static class QueryStore
|
||||
{
|
||||
private static string query;
|
||||
public static string Query
|
||||
{
|
||||
get
|
||||
{
|
||||
return query;
|
||||
}
|
||||
set
|
||||
{
|
||||
query = value;
|
||||
Debug.Write(query);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class NHSQLInterceptor : EmptyInterceptor, IInterceptor
|
||||
{
|
||||
SqlString IInterceptor.OnPrepareStatement(SqlString sql)
|
||||
{
|
||||
QueryStore.Query = sql.ToString();
|
||||
return sql;
|
||||
}
|
||||
}
|
||||
}
|
||||
80
Tanshu.Accounts.Repository/Fluent/SetupStore.cs
Normal file
80
Tanshu.Accounts.Repository/Fluent/SetupStore.cs
Normal file
@ -0,0 +1,80 @@
|
||||
using FluentNHibernate.Automapping;
|
||||
using FluentNHibernate.Cfg;
|
||||
using FluentNHibernate.Cfg.Db;
|
||||
using NHibernate;
|
||||
using NHibernate.Cfg;
|
||||
using Tanshu.Accounts.Conventions;
|
||||
using Tanshu.Accounts.Entities;
|
||||
using Tanshu.Accounts.SqlDAO;
|
||||
|
||||
namespace Tanshu.Accounts.Repository
|
||||
{
|
||||
public sealed class SessionManager
|
||||
{
|
||||
private readonly Configuration cfg;
|
||||
private readonly ISessionFactory factory;
|
||||
private SessionManager()
|
||||
{
|
||||
cfg = BuildConfiguration();
|
||||
factory = cfg.BuildSessionFactory();
|
||||
}
|
||||
|
||||
//public static SessionManager Instance
|
||||
//{
|
||||
// get { return Nested.sessionManager; }
|
||||
//}
|
||||
public static ISession Session
|
||||
{
|
||||
get { return Nested.sessionManager.GetSession(); }
|
||||
}
|
||||
public static IStatelessSession StatelessSession
|
||||
{
|
||||
get { return Nested.sessionManager.GetStatelessSession(); }
|
||||
}
|
||||
public static Configuration Configuration
|
||||
{
|
||||
get { return Nested.sessionManager.GetConfiguration(); }
|
||||
}
|
||||
private class Nested
|
||||
{
|
||||
static Nested() { }
|
||||
internal static readonly SessionManager sessionManager = new SessionManager();
|
||||
}
|
||||
|
||||
private Configuration GetConfiguration()
|
||||
{
|
||||
return cfg;
|
||||
}
|
||||
private Configuration BuildConfiguration()
|
||||
{
|
||||
var storeConfiguration = new StoreConfiguration();
|
||||
var persistenceModel = AutoMap.AssemblyOf<Voucher>(storeConfiguration)
|
||||
.Conventions.Setup(c =>
|
||||
{
|
||||
c.Add<PrimaryKeyConvention>();
|
||||
c.Add<CustomForeignKeyConvention>();
|
||||
c.Add<ClassConvention>();
|
||||
c.Add<CascadeConvention>();
|
||||
c.Add<UniqueConvention>();
|
||||
c.Add<NotNullConvention>();
|
||||
c.Add<FormulaConvention>();
|
||||
//c.Add<AllowNullConvention>();
|
||||
c.Add<EnumConvention>();
|
||||
});
|
||||
|
||||
return Fluently.Configure()
|
||||
.Database(MsSqlConfiguration.MsSql2005.ConnectionString(p => p.FromConnectionStringWithKey("FluentCon")))
|
||||
.Mappings(m => m.AutoMappings.Add(persistenceModel))
|
||||
.BuildConfiguration()
|
||||
.SetInterceptor(new NHSQLInterceptor());
|
||||
}
|
||||
private ISession GetSession()
|
||||
{
|
||||
return factory.OpenSession();
|
||||
}
|
||||
private IStatelessSession GetStatelessSession()
|
||||
{
|
||||
return factory.OpenStatelessSession();
|
||||
}
|
||||
}
|
||||
}
|
||||
37
Tanshu.Accounts.Repository/Fluent/StoreConfiguration.cs
Normal file
37
Tanshu.Accounts.Repository/Fluent/StoreConfiguration.cs
Normal file
@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using FluentNHibernate;
|
||||
using FluentNHibernate.Automapping;
|
||||
|
||||
namespace Tanshu.Accounts.Repository
|
||||
{
|
||||
public class StoreConfiguration : DefaultAutomappingConfiguration
|
||||
{
|
||||
public override bool ShouldMap(Type type)
|
||||
{
|
||||
bool val = type.Namespace.StartsWith("Tanshu.Accounts.Entities");
|
||||
if (type.IsEnum)
|
||||
val = false;
|
||||
return val;
|
||||
}
|
||||
public override bool IsId(Member member)
|
||||
{
|
||||
string key = member.DeclaringType.Name + "ID";
|
||||
return key == member.Name;
|
||||
}
|
||||
public override bool IsDiscriminated(Type type)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
public override string SimpleTypeCollectionValueColumn(Member member)
|
||||
{
|
||||
return base.SimpleTypeCollectionValueColumn(member);
|
||||
}
|
||||
public override bool ShouldMap(Member member)
|
||||
{
|
||||
//if (AttributeChecker.Formula(member.MemberInfo))
|
||||
// return false;
|
||||
//else
|
||||
return base.ShouldMap(member);
|
||||
}
|
||||
}
|
||||
}
|
||||
27
Tanshu.Accounts.Repository/Fluent/UniqueConvention.cs
Normal file
27
Tanshu.Accounts.Repository/Fluent/UniqueConvention.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using FluentNHibernate.Conventions;
|
||||
using System.Reflection;
|
||||
using FluentNHibernate;
|
||||
using FluentNHibernate.Conventions.Instances;
|
||||
using FluentNHibernate.Conventions.Inspections;
|
||||
using FluentNHibernate.Conventions.AcceptanceCriteria;
|
||||
using Tanshu.Accounts.Contracts;
|
||||
using Tanshu.Accounts.SqlDAO;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Tanshu.Accounts.Conventions
|
||||
{
|
||||
public class UniqueConvention : IPropertyConvention, IPropertyConventionAcceptance
|
||||
{
|
||||
public void Apply(IPropertyInstance instance)
|
||||
{
|
||||
var property = instance.Property;
|
||||
Trace.TraceInformation("Unique on {0}.{1}", property.DeclaringType.Name, property.Name);
|
||||
instance.Unique();
|
||||
}
|
||||
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
|
||||
{
|
||||
criteria.Expect(x => AttributeChecker.Unique(x.Property.MemberInfo));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user