Moved to fluent. Added support for Modifiers. Fixtures to load intial test data
This commit is contained in:
1 parent
c841dc25f4
commit
eb48c3754a
272 files changed
+10470
-47160
No files matched your search
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using FluentNHibernate.Conventions;
|
||||
using FluentNHibernate.Conventions.Instances;
|
||||
|
||||
namespace Tanshu.Accounts.SqlDAO
|
||||
{
|
||||
public class ClassConvention : IClassConvention
|
||||
{
|
||||
public void Apply(IClassInstance instance)
|
||||
{
|
||||
string table = Inflector.Pluralize(instance.EntityType.Name);
|
||||
table = instance.EntityType.Namespace.Substring(instance.EntityType.Namespace.LastIndexOf('.') + 1) + "_" + table;
|
||||
instance.Table(table);
|
||||
//instance.Cache.NonStrictReadWrite();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using FluentNHibernate.Conventions;
|
||||
using System.Reflection;
|
||||
using FluentNHibernate;
|
||||
using FluentNHibernate.Conventions.Instances;
|
||||
using FluentNHibernate.Conventions.Inspections;
|
||||
using FluentNHibernate.Conventions.AcceptanceCriteria;
|
||||
|
||||
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)));
|
||||
//criteria.Expect(x => x.Property.PropertyType.IsEnum);
|
||||
}
|
||||
|
||||
public void Apply(IPropertyInstance target)
|
||||
{
|
||||
target.CustomType(target.Property.PropertyType);
|
||||
}
|
||||
}
|
||||
|
||||
//public class EnumConvention : IPropertyConvention, IPropertyConventionAcceptance
|
||||
//{
|
||||
// #region IPropertyConvention Members
|
||||
|
||||
// public void Apply(IPropertyInstance instance)
|
||||
// {
|
||||
// instance.CustomType(instance.Property.PropertyType);
|
||||
// }
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region IPropertyConventionAcceptance Members
|
||||
|
||||
// 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)
|
||||
// );
|
||||
// }
|
||||
|
||||
// #endregion
|
||||
|
||||
//}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,330 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using FluentNHibernate.Automapping;
|
||||
using Tanshu.Accounts.Contracts;
|
||||
using FluentNHibernate.Cfg;
|
||||
using FluentNHibernate.Cfg.Db;
|
||||
using Tanshu.Accounts.Conventions;
|
||||
using Tanshu.Accounts.Entities;
|
||||
using NHibernate.Tool.hbm2ddl;
|
||||
using NHibernate;
|
||||
using NHibernate.Cfg;
|
||||
using Tanshu.Accounts.Entities.Auth;
|
||||
|
||||
namespace Tanshu.Accounts.SqlDAO
|
||||
{
|
||||
public static class Fixtures
|
||||
{
|
||||
public static bool CreateSchema()
|
||||
{
|
||||
var cfg = SessionManager.Configuration;
|
||||
new SchemaExport(cfg).Execute(true, true, true);
|
||||
new SchemaExport(cfg).Execute(true, true, false);
|
||||
// next line is just for checking how the nhibernate xml configuration files look
|
||||
//persistenceModel.WriteMappingsTo(@"C:\Temp");
|
||||
Fixtures.InsertRole();
|
||||
Fixtures.InsertUser();
|
||||
Fixtures.InsertGroup();
|
||||
Fixtures.InsertUserGroup();
|
||||
Fixtures.InsertRoleGroup();
|
||||
Fixtures.InsertCustomer();
|
||||
Fixtures.InsertTax();
|
||||
Fixtures.InsertWaiter();
|
||||
Fixtures.InsertPrintLocation();
|
||||
Fixtures.InsertSettleOption();
|
||||
|
||||
InsertFoodTable();
|
||||
InsertProductGroup();
|
||||
InsertProductBeer();
|
||||
InsertModifier();
|
||||
InsertProductGroupModifier();
|
||||
return true;
|
||||
}
|
||||
|
||||
#region role
|
||||
public static void InsertRole()
|
||||
{
|
||||
using (var session = SessionManager.Session)
|
||||
{
|
||||
session.Save(GetRole(1, "Sales/Checkout"));
|
||||
session.Save(GetRole(2, "Sales/SalesBill"));
|
||||
session.Save(GetRole(3, "Sales/SaleDetail"));
|
||||
session.Save(GetRole(4, "Sales/VoidPrintedBill"));
|
||||
session.Save(GetRole(5, "Sales/EditBill"));
|
||||
session.Save(GetRole(6, "Sales/PrintKOT"));
|
||||
session.Save(GetRole(7, "Sales/PrintBill"));
|
||||
session.Save(GetRole(8, "Sales/EditPrintedProduct"));
|
||||
session.Save(GetRole(9, "Sales/ChangeRate"));
|
||||
session.Save(GetRole(10, "Master/Products"));
|
||||
session.Save(GetRole(11, "Security/ManageRoles"));
|
||||
session.Save(GetRole(12, "Log/View"));
|
||||
session.Save(GetRole(13, "Master/Owner"));
|
||||
}
|
||||
}
|
||||
private static Role GetRole(int roleID, string name)
|
||||
{
|
||||
return new Role()
|
||||
{
|
||||
RoleID = roleID,
|
||||
Name = name
|
||||
};
|
||||
}
|
||||
#endregion
|
||||
#region user
|
||||
public static void InsertUser()
|
||||
{
|
||||
using (var session = SessionManager.Session)
|
||||
{
|
||||
session.Save(GetUser(1, "admin", "password"));
|
||||
}
|
||||
}
|
||||
private static User GetUser(int userID, string name, string password)
|
||||
{
|
||||
return new User()
|
||||
{
|
||||
UserID = userID,
|
||||
Name = name,
|
||||
Password = Tanshu.Common.Md5.Hash(password, "v2"),
|
||||
LockedOut = false
|
||||
};
|
||||
}
|
||||
#endregion
|
||||
#region group
|
||||
public static void InsertGroup()
|
||||
{
|
||||
using (var session = SessionManager.Session)
|
||||
{
|
||||
session.Save(GetGroup(1, "owner"));
|
||||
session.Save(GetGroup(2, "waiter"));
|
||||
session.Save(GetGroup(3, "captain"));
|
||||
session.Save(GetGroup(4, "manager"));
|
||||
session.Save(GetGroup(5, "controller"));
|
||||
session.Save(GetGroup(6, "accountant"));
|
||||
}
|
||||
}
|
||||
private static Group GetGroup(int groupID, string name)
|
||||
{
|
||||
return new Group()
|
||||
{
|
||||
GroupID = groupID,
|
||||
Name = name
|
||||
};
|
||||
}
|
||||
#endregion
|
||||
#region user_group
|
||||
public static void InsertUserGroup()
|
||||
{
|
||||
using (var session = SessionManager.Session)
|
||||
{
|
||||
var admin = session.Get<User>(1);
|
||||
var list = session.CreateCriteria<Group>()
|
||||
.List<Group>();
|
||||
foreach (var item in list)
|
||||
{
|
||||
session.Save(new UserGroup()
|
||||
{
|
||||
User = admin,
|
||||
Group = item
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
#region role_group
|
||||
public static void InsertRoleGroup()
|
||||
{
|
||||
using (var session = SessionManager.Session)
|
||||
{
|
||||
var owner = session.Get<Group>(1);
|
||||
var list = session.CreateCriteria<Role>()
|
||||
.List<Role>();
|
||||
foreach (var item in list)
|
||||
{
|
||||
session.Save(new RoleGroup()
|
||||
{
|
||||
Role = item,
|
||||
Group = owner
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
#region customer
|
||||
public static void InsertCustomer()
|
||||
{
|
||||
using (var session = SessionManager.Session)
|
||||
{
|
||||
session.Save(new Customer()
|
||||
{
|
||||
CustomerID = 1,
|
||||
Code = 1,
|
||||
Name = "Cash",
|
||||
Address = "",
|
||||
Important = false,
|
||||
Phone = "",
|
||||
Remarks = ""
|
||||
});
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
#region tax
|
||||
public static void InsertTax()
|
||||
{
|
||||
using (var session = SessionManager.Session)
|
||||
{
|
||||
session.Save(new Tax()
|
||||
{
|
||||
TaxID = 1,
|
||||
Name = "VAT + SC 12.5%",
|
||||
Rate = .13125M
|
||||
});
|
||||
session.Save(new Tax()
|
||||
{
|
||||
TaxID = 1,
|
||||
Name = "VAT + SC 25%",
|
||||
Rate = .2625M
|
||||
});
|
||||
session.Save(new Tax()
|
||||
{
|
||||
TaxID = 1,
|
||||
Name = "Service Tax 10.3%",
|
||||
Rate = .103M
|
||||
});
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
#region waiter
|
||||
public static void InsertWaiter()
|
||||
{
|
||||
using (var session = SessionManager.Session)
|
||||
{
|
||||
session.Save(new Waiter()
|
||||
{
|
||||
WaiterID = 1,
|
||||
Name = "Waiter",
|
||||
});
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
#region print_location
|
||||
public static void InsertPrintLocation()
|
||||
{
|
||||
using (var session = SessionManager.Session)
|
||||
{
|
||||
session.Save(new PrintLocation()
|
||||
{
|
||||
PrintLocationID = 1,
|
||||
Location = "Ground",
|
||||
Copies = 2,
|
||||
Printer = @"\\gandalf\kitchen",
|
||||
ProductGroup = null,
|
||||
CutCode = "\n\r\n\r\n\r\n\r\n\r\n\r" + (char)29 + (char)86 + (char)49
|
||||
});
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
#region settle_option
|
||||
public static void InsertSettleOption()
|
||||
{
|
||||
using (var session = SessionManager.Session)
|
||||
{
|
||||
session.Save(SettleOptionFactory.OUnsettled);
|
||||
session.Save(SettleOptionFactory.OCash);
|
||||
session.Save(SettleOptionFactory.OCreditCard);
|
||||
session.Save(SettleOptionFactory.ONoCharge);
|
||||
session.Save(SettleOptionFactory.OBillToCompany);
|
||||
session.Save(SettleOptionFactory.OStaff);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region food_table
|
||||
public static void InsertFoodTable()
|
||||
{
|
||||
using (var session = SessionManager.Session)
|
||||
{
|
||||
session.Save(new FoodTable() { FoodTableID = 1, Name = "1" });
|
||||
session.Save(new FoodTable() { FoodTableID = 2, Name = "2" });
|
||||
session.Save(new FoodTable() { FoodTableID = 3, Name = "3" });
|
||||
session.Save(new FoodTable() { FoodTableID = 4, Name = "4" });
|
||||
session.Save(new FoodTable() { FoodTableID = 5, Name = "5" });
|
||||
session.Save(new FoodTable() { FoodTableID = 6, Name = "6" });
|
||||
session.Save(new FoodTable() { FoodTableID = 7, Name = "7" });
|
||||
session.Save(new FoodTable() { FoodTableID = 8, Name = "8" });
|
||||
session.Save(new FoodTable() { FoodTableID = 9, Name = "9" });
|
||||
session.Save(new FoodTable() { FoodTableID = 10, Name = "10" });
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
#region product_group
|
||||
public static void InsertProductGroup()
|
||||
{
|
||||
using (var session = SessionManager.Session)
|
||||
{
|
||||
session.Save(new ProductGroup() { ProductGroupID = 1, Name = "Beer", DiscountLimit = 0, IsModifierCompulsory = false });
|
||||
session.Save(new ProductGroup() { ProductGroupID = 2, Name = "Imported Alcohol", DiscountLimit = 0, IsModifierCompulsory = false });
|
||||
session.Save(new ProductGroup() { ProductGroupID = 3, Name = "IMFL Alcohol", DiscountLimit = 0, IsModifierCompulsory = false });
|
||||
session.Save(new ProductGroup() { ProductGroupID = 4, Name = "Cocktails", DiscountLimit = 0, IsModifierCompulsory = false });
|
||||
session.Save(new ProductGroup() { ProductGroupID = 5, Name = "Indian Starters", DiscountLimit = 0, IsModifierCompulsory = false });
|
||||
session.Save(new ProductGroup() { ProductGroupID = 6, Name = "International Starters", DiscountLimit = 0, IsModifierCompulsory = false });
|
||||
session.Save(new ProductGroup() { ProductGroupID = 7, Name = "Indian Maincourse", DiscountLimit = 0, IsModifierCompulsory = false });
|
||||
session.Save(new ProductGroup() { ProductGroupID = 8, Name = "International Maincourse", DiscountLimit = 0, IsModifierCompulsory = false });
|
||||
session.Save(new ProductGroup() { ProductGroupID = 9, Name = "Pastas", DiscountLimit = 0, IsModifierCompulsory = false });
|
||||
session.Save(new ProductGroup() { ProductGroupID = 10, Name = "Desserts", DiscountLimit = 0, IsModifierCompulsory = false });
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
#region product_group
|
||||
public static void InsertProductBeer()
|
||||
{
|
||||
using (var session = SessionManager.Session)
|
||||
{
|
||||
var tax = session.Get<Tax>(2);
|
||||
var pg = session.Get<ProductGroup>(1);
|
||||
session.Save(new Product() { ProductID = 1, Code = 1, Name = "Wheat Beer", Units = ".5L Mug", SalePrice = 185, ServiceCharge = .1M, Discontinued = false, MaximumLevel = 0, MinimumLevel = 0, ProductGroup = pg, SortOrder = 1, Tax = tax });
|
||||
session.Save(new Product() { ProductID = 2, Code = 2, Name = "Light Beer", Units = ".5L Mug", SalePrice = 160, ServiceCharge = .1M, Discontinued = false, MaximumLevel = 0, MinimumLevel = 0, ProductGroup = pg, SortOrder = 1, Tax = tax });
|
||||
session.Save(new Product() { ProductID = 3, Code = 3, Name = "Dark Beer", Units = ".5L Mug", SalePrice = 160, ServiceCharge = .1M, Discontinued = false, MaximumLevel = 0, MinimumLevel = 0, ProductGroup = pg, SortOrder = 1, Tax = tax });
|
||||
session.Save(new Product() { ProductID = 4, Code = 4, Name = "Premium Beer", Units = ".5L Mug", SalePrice = 185, ServiceCharge = .1M, Discontinued = false, MaximumLevel = 0, MinimumLevel = 0, ProductGroup = pg, SortOrder = 1, Tax = tax });
|
||||
session.Save(new Product() { ProductID = 5, Code = 5, Name = "Wheat Beer", Units = "2L Pcr", SalePrice = 650, ServiceCharge = .1M, Discontinued = false, MaximumLevel = 0, MinimumLevel = 0, ProductGroup = pg, SortOrder = 1, Tax = tax });
|
||||
session.Save(new Product() { ProductID = 6, Code = 6, Name = "Light Beer", Units = "2L Pcr", SalePrice = 550, ServiceCharge = .1M, Discontinued = false, MaximumLevel = 0, MinimumLevel = 0, ProductGroup = pg, SortOrder = 1, Tax = tax });
|
||||
session.Save(new Product() { ProductID = 7, Code = 7, Name = "Dark Beer", Units = "2L Pcr", SalePrice = 550, ServiceCharge = .1M, Discontinued = false, MaximumLevel = 0, MinimumLevel = 0, ProductGroup = pg, SortOrder = 1, Tax = tax });
|
||||
session.Save(new Product() { ProductID = 8, Code = 8, Name = "Premium Beer", Units = "2L Pcr", SalePrice = 650, ServiceCharge = .1M, Discontinued = false, MaximumLevel = 0, MinimumLevel = 0, ProductGroup = pg, SortOrder = 1, Tax = tax });
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
#region modifiers
|
||||
public static void InsertModifier()
|
||||
{
|
||||
using (var session = SessionManager.Session)
|
||||
{
|
||||
session.Save(new Modifier() { ModifierID = 1, Name = "On the Rocks" });
|
||||
session.Save(new Modifier() { ModifierID = 2, Name = "Frozen" });
|
||||
session.Save(new Modifier() { ModifierID = 3, Name = "Soda" });
|
||||
session.Save(new Modifier() { ModifierID = 4, Name = "Water" });
|
||||
|
||||
session.Save(new Modifier() { ModifierID = 5, Name = "Spicy" });
|
||||
session.Save(new Modifier() { ModifierID = 6, Name = "HOLD" });
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
#region product_group_modifiers
|
||||
public static void InsertProductGroupModifier()
|
||||
{
|
||||
using (var session = SessionManager.Session)
|
||||
{
|
||||
using (var trans = session.BeginTransaction())
|
||||
{
|
||||
foreach (var item in session.CreateCriteria<Modifier>().List<Modifier>())
|
||||
{
|
||||
session.Save(new ProductGroupModifier() { Modifier = item });
|
||||
}
|
||||
trans.Commit();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
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;
|
||||
|
||||
namespace Tanshu.Accounts.Conventions
|
||||
{
|
||||
public class PrimaryKeyConvention : IIdConvention, IIdConventionAcceptance
|
||||
{
|
||||
public void Apply(IIdentityInstance instance)
|
||||
{
|
||||
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.EndsWith("ID"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
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;
|
||||
using Tanshu.Accounts.Entities;
|
||||
|
||||
namespace Tanshu.Accounts.SqlDAO
|
||||
{
|
||||
public static class SettleOptionFactory
|
||||
{
|
||||
public static SettleOption OUnsettled = new SettleOption() { SettleOptionID = 1, Name = "Unsettled" };
|
||||
public static SettleOption OCash = new SettleOption() { SettleOptionID = 2, Name = "Cash" };
|
||||
public static SettleOption OCreditCard = new SettleOption() { SettleOptionID = 3, Name = "CreditCard" };
|
||||
public static SettleOption ONoCharge = new SettleOption() { SettleOptionID = 4, Name = "NoCharge" };
|
||||
public static SettleOption OBillToCompany = new SettleOption() { SettleOptionID = 5, Name = "BillToCompany" };
|
||||
public static SettleOption OStaff = new SettleOption() { SettleOptionID = 6, Name = "Staff" };
|
||||
|
||||
public static int Unsettled = 1;
|
||||
public static int Cash = 2;
|
||||
public static int CreditCard = 3;
|
||||
public static int NoCharge = 4;
|
||||
public static int BillToCompany = 5;
|
||||
public static int Staff = 6;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using FluentNHibernate.Automapping;
|
||||
using Tanshu.Accounts.Contracts;
|
||||
using FluentNHibernate.Cfg;
|
||||
using FluentNHibernate.Cfg.Db;
|
||||
using Tanshu.Accounts.Conventions;
|
||||
using Tanshu.Accounts.Entities;
|
||||
using NHibernate.Tool.hbm2ddl;
|
||||
using NHibernate;
|
||||
using NHibernate.Cfg;
|
||||
using FluentNHibernate.Conventions.Helpers;
|
||||
|
||||
namespace Tanshu.Accounts.SqlDAO
|
||||
{
|
||||
public sealed class SessionManager
|
||||
{
|
||||
private readonly Configuration cfg;
|
||||
private readonly ISessionFactory factory;
|
||||
private SessionManager()
|
||||
{
|
||||
cfg = GetConfiguration();
|
||||
factory = cfg.BuildSessionFactory();
|
||||
}
|
||||
|
||||
//public static SessionManager Instance
|
||||
//{
|
||||
// get { return Nested.sessionManager; }
|
||||
//}
|
||||
public static ISession Session
|
||||
{
|
||||
get { return Nested.sessionManager.GetSession(); }
|
||||
}
|
||||
public static Configuration Configuration
|
||||
{
|
||||
get { return Nested.sessionManager.GetConfiguration(); }
|
||||
}
|
||||
private class Nested
|
||||
{
|
||||
static Nested() { }
|
||||
internal static readonly SessionManager sessionManager = new SessionManager();
|
||||
}
|
||||
|
||||
public Configuration GetConfiguration()
|
||||
{
|
||||
var storeConfiguration = new StoreConfiguration();
|
||||
var persistenceModel = AutoMap.AssemblyOf<Voucher>(storeConfiguration)
|
||||
.Conventions.Setup(c =>
|
||||
{
|
||||
c.Add<PrimaryKeyConvention>();
|
||||
c.Add<CustomForeignKeyConvention>();
|
||||
c.Add<ClassConvention>();
|
||||
c.Add(DefaultLazy.Never());
|
||||
//c.Add<EnumConvention>();
|
||||
});
|
||||
|
||||
return Fluently.Configure()
|
||||
.Database(MsSqlConfiguration.MsSql2005.ConnectionString(p => p.FromConnectionStringWithKey("FluentCon")))
|
||||
.Mappings(m => m.AutoMappings.Add(persistenceModel))
|
||||
.BuildConfiguration()
|
||||
.SetInterceptor(new NHSQLInterceptor());
|
||||
}
|
||||
public ISession GetSession()
|
||||
{
|
||||
return factory.OpenSession();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Diagnostics;
|
||||
using Tanshu.Data.DAO;
|
||||
using FluentNHibernate.Automapping;
|
||||
using FluentNHibernate;
|
||||
|
||||
namespace Tanshu.Accounts.SqlDAO
|
||||
{
|
||||
public class StoreConfiguration : DefaultAutomappingConfiguration
|
||||
{
|
||||
public override bool ShouldMap(Type type)
|
||||
{
|
||||
return type.Namespace.StartsWith("Tanshu.Accounts.Entities");
|
||||
}
|
||||
public override bool IsId(Member member)
|
||||
{
|
||||
string key = member.DeclaringType.Name + "ID";
|
||||
return key == member.Name;
|
||||
}
|
||||
public override bool IsDiscriminated(Type type)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user