├── README.md ├── Models ├── Domain │ ├── Project.cs │ ├── Gender.cs │ ├── PersonType.cs │ ├── TransactionType.cs │ ├── AccountType.cs │ ├── Bank.cs │ ├── CashBox.cs │ ├── PersonGroup.cs │ ├── Product.cs │ ├── Party.cs │ ├── Transaction.cs │ ├── Entry.cs │ ├── Person.cs │ └── Account.cs ├── Common │ ├── Constants.cs │ └── Currency.cs ├── Mappings │ ├── ProjectMap.cs │ ├── BankMap.cs │ ├── CashBoxMap.cs │ ├── ProductMap.cs │ ├── CurrencyMap.cs │ ├── PersonGroupMap.cs │ ├── PersonMap.cs │ ├── TransactionMap.cs │ ├── EntryMap.cs │ ├── PartyMap.cs │ └── AccountMap.cs ├── packages.config ├── Properties │ └── AssemblyInfo.cs └── Models.csproj ├── DataLayer ├── UnitOfWorks │ ├── IUnitOfWorkImplementor.cs │ ├── ILocalData.cs │ ├── IGenericTransaction.cs │ ├── IUnitOfWorkFactory.cs │ ├── IUnitOfWork.cs │ ├── GenericTransaction.cs │ ├── With.cs │ ├── UnitOfWork.cs │ ├── Local.cs │ ├── UnitOfWorkImplementor.cs │ └── UnitOfWorkFactory.cs ├── Constants.cs ├── packages.config ├── hibernate.cfg.xml ├── Properties │ └── AssemblyInfo.cs ├── Repository │ ├── RepositoryHelper.cs │ ├── IRepository.cs │ └── Repository.cs └── DataLayer.csproj ├── Services ├── EntityEventArgs.cs ├── packages.config ├── IService.cs ├── AccountingService.cs ├── Properties │ └── AssemblyInfo.cs ├── EntityService.cs └── Services.csproj ├── Accounting.Console ├── App.config ├── packages.config ├── Properties │ └── AssemblyInfo.cs ├── Program.cs └── Accounting.Console.csproj ├── DataLayer.Tests ├── packages.config ├── hibernate.cfg.xml ├── Properties │ └── AssemblyInfo.cs └── DataLayer.Tests.csproj ├── Entities ├── Domain │ └── Product.cs ├── Mappings │ └── Product.hbm.xml ├── Properties │ └── AssemblyInfo.cs └── Entities.csproj ├── .gitattributes ├── Accounting.sln └── .gitignore /README.md: -------------------------------------------------------------------------------- 1 | # Accounting 2 | Double entry accounting in C# 3 | -------------------------------------------------------------------------------- /Models/Domain/Project.cs: -------------------------------------------------------------------------------- 1 | namespace Models.Domain 2 | { 3 | public class Project : Party 4 | { 5 | 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Models/Domain/Gender.cs: -------------------------------------------------------------------------------- 1 | namespace Models.Domain 2 | { 3 | public enum Gender 4 | { 5 | MALE, 6 | FEMALE, 7 | NOT_SPECIFIED 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Models/Domain/PersonType.cs: -------------------------------------------------------------------------------- 1 | namespace Models.Domain 2 | { 3 | public enum PersonType 4 | { 5 | 6 | PERSON, 7 | COMPANY, 8 | ORGANIZATION 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /DataLayer/UnitOfWorks/IUnitOfWorkImplementor.cs: -------------------------------------------------------------------------------- 1 | namespace DataLayer.UnitOfWorks 2 | { 3 | public interface IUnitOfWorkImplementor : IUnitOfWork 4 | { 5 | void IncrementUsages(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /DataLayer/Constants.cs: -------------------------------------------------------------------------------- 1 | namespace DataLayer 2 | { 3 | public static class Constants 4 | { 5 | public static string Default_HibernateConfig { get { return "hibernate.cfg.xml"; } } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Services/EntityEventArgs.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Services 4 | { 5 | public class EntityEventArgs :EventArgs 6 | { 7 | 8 | public T Entity { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Models/Common/Constants.cs: -------------------------------------------------------------------------------- 1 | namespace Models.Common 2 | { 3 | public static class Constants 4 | { 5 | public static char ACCOUNT_CODE_DELIMITER = '/'; 6 | 7 | } 8 | 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Models/Domain/TransactionType.cs: -------------------------------------------------------------------------------- 1 | namespace Models.Domain 2 | { 3 | public enum TransactionType 4 | { 5 | DRAFT, 6 | TEMPRORY, 7 | ACCEPTED, 8 | CLOSED 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Accounting.Console/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Models/Domain/AccountType.cs: -------------------------------------------------------------------------------- 1 | namespace Models.Domain 2 | { 3 | public enum AccountType 4 | { 5 | ASSETS, 6 | LIABILITY, 7 | REVENUE, 8 | EXPENCE, 9 | CAPITAL 10 | 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Models/Domain/Bank.cs: -------------------------------------------------------------------------------- 1 | namespace Models.Domain 2 | { 3 | public class Bank : Party 4 | { 5 | public virtual string Branch { get; set; } 6 | 7 | public virtual string BranchCode { get; set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /DataLayer/UnitOfWorks/ILocalData.cs: -------------------------------------------------------------------------------- 1 | namespace DataLayer.UnitOfWorks 2 | { 3 | public interface ILocalData 4 | { 5 | object this[object key] { get; set; } 6 | int Count { get; } 7 | void Clear(); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /DataLayer/UnitOfWorks/IGenericTransaction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace DataLayer.UnitOfWorks 4 | { 5 | public interface IGenericTransaction : IDisposable 6 | { 7 | void Commit(); 8 | void Rollback(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Services/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /Models/Domain/CashBox.cs: -------------------------------------------------------------------------------- 1 | namespace Models.Domain 2 | { 3 | public class CashBox : Party 4 | { 5 | public virtual int BoxNumber { get; set; } 6 | 7 | public virtual string Address { get; set; } 8 | 9 | 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /DataLayer.Tests/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /DataLayer/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Models/Mappings/ProjectMap.cs: -------------------------------------------------------------------------------- 1 | using FluentNHibernate.Mapping; 2 | using Models.Domain; 3 | 4 | namespace Models.Mappings 5 | { 6 | public class ProjectMap : SubclassMap 7 | { 8 | public ProjectMap() { 9 | Table("Projects"); 10 | KeyColumn("Id"); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Models/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Models/Domain/PersonGroup.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Models.Domain 4 | { 5 | public class PersonGroup 6 | { 7 | public virtual int Id { get; set; } 8 | public virtual string GroupName { get; set; } 9 | public virtual IList People { get; set; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Models/Domain/Product.cs: -------------------------------------------------------------------------------- 1 | namespace Models.Domain 2 | { 3 | public class Product 4 | { 5 | 6 | public virtual int Id { get; set; } 7 | public virtual string Name { get; set; } 8 | public virtual string Category { get; set; } 9 | public virtual int Price { get; set; } 10 | 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Accounting.Console/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Entities/Domain/Product.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Entities.Domain 4 | { 5 | public class Product 6 | { 7 | 8 | public virtual Guid Id { get; set; } 9 | public virtual string Name { get; set; } 10 | public virtual string Category { get; set; } 11 | public virtual int Price { get; set; } 12 | 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Models/Common/Currency.cs: -------------------------------------------------------------------------------- 1 | namespace Models.Common 2 | { 3 | public class Currency 4 | { 5 | public virtual int Id { get; set; } 6 | public virtual string Country { get; set; } 7 | public virtual string CurrencyName { get; set; } 8 | public virtual string Sign { get; set; } 9 | public virtual int Rate { get; set; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Models/Mappings/BankMap.cs: -------------------------------------------------------------------------------- 1 | using FluentNHibernate.Mapping; 2 | using Models.Domain; 3 | 4 | namespace Models.Mappings 5 | { 6 | public class BankMap : SubclassMap 7 | { 8 | 9 | public BankMap() { 10 | 11 | Table("Banks"); 12 | KeyColumn("Id"); 13 | Map(x => x.Branch); 14 | Map(x => x.BranchCode); 15 | 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Models/Mappings/CashBoxMap.cs: -------------------------------------------------------------------------------- 1 | using FluentNHibernate.Mapping; 2 | using Models.Domain; 3 | 4 | namespace Models.Mappings 5 | { 6 | public class CashBoxMap: SubclassMap 7 | { 8 | public CashBoxMap() { 9 | 10 | Table("CashBoxes"); 11 | KeyColumn("Id"); 12 | 13 | Map(x => x.Address); 14 | Map(x => x.BoxNumber); 15 | 16 | 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Models/Mappings/ProductMap.cs: -------------------------------------------------------------------------------- 1 | using FluentNHibernate.Mapping; 2 | using Models.Domain; 3 | 4 | namespace Models.Mappings 5 | { 6 | public class ProductMap : ClassMap 7 | { 8 | public ProductMap() { 9 | Table("Products"); 10 | Id(x => x.Id); 11 | Map(x => x.Name).Length(500).Not.Nullable(); 12 | Map(x => x.Price); 13 | Map(x => x.Category); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /DataLayer/UnitOfWorks/IUnitOfWorkFactory.cs: -------------------------------------------------------------------------------- 1 | using NHibernate; 2 | using NHibernate.Cfg; 3 | 4 | namespace DataLayer.UnitOfWorks 5 | { 6 | public interface IUnitOfWorkFactory 7 | { 8 | Configuration Configuration { get; } 9 | ISessionFactory SessionFactory { get; } 10 | ISession CurrentSession { get; set; } 11 | 12 | IUnitOfWork Create(); 13 | void DisposeUnitOfWork(UnitOfWorkImplementor adapter); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Entities/Mappings/Product.hbm.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Models/Domain/Party.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Models.Domain 4 | { 5 | public class Party 6 | { 7 | public virtual int Id { get; set; } 8 | public virtual string PartyName { get; set; } 9 | public virtual string PartyCode { get; set; } 10 | public virtual string Description { get; set; } 11 | public virtual decimal TrialBalance { get; set; } 12 | 13 | public virtual IList Entries { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /DataLayer/UnitOfWorks/IUnitOfWork.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | 4 | namespace DataLayer.UnitOfWorks 5 | { 6 | public interface IUnitOfWork : IDisposable 7 | { 8 | void Flush(); 9 | bool IsInActiveTransaction { get; } 10 | 11 | IGenericTransaction BeginTransaction(); 12 | IGenericTransaction BeginTransaction(IsolationLevel isolationLevel); 13 | void TransactionalFlush(); 14 | void TransactionalFlush(IsolationLevel isolationLevel); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Models/Mappings/CurrencyMap.cs: -------------------------------------------------------------------------------- 1 | using FluentNHibernate.Mapping; 2 | using Models.Common; 3 | 4 | namespace Models.Mappings 5 | { 6 | public class CurrencyMap : ClassMap 7 | { 8 | public CurrencyMap() { 9 | Table("Currencies"); 10 | Id(x => x.Id) 11 | .Column("Id") 12 | .Not.Nullable() 13 | .GeneratedBy.Identity(); 14 | 15 | Map(x => x.Country); 16 | Map(x => x.CurrencyName); 17 | Map(x => x.Rate); 18 | Map(x => x.Sign); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /DataLayer/hibernate.cfg.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | NHibernate.Driver.SQLite20Driver 6 | Data Source=accounting-core.db; 7 | NHibernate.Dialect.SQLiteDialect 8 | true=1;false=0 9 | true 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Services/IService.cs: -------------------------------------------------------------------------------- 1 | using DataLayer.Repository; 2 | using System; 3 | using System.Collections.Generic; 4 | 5 | namespace Services 6 | { 7 | public interface IService : IDisposable 8 | { 9 | 10 | 11 | IRepository Repository { get; set; } 12 | T Save(T entity); 13 | T SaveOrUpdate(T entity); 14 | void Delete(object id); 15 | void Delete(T entity); 16 | T Get(object id); 17 | bool Exists(object id); 18 | ICollection FindAll(string orderField); 19 | 20 | ICollection FindByProperty(string propertyName, object value); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Models/Mappings/PersonGroupMap.cs: -------------------------------------------------------------------------------- 1 | using FluentNHibernate.Mapping; 2 | using Models.Domain; 3 | 4 | namespace Models.Mappings 5 | { 6 | public class PersonGroupMap : ClassMap 7 | { 8 | public PersonGroupMap() { 9 | Table("PersonGroup"); 10 | 11 | Id(x => x.Id) 12 | .Column("Id") 13 | .Not.Nullable() 14 | .GeneratedBy.Identity(); 15 | 16 | Map(x => x.GroupName); 17 | 18 | HasMany(x => x.People) 19 | .KeyColumn("GroupFk") 20 | .Cascade.SaveUpdate(); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /DataLayer.Tests/hibernate.cfg.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | NHibernate.Connection.DriverConnectionProvider 6 | NHibernate.Dialect.MsSql2005Dialect 7 | NHibernate.Driver.SqlClientDriver 8 | Server=(local);Database=Test;Integrated Security=SSPI; 9 | true 10 | 11 | 12 | -------------------------------------------------------------------------------- /DataLayer/UnitOfWorks/GenericTransaction.cs: -------------------------------------------------------------------------------- 1 | using NHibernate; 2 | 3 | namespace DataLayer.UnitOfWorks 4 | { 5 | public class GenericTransaction : IGenericTransaction 6 | { 7 | private readonly ITransaction _transaction; 8 | 9 | public GenericTransaction(ITransaction transaction) 10 | { 11 | _transaction = transaction; 12 | } 13 | 14 | public void Commit() 15 | { 16 | _transaction.Commit(); 17 | } 18 | 19 | public void Rollback() 20 | { 21 | _transaction.Rollback(); 22 | } 23 | 24 | public void Dispose() 25 | { 26 | _transaction.Dispose(); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Models/Domain/Transaction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Models.Domain 5 | { 6 | public class Transaction 7 | { 8 | public virtual int Id { get; set; } 9 | public virtual int Serial { get; set; } 10 | public virtual DateTime CreateDate { get; set; } 11 | public virtual DateTime LastModifyDate { get; set; } 12 | public virtual DateTime PaperDate { get; set; } 13 | public virtual string Note { get; set; } 14 | public virtual IList Entries { get; set; } 15 | 16 | public virtual string CreatedBy { get; set; } 17 | public virtual string ModifiedBy { get; set; } 18 | 19 | public virtual string RefNumber { get; set; } 20 | public virtual TransactionType TransactionType { get; set; } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Models/Domain/Entry.cs: -------------------------------------------------------------------------------- 1 | using Models.Common; 2 | using System; 3 | 4 | namespace Models.Domain 5 | { 6 | public class Entry 7 | { 8 | public virtual int Id { get; set; } 9 | public virtual DateTime CreateDate { get; set; } 10 | public virtual DateTime LastModifyDate { get; set; } 11 | public virtual string CreatedBy { get; set; } 12 | public virtual string ModifiedBy { get; set; } 13 | 14 | public virtual string Description { get; set; } 15 | 16 | public virtual decimal Debit { get; set; } 17 | public virtual decimal Credit { get; set; } 18 | 19 | public virtual Account Account { get; set; } 20 | public virtual Party Party { get; set; } 21 | public virtual Transaction Transaction { get; set; } 22 | 23 | public virtual Currency Currency { get; set; } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Models/Domain/Person.cs: -------------------------------------------------------------------------------- 1 | namespace Models.Domain 2 | { 3 | public class Person : Party 4 | { 5 | 6 | public virtual Gender Gender { get; set; } 7 | public virtual PersonType PersonType { get; set; } 8 | public virtual string RegisteredId { get; set; } 9 | public virtual string Address { get; set; } 10 | public virtual string Phone { get; set; } 11 | public virtual string Phone2 { get; set; } 12 | public virtual string Mobile { get; set; } 13 | public virtual string Mobile2 { get; set; } 14 | public virtual string Fax { get; set; } 15 | public virtual string Email { get; set; } 16 | public virtual string Email2 { get; set; } 17 | public virtual bool IsBlocked { get; set; } 18 | public virtual PersonGroup Group { get; set; } 19 | public virtual byte[] Image { get; set; } 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Models/Mappings/PersonMap.cs: -------------------------------------------------------------------------------- 1 | using FluentNHibernate.Mapping; 2 | using Models.Domain; 3 | 4 | namespace Models.Mappings 5 | { 6 | public class PersonMap : SubclassMap 7 | { 8 | public PersonMap() { 9 | Table("People"); 10 | KeyColumn("Id"); 11 | Map(x => x.Address); 12 | Map(x => x.Email); 13 | Map(x => x.Email2); 14 | Map(x => x.Fax); 15 | Map(x => x.Gender); 16 | Map(x => x.Image); 17 | Map(x => x.IsBlocked); 18 | Map(x => x.Mobile); 19 | Map(x => x.Mobile2); 20 | Map(x => x.PersonType); 21 | Map(x => x.Phone); 22 | Map(x => x.Phone2); 23 | Map(x => x.RegisteredId); 24 | 25 | References(x => x.Group) 26 | .Column("GroupFk"); 27 | 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Models/Mappings/TransactionMap.cs: -------------------------------------------------------------------------------- 1 | using FluentNHibernate.Mapping; 2 | using Models.Domain; 3 | 4 | namespace Models.Mappings 5 | { 6 | public class TransactionMap : ClassMap 7 | { 8 | public TransactionMap() { 9 | 10 | Table("Transactions"); 11 | 12 | Id(x => x.Id) 13 | .Column("Id") 14 | .Not.Nullable() 15 | .GeneratedBy.Identity(); 16 | Map(x => x.CreateDate); 17 | Map(x => x.CreatedBy); 18 | Map(x => x.LastModifyDate); 19 | Map(x => x.ModifiedBy); 20 | Map(x => x.Note); 21 | Map(x => x.PaperDate); 22 | Map(x => x.Serial); 23 | Map(x => x.TransactionType); 24 | Map(x => x.RefNumber); 25 | 26 | HasMany(x => x.Entries) 27 | .KeyColumn("TransactionFk") 28 | .Cascade.All(); 29 | 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Models/Mappings/EntryMap.cs: -------------------------------------------------------------------------------- 1 | using FluentNHibernate.Mapping; 2 | using Models.Common; 3 | using Models.Domain; 4 | 5 | namespace Models.Mappings 6 | { 7 | public class EntryMap : ClassMap 8 | { 9 | public EntryMap() 10 | { 11 | Table("Entries"); 12 | Id(x => x.Id) 13 | .Column("Id") 14 | .Not.Nullable() 15 | .GeneratedBy.Identity(); 16 | Map(x => x.Debit); 17 | Map(x => x.Credit); 18 | Map(x => x.CreateDate); 19 | Map(x => x.CreatedBy); 20 | Map(x => x.Description); 21 | Map(x => x.LastModifyDate); 22 | Map(x => x.ModifiedBy); 23 | 24 | References(x => x.Account).Column("AccountFk"); 25 | References(x => x.Transaction).Column("TransactionFk"); 26 | References(x => x.Party).Column("PartyFk"); 27 | References(x => x.Currency).Column("CurrencyFk"); 28 | 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Services/AccountingService.cs: -------------------------------------------------------------------------------- 1 | using DataLayer.Repository; 2 | using Models.Domain; 3 | using DataLayer.UnitOfWorks; 4 | using System; 5 | 6 | namespace Services 7 | { 8 | public class AccountingService 9 | { 10 | private EntityService accountService; 11 | 12 | public Account AddAccount(Account account, bool isRoot=false) 13 | { 14 | using (UnitOfWork.Start()) 15 | { 16 | using (accountService = new EntityService(new Repository())) 17 | { 18 | if (isRoot) 19 | { 20 | account.Parent = null; 21 | } 22 | var olds = accountService.FindByProperty("AccountName", account.AccountName.Trim()); 23 | if (olds != null && olds.Count > 0) 24 | { 25 | throw new Exception("Account {0}- {1} already exists."); 26 | } 27 | else { 28 | return accountService.Save(account); 29 | } 30 | 31 | } 32 | } 33 | 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Entities/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Resources; 2 | using System.Reflection; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.InteropServices; 5 | 6 | // General Information about an assembly is controlled through the following 7 | // set of attributes. Change these attribute values to modify the information 8 | // associated with an assembly. 9 | [assembly: AssemblyTitle("Entities")] 10 | [assembly: AssemblyDescription("")] 11 | [assembly: AssemblyConfiguration("")] 12 | [assembly: AssemblyCompany("")] 13 | [assembly: AssemblyProduct("Entities")] 14 | [assembly: AssemblyCopyright("Copyright © 2016")] 15 | [assembly: AssemblyTrademark("")] 16 | [assembly: AssemblyCulture("")] 17 | [assembly: NeutralResourcesLanguage("en")] 18 | 19 | // Version information for an assembly consists of the following four values: 20 | // 21 | // Major Version 22 | // Minor Version 23 | // Build Number 24 | // Revision 25 | // 26 | // You can specify all the values or you can default the Build and Revision Numbers 27 | // by using the '*' as shown below: 28 | // [assembly: AssemblyVersion("1.0.*")] 29 | [assembly: AssemblyVersion("1.0.0.0")] 30 | [assembly: AssemblyFileVersion("1.0.0.0")] 31 | -------------------------------------------------------------------------------- /Models/Mappings/PartyMap.cs: -------------------------------------------------------------------------------- 1 | using FluentNHibernate.Mapping; 2 | using Models.Domain; 3 | 4 | namespace Models.Mappings 5 | { 6 | public class PartyMap: ClassMap 7 | { 8 | public PartyMap() { 9 | 10 | 11 | Table("Parties"); 12 | 13 | // the Id function is used for identity 14 | // key mapping, it is possible to specify the type 15 | // of the property, its access, the name 16 | // of the field in the table and its server type, 17 | // facets and other mapping settings, 18 | // as well as to specify the class name to be used to 19 | // generate the primary key for a new 20 | // record while saving a new record 21 | Id(x => x.Id) 22 | .Column("Id") 23 | .Not.Nullable() 24 | .GeneratedBy.Identity(); 25 | 26 | Map(q => q.Description); 27 | Map(q => q.PartyCode); 28 | Map(q => q.PartyName); 29 | Map(q => q.TrialBalance); 30 | 31 | HasMany(x => x.Entries) 32 | .KeyColumn("PartyFk") 33 | .LazyLoad() 34 | .Cascade.SaveUpdate(); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Models/Mappings/AccountMap.cs: -------------------------------------------------------------------------------- 1 | using FluentNHibernate.Mapping; 2 | using Models.Domain; 3 | 4 | namespace Models.Mappings 5 | { 6 | public class AccountMap : ClassMap 7 | { 8 | public AccountMap() { 9 | 10 | 11 | // indicates that this class is the base 12 | // one for the TPC inheritance strategy and that 13 | // the values of its properties should 14 | // be united with the values of derived classes 15 | // UseUnionSubclassForInheritanceMapping(); 16 | Table("Accounts"); 17 | 18 | Id(x => x.Id) 19 | .Column("Id") 20 | .Not.Nullable() 21 | .GeneratedBy.Identity(); 22 | 23 | Map(q => q.AccountCode); 24 | Map(q => q.AccountName); 25 | Map(q => q.Description); 26 | Map(q => q.IsActive); 27 | Map(q => q.AccountType); 28 | Map(q => q.TrialBalance); 29 | 30 | HasMany(q => q.Childeren) 31 | .KeyColumn("ParentFk") 32 | .Cascade.All(); 33 | 34 | References(x => x.Parent).Column("ParentFk"); 35 | 36 | HasMany(x => x.Entries) 37 | .KeyColumn("AccountFk") 38 | .LazyLoad() 39 | .Cascade.All(); 40 | 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /DataLayer/UnitOfWorks/With.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | 4 | namespace DataLayer.UnitOfWorks 5 | { 6 | public static partial class With 7 | { 8 | public static void Transaction(IsolationLevel level, Action transactional) 9 | { 10 | using (UnitOfWork.Start()) 11 | { 12 | // If we are already in a transaction, don't start a new one 13 | if (UnitOfWork.Current.IsInActiveTransaction) 14 | { 15 | transactional(); 16 | } 17 | else 18 | { 19 | IGenericTransaction tx = UnitOfWork.Current.BeginTransaction(level); 20 | try 21 | { 22 | transactional(); 23 | tx.Commit(); 24 | } 25 | catch 26 | { 27 | tx.Rollback(); 28 | throw; 29 | } 30 | finally 31 | { 32 | tx.Dispose(); 33 | } 34 | } 35 | } 36 | } 37 | 38 | public static void Transaction(Action transactional) 39 | { 40 | Transaction(IsolationLevel.ReadCommitted, transactional); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Models/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyTitle("Models")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("Models")] 12 | [assembly: AssemblyCopyright("Copyright © 2016")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // Setting ComVisible to false makes the types in this assembly not visible 17 | // to COM components. If you need to access a type in this assembly from 18 | // COM, set the ComVisible attribute to true on that type. 19 | [assembly: ComVisible(false)] 20 | 21 | // The following GUID is for the ID of the typelib if this project is exposed to COM 22 | [assembly: Guid("d5b9c4a6-45ed-4f3d-9845-3be348c6eadd")] 23 | 24 | // Version information for an assembly consists of the following four values: 25 | // 26 | // Major Version 27 | // Minor Version 28 | // Build Number 29 | // Revision 30 | // 31 | // You can specify all the values or you can default the Build and Revision Numbers 32 | // by using the '*' as shown below: 33 | // [assembly: AssemblyVersion("1.0.*")] 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] 36 | -------------------------------------------------------------------------------- /DataLayer/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyTitle("DataLayer")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("DataLayer")] 12 | [assembly: AssemblyCopyright("Copyright © 2016")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // Setting ComVisible to false makes the types in this assembly not visible 17 | // to COM components. If you need to access a type in this assembly from 18 | // COM, set the ComVisible attribute to true on that type. 19 | [assembly: ComVisible(false)] 20 | 21 | // The following GUID is for the ID of the typelib if this project is exposed to COM 22 | [assembly: Guid("912d8b4c-46e4-4322-8b4c-f6968651a0d8")] 23 | 24 | // Version information for an assembly consists of the following four values: 25 | // 26 | // Major Version 27 | // Minor Version 28 | // Build Number 29 | // Revision 30 | // 31 | // You can specify all the values or you can default the Build and Revision Numbers 32 | // by using the '*' as shown below: 33 | // [assembly: AssemblyVersion("1.0.*")] 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] 36 | -------------------------------------------------------------------------------- /Services/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyTitle("Services")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("Services")] 12 | [assembly: AssemblyCopyright("Copyright © 2016")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // Setting ComVisible to false makes the types in this assembly not visible 17 | // to COM components. If you need to access a type in this assembly from 18 | // COM, set the ComVisible attribute to true on that type. 19 | [assembly: ComVisible(false)] 20 | 21 | // The following GUID is for the ID of the typelib if this project is exposed to COM 22 | [assembly: Guid("e2b238c4-4f89-4796-908b-aac68e5d9897")] 23 | 24 | // Version information for an assembly consists of the following four values: 25 | // 26 | // Major Version 27 | // Minor Version 28 | // Build Number 29 | // Revision 30 | // 31 | // You can specify all the values or you can default the Build and Revision Numbers 32 | // by using the '*' as shown below: 33 | // [assembly: AssemblyVersion("1.0.*")] 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] 36 | -------------------------------------------------------------------------------- /DataLayer.Tests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyTitle("DataLayer.Tests")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("DataLayer.Tests")] 12 | [assembly: AssemblyCopyright("Copyright © 2016")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // Setting ComVisible to false makes the types in this assembly not visible 17 | // to COM components. If you need to access a type in this assembly from 18 | // COM, set the ComVisible attribute to true on that type. 19 | [assembly: ComVisible(false)] 20 | 21 | // The following GUID is for the ID of the typelib if this project is exposed to COM 22 | [assembly: Guid("87c92c93-54d8-4940-924b-6b3eeda48b9f")] 23 | 24 | // Version information for an assembly consists of the following four values: 25 | // 26 | // Major Version 27 | // Minor Version 28 | // Build Number 29 | // Revision 30 | // 31 | // You can specify all the values or you can default the Build and Revision Numbers 32 | // by using the '*' as shown below: 33 | // [assembly: AssemblyVersion("1.0.*")] 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] 36 | -------------------------------------------------------------------------------- /Accounting.Console/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyTitle("Accounting.Console")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("Accounting.Console")] 12 | [assembly: AssemblyCopyright("Copyright © 2016")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // Setting ComVisible to false makes the types in this assembly not visible 17 | // to COM components. If you need to access a type in this assembly from 18 | // COM, set the ComVisible attribute to true on that type. 19 | [assembly: ComVisible(false)] 20 | 21 | // The following GUID is for the ID of the typelib if this project is exposed to COM 22 | [assembly: Guid("1292d720-32c9-4090-af03-5606913cef0f")] 23 | 24 | // Version information for an assembly consists of the following four values: 25 | // 26 | // Major Version 27 | // Minor Version 28 | // Build Number 29 | // Revision 30 | // 31 | // You can specify all the values or you can default the Build and Revision Numbers 32 | // by using the '*' as shown below: 33 | // [assembly: AssemblyVersion("1.0.*")] 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] 36 | -------------------------------------------------------------------------------- /DataLayer/Repository/RepositoryHelper.cs: -------------------------------------------------------------------------------- 1 | using NHibernate; 2 | using NHibernate.Criterion; 3 | 4 | namespace DataLayer.Repository 5 | { 6 | public class RepositoryHelper 7 | { 8 | public static ICriteria GetExecutableCriteria(ISession session, DetachedCriteria criteria, Order[] orders) 9 | { 10 | ICriteria executableCriteria; 11 | if (criteria != null) 12 | executableCriteria = criteria.GetExecutableCriteria(session); 13 | else 14 | executableCriteria = session.CreateCriteria(typeof(T)); 15 | 16 | if (orders != null) 17 | { 18 | foreach (Order order in orders) 19 | executableCriteria.AddOrder(order); 20 | } 21 | return executableCriteria; 22 | } 23 | 24 | public static ICriteria CreateCriteriaFromArray(ISession session, ICriterion[] criteria, Order[] orders) 25 | { 26 | ICriteria crit = session.CreateCriteria(typeof(T)); 27 | foreach (ICriterion criterion in criteria) 28 | { 29 | //allow some fancy antics like returning possible return 30 | // or null to ignore the criteria 31 | if (criterion == null) 32 | continue; 33 | crit.Add(criterion); 34 | } 35 | if (orders != null) 36 | { 37 | foreach (Order order in orders) 38 | crit.AddOrder(order); 39 | } 40 | return crit; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Models/Domain/Account.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Models.Domain 4 | { 5 | public class Account 6 | { 7 | 8 | public Account() { 9 | this._childeren = new List(); 10 | } 11 | 12 | public virtual int Id { get; set; } 13 | public virtual int AccountCode { get; set; } 14 | public virtual string AccountName { get; set; } 15 | public virtual string Description { get; set; } 16 | public virtual bool IsActive { get; set; } 17 | public virtual decimal TrialBalance { get; set; } 18 | public virtual AccountType AccountType { get; set; } 19 | 20 | public virtual Account Parent { get; set; } 21 | 22 | public virtual IList Entries { get; set; } 23 | 24 | private IList _childeren; 25 | public virtual IList Childeren { 26 | get { 27 | return this._childeren; 28 | } 29 | set 30 | { 31 | this._childeren = value; 32 | } 33 | } 34 | 35 | 36 | public virtual string ToString(string tab ="") 37 | { 38 | string str = ""; 39 | if (this.Parent == null) 40 | { 41 | str += string.Format("{0} {1}\n", this.AccountCode, this.AccountName); 42 | } 43 | else { 44 | tab += "\t"; 45 | str += string.Format("{0} {1} {2}\n", tab, this.AccountCode, this.AccountName); 46 | } 47 | foreach (var child in this._childeren) 48 | { 49 | str += child.ToString(tab); 50 | } 51 | 52 | return str; 53 | } 54 | 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /DataLayer/UnitOfWorks/UnitOfWork.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using NHibernate; 3 | using NHibernate.Cfg; 4 | 5 | namespace DataLayer.UnitOfWorks 6 | { 7 | public static class UnitOfWork 8 | { 9 | private static readonly IUnitOfWorkFactory _unitOfWorkFactory = new UnitOfWorkFactory(); 10 | 11 | public static Configuration Configuration 12 | { 13 | get { return _unitOfWorkFactory.Configuration; } 14 | } 15 | 16 | public const string CurrentUnitOfWorkKey = "CurrentUnitOfWork.Key"; 17 | 18 | private static IUnitOfWork CurrentUnitOfWork 19 | { 20 | get { return Local.Data[CurrentUnitOfWorkKey] as IUnitOfWork; } 21 | set { Local.Data[CurrentUnitOfWorkKey] = value; } 22 | } 23 | 24 | public static IUnitOfWork Current 25 | { 26 | get 27 | { 28 | var unitOfWork = CurrentUnitOfWork; 29 | if (unitOfWork == null) 30 | throw new InvalidOperationException("You are not in a unit of work"); 31 | return unitOfWork; 32 | } 33 | } 34 | 35 | public static bool IsStarted 36 | { 37 | get { return CurrentUnitOfWork != null; } 38 | } 39 | 40 | public static ISession CurrentSession 41 | { 42 | get { return _unitOfWorkFactory.CurrentSession; } 43 | internal set { _unitOfWorkFactory.CurrentSession = value; } 44 | } 45 | 46 | public static IUnitOfWork Start() 47 | { 48 | if (CurrentUnitOfWork != null) 49 | throw new InvalidOperationException("You cannot start more than one unit of work at the same time."); 50 | 51 | var unitOfWork = _unitOfWorkFactory.Create(); 52 | CurrentUnitOfWork = unitOfWork; 53 | return unitOfWork; 54 | } 55 | 56 | public static void DisposeUnitOfWork(IUnitOfWorkImplementor unitOfWork) 57 | { 58 | CurrentUnitOfWork = null; 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /DataLayer/Repository/IRepository.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using NHibernate.Criterion; 3 | 4 | namespace DataLayer.Repository 5 | { 6 | public interface IRepository 7 | { 8 | T Get(object id); 9 | T Load(object id); 10 | void Delete(T entity); 11 | void DeleteAll(); 12 | void DeleteAll(DetachedCriteria where); 13 | T Save(T entity); 14 | T SaveOrUpdate(T entity); 15 | T SaveOrUpdateCopy(T entity); 16 | void Update(T entity); 17 | long Count(DetachedCriteria criteria); 18 | long Count(); 19 | bool Exists(DetachedCriteria criteria); 20 | bool Exists(); 21 | ICollection FindByProperty(string propertyName, object value); 22 | ICollection FindAll(string orderField); 23 | ICollection FindAll(DetachedCriteria criteria, params Order[] orders); 24 | ICollection FindAll(Order order, params ICriterion[] criteria); 25 | ICollection FindAll(Order[] orders, params ICriterion[] criteria); 26 | T FindFirst(params Order[] orders); 27 | T FindFirst(DetachedCriteria criteria, params Order[] orders); 28 | T FindOne(params ICriterion[] criteria); 29 | T FindOne(DetachedCriteria criteria); 30 | 31 | ProjT ReportOne(ProjectionList projectionList); 32 | ProjT ReportOne(DetachedCriteria criteria, ProjectionList projectionList); 33 | ICollection ReportAll(ProjectionList projectionList); 34 | ICollection ReportAll(bool distinctResults, ProjectionList projectionList); 35 | ICollection ReportAll(ProjectionList projectionList, params Order[] orders); 36 | ICollection ReportAll(bool distinctResults, ProjectionList projectionList, params Order[] orders); 37 | ICollection ReportAll(DetachedCriteria criteria, ProjectionList projectionList, params Order[] orders); 38 | ICollection ReportAll(bool distinctResults, DetachedCriteria criteria, ProjectionList projectionList, params Order[] orders); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Accounting.Console/Program.cs: -------------------------------------------------------------------------------- 1 | using DataLayer.Repository; 2 | using DataLayer.UnitOfWorks; 3 | 4 | using Models.Domain; 5 | using NHibernate.Cfg; 6 | using NHibernate.Tool.hbm2ddl; 7 | using Services; 8 | using System.IO; 9 | 10 | namespace Accounting.Console 11 | { 12 | class Program 13 | { 14 | private static string DbFile = "core.db"; 15 | 16 | static void Main(string[] args) 17 | { 18 | 19 | 20 | using (UnitOfWork.Start()) 21 | { 22 | // BuildSchema(UnitOfWork.Configuration); 23 | 24 | 25 | 26 | var _AccountRepository = new Repository(); 27 | EntityService service = new EntityService(_AccountRepository); 28 | service.Saved += Service_Saved; 29 | 30 | var tableAcc = new Account 31 | { 32 | AccountCode = 2, 33 | IsActive = true, 34 | AccountName = "Sales", 35 | TrialBalance = 0, 36 | Description = "Sales account", 37 | // Parent = parent 38 | }; 39 | 40 | 41 | // service.Save(tableAcc); 42 | 43 | var accounts = service.FindAll("Id"); 44 | foreach (var account in accounts) 45 | { 46 | if (account.Parent == null) 47 | System.Console.WriteLine(account.ToString()); 48 | } 49 | System.Console.ReadKey(); 50 | } 51 | } 52 | 53 | private static void Service_Saved(object sender, EntityEventArgs e) 54 | { 55 | System.Console.WriteLine("{0}- {1} Saved successfully.", e.Entity.AccountCode, e.Entity.AccountName); 56 | } 57 | 58 | private static void BuildSchema(Configuration config) 59 | { 60 | // delete the existing db on each run 61 | if (File.Exists(DbFile)) 62 | File.Delete(DbFile); 63 | 64 | // this NHibernate tool takes a configuration (with mapping info in) 65 | // and exports a database schema from it 66 | new SchemaExport(config) 67 | .Create(true, true); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /DataLayer/UnitOfWorks/Local.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | 4 | namespace DataLayer.UnitOfWorks 5 | { 6 | public static class Local 7 | { 8 | static readonly ILocalData _data = new LocalData(); 9 | 10 | public static ILocalData Data 11 | { 12 | get { return _data; } 13 | } 14 | 15 | private class LocalData : ILocalData 16 | { 17 | [ThreadStatic] 18 | private static Hashtable _localData; 19 | private static readonly object LocalDataHashtableKey = new object(); 20 | 21 | private static Hashtable LocalHashtable 22 | { 23 | get 24 | { 25 | if (!RunningInWeb) 26 | { 27 | if (_localData == null) 28 | _localData = new Hashtable(); 29 | return _localData; 30 | } 31 | else { 32 | var web_hashtable = new Hashtable(); 33 | return web_hashtable; 34 | } 35 | //else 36 | //{ 37 | // var web_hashtable = HttpContext.Current.Items[LocalDataHashtableKey] as Hashtable; 38 | // if (web_hashtable == null) 39 | // { 40 | // web_hashtable = new Hashtable(); 41 | // HttpContext.Current.Items[LocalDataHashtableKey] = web_hashtable; 42 | // } 43 | // return web_hashtable; 44 | //} 45 | } 46 | } 47 | 48 | public object this[object key] 49 | { 50 | get { return LocalHashtable[key]; } 51 | set { LocalHashtable[key] = value; } 52 | } 53 | 54 | public int Count 55 | { 56 | get { return LocalHashtable.Count; } 57 | } 58 | 59 | public void Clear() 60 | { 61 | LocalHashtable.Clear(); 62 | } 63 | 64 | public static bool RunningInWeb 65 | { 66 | // get { return HttpContext.Current != null; } 67 | get { return false; } 68 | } 69 | } 70 | } 71 | } -------------------------------------------------------------------------------- /DataLayer/UnitOfWorks/UnitOfWorkImplementor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data; 3 | using NHibernate; 4 | 5 | namespace DataLayer.UnitOfWorks 6 | { 7 | public class UnitOfWorkImplementor : IUnitOfWorkImplementor 8 | { 9 | private readonly IUnitOfWorkFactory _factory; 10 | private readonly ISession _session; 11 | 12 | public UnitOfWorkImplementor(IUnitOfWorkFactory factory, ISession session) 13 | { 14 | _factory = factory; 15 | _session = session; 16 | } 17 | 18 | public void Dispose() 19 | { 20 | _factory.DisposeUnitOfWork(this); 21 | _session.Dispose(); 22 | } 23 | 24 | public void IncrementUsages() 25 | { 26 | throw new NotImplementedException(); 27 | } 28 | 29 | public void Flush() 30 | { 31 | _session.Flush(); 32 | } 33 | 34 | public bool IsInActiveTransaction 35 | { 36 | get 37 | { 38 | return _session.Transaction.IsActive; 39 | } 40 | } 41 | 42 | public IUnitOfWorkFactory Factory 43 | { 44 | get { return _factory; } 45 | } 46 | 47 | public ISession Session 48 | { 49 | get { return _session; } 50 | } 51 | 52 | public IGenericTransaction BeginTransaction() 53 | { 54 | return new GenericTransaction(_session.BeginTransaction()); 55 | } 56 | 57 | public IGenericTransaction BeginTransaction(IsolationLevel isolationLevel) 58 | { 59 | return new GenericTransaction(_session.BeginTransaction(isolationLevel)); 60 | } 61 | 62 | public void TransactionalFlush() 63 | { 64 | TransactionalFlush(IsolationLevel.ReadCommitted); 65 | } 66 | 67 | public void TransactionalFlush(IsolationLevel isolationLevel) 68 | { 69 | // $$$$$$$$$$$$$$$$ gns: take this, when making thread safe! $$$$$$$$$$$$$$ 70 | //IUoWTransaction tx = UnitOfWork.Current.BeginTransaction(isolationLevel); 71 | 72 | IGenericTransaction tx = BeginTransaction(isolationLevel); 73 | try 74 | { 75 | //forces a flush of the current unit of work 76 | tx.Commit(); 77 | } 78 | catch 79 | { 80 | tx.Rollback(); 81 | throw; 82 | } 83 | finally 84 | { 85 | tx.Dispose(); 86 | } 87 | } 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /Entities/Entities.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 11.0 6 | Debug 7 | AnyCPU 8 | {E4AA544C-6E70-4EDA-8471-83E9D05B7EC5} 9 | Library 10 | Properties 11 | Entities 12 | Entities 13 | en-US 14 | 512 15 | {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 16 | Profile111 17 | v4.5 18 | 19 | 20 | true 21 | full 22 | false 23 | bin\Debug\ 24 | DEBUG;TRACE 25 | prompt 26 | 4 27 | 28 | 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | Always 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 54 | -------------------------------------------------------------------------------- /Services/EntityService.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using DataLayer.Repository; 3 | 4 | namespace Services 5 | { 6 | public class EntityService : IService 7 | { 8 | 9 | private IRepository _repository; 10 | 11 | public delegate void SavedEventHandler(object sender, EntityEventArgs e); 12 | public event SavedEventHandler Saved; 13 | 14 | public delegate void DeleteEventHandler(object sender, EntityEventArgs e); 15 | public event DeleteEventHandler Deleted; 16 | 17 | 18 | protected virtual void OnSaved(EntityEventArgs e) 19 | { 20 | if (Saved != null) 21 | Saved(this, e); 22 | } 23 | 24 | protected virtual void OnDeleted(EntityEventArgs e) 25 | { 26 | if (Deleted != null) 27 | Deleted(this, e); 28 | } 29 | 30 | 31 | public EntityService(IRepository repository) { 32 | this._repository = repository; 33 | } 34 | 35 | 36 | 37 | 38 | public IRepository Repository 39 | { 40 | get 41 | { 42 | return this._repository; 43 | } 44 | 45 | set 46 | { 47 | this._repository = value; 48 | } 49 | } 50 | 51 | public virtual void Delete(T entity) 52 | { 53 | this._repository.Delete(entity); 54 | var arg = new EntityEventArgs(); 55 | arg.Entity = entity; 56 | OnDeleted(arg); 57 | } 58 | 59 | public virtual void Delete(object id) 60 | { 61 | var entity = this.Get(id); 62 | this._repository.Delete(entity); 63 | } 64 | 65 | public virtual bool Exists(object id) 66 | { 67 | return false; 68 | } 69 | 70 | public virtual ICollection FindAll(string orderField) 71 | { 72 | return this._repository.FindAll(orderField); 73 | } 74 | 75 | public virtual T Get(object id) 76 | { 77 | return this.Repository.Get(id); 78 | } 79 | 80 | public virtual T Save(T entity) 81 | { 82 | var result = this._repository.Save(entity); 83 | var arg = new EntityEventArgs(); 84 | arg.Entity = result; 85 | OnSaved(arg); 86 | return result; 87 | } 88 | 89 | public virtual T SaveOrUpdate(T entity) 90 | { 91 | return this._repository.SaveOrUpdate(entity); 92 | } 93 | 94 | public virtual ICollection FindByProperty(string propertyName, object value) 95 | { 96 | return this._repository.FindByProperty(propertyName, value); 97 | } 98 | 99 | public void Dispose() 100 | { 101 | this._repository = null; 102 | } 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /Accounting.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.24720.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataLayer", "DataLayer\DataLayer.csproj", "{912D8B4C-46E4-4322-8B4C-F6968651A0D8}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataLayer.Tests", "DataLayer.Tests\DataLayer.Tests.csproj", "{87C92C93-54D8-4940-924B-6B3EEDA48B9F}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Accounting.Console", "Accounting.Console\Accounting.Console.csproj", "{1292D720-32C9-4090-AF03-5606913CEF0F}" 11 | EndProject 12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Models", "Models\Models.csproj", "{D5B9C4A6-45ED-4F3D-9845-3BE348C6EADD}" 13 | EndProject 14 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Services", "Services\Services.csproj", "{E2B238C4-4F89-4796-908B-AAC68E5D9897}" 15 | EndProject 16 | Global 17 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 18 | Debug|Any CPU = Debug|Any CPU 19 | Release|Any CPU = Release|Any CPU 20 | EndGlobalSection 21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 22 | {912D8B4C-46E4-4322-8B4C-F6968651A0D8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {912D8B4C-46E4-4322-8B4C-F6968651A0D8}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {912D8B4C-46E4-4322-8B4C-F6968651A0D8}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {912D8B4C-46E4-4322-8B4C-F6968651A0D8}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {87C92C93-54D8-4940-924B-6B3EEDA48B9F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {87C92C93-54D8-4940-924B-6B3EEDA48B9F}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {87C92C93-54D8-4940-924B-6B3EEDA48B9F}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {87C92C93-54D8-4940-924B-6B3EEDA48B9F}.Release|Any CPU.Build.0 = Release|Any CPU 30 | {1292D720-32C9-4090-AF03-5606913CEF0F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 31 | {1292D720-32C9-4090-AF03-5606913CEF0F}.Debug|Any CPU.Build.0 = Debug|Any CPU 32 | {1292D720-32C9-4090-AF03-5606913CEF0F}.Release|Any CPU.ActiveCfg = Release|Any CPU 33 | {1292D720-32C9-4090-AF03-5606913CEF0F}.Release|Any CPU.Build.0 = Release|Any CPU 34 | {D5B9C4A6-45ED-4F3D-9845-3BE348C6EADD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 35 | {D5B9C4A6-45ED-4F3D-9845-3BE348C6EADD}.Debug|Any CPU.Build.0 = Debug|Any CPU 36 | {D5B9C4A6-45ED-4F3D-9845-3BE348C6EADD}.Release|Any CPU.ActiveCfg = Release|Any CPU 37 | {D5B9C4A6-45ED-4F3D-9845-3BE348C6EADD}.Release|Any CPU.Build.0 = Release|Any CPU 38 | {E2B238C4-4F89-4796-908B-AAC68E5D9897}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 39 | {E2B238C4-4F89-4796-908B-AAC68E5D9897}.Debug|Any CPU.Build.0 = Debug|Any CPU 40 | {E2B238C4-4F89-4796-908B-AAC68E5D9897}.Release|Any CPU.ActiveCfg = Release|Any CPU 41 | {E2B238C4-4F89-4796-908B-AAC68E5D9897}.Release|Any CPU.Build.0 = Release|Any CPU 42 | EndGlobalSection 43 | GlobalSection(SolutionProperties) = preSolution 44 | HideSolutionNode = FALSE 45 | EndGlobalSection 46 | EndGlobal 47 | -------------------------------------------------------------------------------- /DataLayer/UnitOfWorks/UnitOfWorkFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using NHibernate; 3 | using NHibernate.Cfg; 4 | using FluentNHibernate.Cfg; 5 | using FluentNHibernate.Cfg.Db; 6 | using NHibernate.Tool.hbm2ddl; 7 | 8 | namespace DataLayer.UnitOfWorks 9 | { 10 | public class UnitOfWorkFactory : IUnitOfWorkFactory 11 | { 12 | private const string Default_HibernateConfig = "hibernate.cfg.xml"; 13 | 14 | private static ISession _currentSession; 15 | private ISessionFactory _sessionFactory; 16 | private Configuration _configuration; 17 | 18 | internal UnitOfWorkFactory() 19 | { } 20 | 21 | public IUnitOfWork Create() 22 | { 23 | ISession session = CreateSession(); 24 | session.FlushMode = FlushMode.Commit; 25 | _currentSession = session; 26 | return new UnitOfWorkImplementor(this, session); 27 | } 28 | 29 | public Configuration Configuration 30 | { 31 | get 32 | { 33 | if (_configuration == null) 34 | { 35 | 36 | _configuration = Fluently.Configure() 37 | .Database( 38 | SQLiteConfiguration.Standard 39 | .UsingFile("core.db") 40 | 41 | // .ShowSql() 42 | 43 | ) 44 | 45 | 46 | .Mappings(m => 47 | m.FluentMappings.AddFromAssemblyOf() 48 | 49 | ) 50 | .ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(false, true)) 51 | .BuildConfiguration(); 52 | //_configuration = new Configuration(); 53 | //string hibernateConfig = Default_HibernateConfig; 54 | ////if not rooted, assume path from base directory 55 | //if (Path.IsPathRooted(hibernateConfig) == false) 56 | // hibernateConfig = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, hibernateConfig); 57 | //if (File.Exists(hibernateConfig)) 58 | // _configuration.Configure(new XmlTextReader(hibernateConfig)); 59 | } 60 | return _configuration; 61 | } 62 | } 63 | 64 | public ISessionFactory SessionFactory 65 | { 66 | get 67 | { 68 | if (_sessionFactory == null) 69 | _sessionFactory = Configuration.BuildSessionFactory(); 70 | return _sessionFactory; 71 | } 72 | } 73 | 74 | public ISession CurrentSession 75 | { 76 | get 77 | { 78 | if (_currentSession == null) 79 | throw new InvalidOperationException("You are not in a unit of work."); 80 | return _currentSession; 81 | } 82 | set { _currentSession = value; } 83 | } 84 | 85 | public void DisposeUnitOfWork(UnitOfWorkImplementor adapter) 86 | { 87 | CurrentSession = null; 88 | UnitOfWork.DisposeUnitOfWork(adapter); 89 | } 90 | 91 | private ISession CreateSession() 92 | { 93 | return SessionFactory.OpenSession(); 94 | } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /Services/Services.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {E2B238C4-4F89-4796-908B-AAC68E5D9897} 8 | Library 9 | Properties 10 | Services 11 | Services 12 | v4.5.2 13 | 512 14 | 15 | 16 | true 17 | full 18 | false 19 | bin\Debug\ 20 | DEBUG;TRACE 21 | prompt 22 | 4 23 | 24 | 25 | pdbonly 26 | true 27 | bin\Release\ 28 | TRACE 29 | prompt 30 | 4 31 | 32 | 33 | 34 | ..\packages\Iesi.Collections.4.0.0.4000\lib\net40\Iesi.Collections.dll 35 | True 36 | 37 | 38 | ..\packages\NHibernate.4.0.4.4000\lib\net40\NHibernate.dll 39 | True 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | {912d8b4c-46e4-4322-8b4c-f6968651a0d8} 60 | DataLayer 61 | 62 | 63 | {d5b9c4a6-45ed-4f3d-9845-3be348c6eadd} 64 | Models 65 | 66 | 67 | 68 | 69 | 70 | 71 | 78 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | build/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | 28 | # MSTest test Results 29 | [Tt]est[Rr]esult*/ 30 | [Bb]uild[Ll]og.* 31 | 32 | # NUNIT 33 | *.VisualState.xml 34 | TestResult.xml 35 | 36 | # Build Results of an ATL Project 37 | [Dd]ebugPS/ 38 | [Rr]eleasePS/ 39 | dlldata.c 40 | 41 | # DNX 42 | project.lock.json 43 | artifacts/ 44 | 45 | *_i.c 46 | *_p.c 47 | *_i.h 48 | *.ilk 49 | *.meta 50 | *.obj 51 | *.pch 52 | *.pdb 53 | *.pgc 54 | *.pgd 55 | *.rsp 56 | *.sbr 57 | *.tlb 58 | *.tli 59 | *.tlh 60 | *.tmp 61 | *.tmp_proj 62 | *.log 63 | *.vspscc 64 | *.vssscc 65 | .builds 66 | *.pidb 67 | *.svclog 68 | *.scc 69 | 70 | # Chutzpah Test files 71 | _Chutzpah* 72 | 73 | # Visual C++ cache files 74 | ipch/ 75 | *.aps 76 | *.ncb 77 | *.opensdf 78 | *.sdf 79 | *.cachefile 80 | 81 | # Visual Studio profiler 82 | *.psess 83 | *.vsp 84 | *.vspx 85 | 86 | # TFS 2012 Local Workspace 87 | $tf/ 88 | 89 | # Guidance Automation Toolkit 90 | *.gpState 91 | 92 | # ReSharper is a .NET coding add-in 93 | _ReSharper*/ 94 | *.[Rr]e[Ss]harper 95 | *.DotSettings.user 96 | 97 | # JustCode is a .NET coding add-in 98 | .JustCode 99 | 100 | # TeamCity is a build add-in 101 | _TeamCity* 102 | 103 | # DotCover is a Code Coverage Tool 104 | *.dotCover 105 | 106 | # NCrunch 107 | _NCrunch_* 108 | .*crunch*.local.xml 109 | 110 | # MightyMoose 111 | *.mm.* 112 | AutoTest.Net/ 113 | 114 | # Web workbench (sass) 115 | .sass-cache/ 116 | 117 | # Installshield output folder 118 | [Ee]xpress/ 119 | 120 | # DocProject is a documentation generator add-in 121 | DocProject/buildhelp/ 122 | DocProject/Help/*.HxT 123 | DocProject/Help/*.HxC 124 | DocProject/Help/*.hhc 125 | DocProject/Help/*.hhk 126 | DocProject/Help/*.hhp 127 | DocProject/Help/Html2 128 | DocProject/Help/html 129 | 130 | # Click-Once directory 131 | publish/ 132 | 133 | # Publish Web Output 134 | *.[Pp]ublish.xml 135 | *.azurePubxml 136 | ## TODO: Comment the next line if you want to checkin your 137 | ## web deploy settings but do note that will include unencrypted 138 | ## passwords 139 | #*.pubxml 140 | 141 | *.publishproj 142 | 143 | # NuGet Packages 144 | *.nupkg 145 | # The packages folder can be ignored because of Package Restore 146 | **/packages/* 147 | # except build/, which is used as an MSBuild target. 148 | !**/packages/build/ 149 | # Uncomment if necessary however generally it will be regenerated when needed 150 | #!**/packages/repositories.config 151 | 152 | # Windows Azure Build Output 153 | csx/ 154 | *.build.csdef 155 | 156 | # Windows Store app package directory 157 | AppPackages/ 158 | 159 | # Visual Studio cache files 160 | # files ending in .cache can be ignored 161 | *.[Cc]ache 162 | # but keep track of directories ending in .cache 163 | !*.[Cc]ache/ 164 | 165 | # Others 166 | ClientBin/ 167 | [Ss]tyle[Cc]op.* 168 | ~$* 169 | *~ 170 | *.dbmdl 171 | *.dbproj.schemaview 172 | *.pfx 173 | *.publishsettings 174 | node_modules/ 175 | orleans.codegen.cs 176 | 177 | # RIA/Silverlight projects 178 | Generated_Code/ 179 | 180 | # Backup & report files from converting an old project file 181 | # to a newer Visual Studio version. Backup files are not needed, 182 | # because we have git ;-) 183 | _UpgradeReport_Files/ 184 | Backup*/ 185 | UpgradeLog*.XML 186 | UpgradeLog*.htm 187 | 188 | # SQL Server files 189 | *.mdf 190 | *.ldf 191 | 192 | # Business Intelligence projects 193 | *.rdl.data 194 | *.bim.layout 195 | *.bim_*.settings 196 | 197 | # Microsoft Fakes 198 | FakesAssemblies/ 199 | 200 | # Node.js Tools for Visual Studio 201 | .ntvs_analysis.dat 202 | 203 | # Visual Studio 6 build log 204 | *.plg 205 | 206 | # Visual Studio 6 workspace options file 207 | *.opt 208 | 209 | # LightSwitch generated files 210 | GeneratedArtifacts/ 211 | _Pvt_Extensions/ 212 | ModelManifest.xml 213 | -------------------------------------------------------------------------------- /DataLayer/DataLayer.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {912D8B4C-46E4-4322-8B4C-F6968651A0D8} 8 | Library 9 | Properties 10 | DataLayer 11 | DataLayer 12 | v4.5.2 13 | 512 14 | 15 | 16 | true 17 | full 18 | false 19 | bin\Debug\ 20 | DEBUG;TRACE 21 | prompt 22 | 4 23 | 24 | 25 | pdbonly 26 | true 27 | bin\Release\ 28 | TRACE 29 | prompt 30 | 4 31 | 32 | 33 | 34 | ..\packages\FluentNHibernate.2.0.3.0\lib\net40\FluentNHibernate.dll 35 | True 36 | 37 | 38 | ..\packages\Iesi.Collections.4.0.0.4000\lib\net40\Iesi.Collections.dll 39 | True 40 | 41 | 42 | ..\packages\NHibernate.4.0.4.4000\lib\net40\NHibernate.dll 43 | True 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | Always 78 | 79 | 80 | 81 | 82 | {d5b9c4a6-45ed-4f3d-9845-3be348c6eadd} 83 | Models 84 | 85 | 86 | 87 | 94 | -------------------------------------------------------------------------------- /Models/Models.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {D5B9C4A6-45ED-4F3D-9845-3BE348C6EADD} 8 | Library 9 | Properties 10 | Models 11 | Models 12 | v4.5.2 13 | 512 14 | 15 | 16 | true 17 | full 18 | false 19 | bin\Debug\ 20 | DEBUG;TRACE 21 | prompt 22 | 4 23 | 24 | 25 | pdbonly 26 | true 27 | bin\Release\ 28 | TRACE 29 | prompt 30 | 4 31 | 32 | 33 | 34 | ..\packages\FluentNHibernate.2.0.3.0\lib\net40\FluentNHibernate.dll 35 | True 36 | 37 | 38 | ..\packages\Iesi.Collections.4.0.0.4000\lib\net40\Iesi.Collections.dll 39 | True 40 | 41 | 42 | ..\packages\NHibernate.4.0.0.4000\lib\net40\NHibernate.dll 43 | True 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 96 | -------------------------------------------------------------------------------- /Accounting.Console/Accounting.Console.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {1292D720-32C9-4090-AF03-5606913CEF0F} 8 | Exe 9 | Properties 10 | Accounting.Console 11 | Accounting.Console 12 | v4.5.2 13 | 512 14 | true 15 | 16 | 17 | 18 | 19 | AnyCPU 20 | true 21 | full 22 | false 23 | bin\Debug\ 24 | DEBUG;TRACE 25 | prompt 26 | 4 27 | 28 | 29 | AnyCPU 30 | pdbonly 31 | true 32 | bin\Release\ 33 | TRACE 34 | prompt 35 | 4 36 | 37 | 38 | 39 | ..\packages\Iesi.Collections.4.0.0.4000\lib\net40\Iesi.Collections.dll 40 | True 41 | 42 | 43 | ..\packages\NHibernate.4.0.4.4000\lib\net40\NHibernate.dll 44 | True 45 | 46 | 47 | 48 | 49 | ..\packages\System.Data.SQLite.Core.1.0.99.0\lib\net451\System.Data.SQLite.dll 50 | True 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | {912d8b4c-46e4-4322-8b4c-f6968651a0d8} 70 | DataLayer 71 | 72 | 73 | {d5b9c4a6-45ed-4f3d-9845-3be348c6eadd} 74 | Models 75 | 76 | 77 | {e2b238c4-4f89-4796-908b-aac68e5d9897} 78 | Services 79 | 80 | 81 | 82 | 83 | 84 | 85 | This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. 86 | 87 | 88 | 89 | 96 | -------------------------------------------------------------------------------- /DataLayer.Tests/DataLayer.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Debug 5 | AnyCPU 6 | {87C92C93-54D8-4940-924B-6B3EEDA48B9F} 7 | Library 8 | Properties 9 | DataLayer.Tests 10 | DataLayer.Tests 11 | v4.5.2 12 | 512 13 | {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 14 | 10.0 15 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) 16 | $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages 17 | False 18 | UnitTest 19 | 20 | 21 | true 22 | full 23 | false 24 | bin\Debug\ 25 | DEBUG;TRACE 26 | prompt 27 | 4 28 | 29 | 30 | pdbonly 31 | true 32 | bin\Release\ 33 | TRACE 34 | prompt 35 | 4 36 | 37 | 38 | 39 | ..\packages\Iesi.Collections.4.0.0.4000\lib\net40\Iesi.Collections.dll 40 | True 41 | 42 | 43 | ..\packages\NHibernate.4.0.4.4000\lib\net40\NHibernate.dll 44 | True 45 | 46 | 47 | ..\packages\RhinoMocks.3.6.1\lib\net\Rhino.Mocks.dll 48 | True 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | False 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | {912d8b4c-46e4-4322-8b4c-f6968651a0d8} 72 | DataLayer 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | PreserveNewest 81 | 82 | 83 | 84 | 85 | 86 | 87 | False 88 | 89 | 90 | False 91 | 92 | 93 | False 94 | 95 | 96 | False 97 | 98 | 99 | 100 | 101 | 102 | 103 | 110 | -------------------------------------------------------------------------------- /DataLayer/Repository/Repository.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using NHibernate; 5 | using NHibernate.Criterion; 6 | using NHibernate.Transform; 7 | using DataLayer.UnitOfWorks; 8 | 9 | namespace DataLayer.Repository 10 | { 11 | public class Repository : IRepository 12 | { 13 | protected virtual ISession Session 14 | { 15 | get { return UnitOfWork.CurrentSession; } 16 | } 17 | 18 | protected virtual ISessionFactory SessionFactory 19 | { 20 | get { return UnitOfWork.CurrentSession.GetSessionImplementation().Factory; } 21 | } 22 | 23 | public T Get(object id) 24 | { 25 | return (T)Session.Get(typeof(T), id); 26 | } 27 | 28 | public T Load(object id) 29 | { 30 | return (T)Session.Load(typeof(T), id); 31 | } 32 | 33 | public void Delete(T entity) 34 | { 35 | Session.Delete(entity); 36 | } 37 | 38 | public void DeleteAll() 39 | { 40 | Session.Delete(string.Format("from {0}", typeof(T).Name)); 41 | } 42 | 43 | public void DeleteAll(DetachedCriteria where) 44 | { 45 | foreach (object entity in where.GetExecutableCriteria(Session).List()) 46 | { 47 | Session.Delete(entity); 48 | } 49 | } 50 | 51 | public T Save(T entity) 52 | { 53 | Session.Save(entity); 54 | return entity; 55 | } 56 | 57 | public T SaveOrUpdate(T entity) 58 | { 59 | Session.SaveOrUpdate(entity); 60 | return entity; 61 | } 62 | 63 | public T SaveOrUpdateCopy(T entity) 64 | { 65 | //Implementation changed. 66 | Session.SaveOrUpdate(entity); 67 | return entity; 68 | } 69 | 70 | public void Update(T entity) 71 | { 72 | Session.Update(entity); 73 | } 74 | 75 | public long Count(DetachedCriteria criteria) 76 | { 77 | ICriteria crit = RepositoryHelper.GetExecutableCriteria(Session, criteria, null); 78 | crit.SetProjection(Projections.RowCount()); 79 | object countMayBe_Int32_Or_Int64_DependingOnDatabase = crit.UniqueResult(); 80 | return Convert.ToInt64(countMayBe_Int32_Or_Int64_DependingOnDatabase); 81 | } 82 | 83 | public long Count() 84 | { 85 | return Count(null); 86 | } 87 | 88 | public bool Exists(DetachedCriteria criteria) 89 | { 90 | return 0 != Count(criteria); 91 | } 92 | 93 | public bool Exists() 94 | { 95 | return Exists(null); 96 | } 97 | 98 | public ICollection FindAll(DetachedCriteria criteria, params Order[] orders) 99 | { 100 | ICriteria crit = RepositoryHelper.GetExecutableCriteria(Session, criteria, orders); 101 | return crit.List(); 102 | } 103 | 104 | public ICollection FindAll(Order order, params ICriterion[] criteria) 105 | { 106 | return FindAll(new[] { order }, criteria); 107 | } 108 | 109 | public ICollection FindAll(Order[] orders, params ICriterion[] criteria) 110 | { 111 | ICriteria crit = RepositoryHelper.CreateCriteriaFromArray(Session, criteria, orders); 112 | return crit.List(); 113 | } 114 | 115 | public T FindFirst(params Order[] orders) 116 | { 117 | return FindFirst(null, orders); 118 | } 119 | 120 | public T FindFirst(DetachedCriteria criteria, params Order[] orders) 121 | { 122 | ICriteria crit = RepositoryHelper.GetExecutableCriteria(Session, criteria, orders); 123 | crit.SetFirstResult(0); 124 | crit.SetMaxResults(1); 125 | return crit.UniqueResult(); 126 | } 127 | 128 | public T FindOne(params ICriterion[] criteria) 129 | { 130 | ICriteria crit = RepositoryHelper.CreateCriteriaFromArray(Session, criteria, null); 131 | return crit.UniqueResult(); 132 | } 133 | 134 | public T FindOne(DetachedCriteria criteria) 135 | { 136 | ICriteria crit = RepositoryHelper.GetExecutableCriteria(Session, criteria, null); 137 | return crit.UniqueResult(); 138 | } 139 | 140 | public ProjT ReportOne(ProjectionList projectionList) 141 | { 142 | ICriteria crit = RepositoryHelper.GetExecutableCriteria(Session, null, null); 143 | return DoReportOne(crit, projectionList); 144 | } 145 | 146 | public ProjT ReportOne(DetachedCriteria criteria, ProjectionList projectionList) 147 | { 148 | ICriteria crit = RepositoryHelper.GetExecutableCriteria(Session, criteria, null); 149 | return DoReportOne(crit, projectionList); 150 | } 151 | 152 | public ICollection ReportAll(ProjectionList projectionList) 153 | { 154 | return ReportAll(false, projectionList); 155 | } 156 | 157 | public ICollection ReportAll(bool distinctResults, ProjectionList projectionList) 158 | { 159 | ICriteria crit = RepositoryHelper.GetExecutableCriteria(Session, null, null); 160 | return DoReportAll(crit, projectionList, distinctResults); 161 | } 162 | 163 | public ICollection ReportAll(ProjectionList projectionList, params Order[] orders) 164 | { 165 | return ReportAll(false, projectionList, orders); 166 | } 167 | 168 | public ICollection ReportAll(bool distinctResults, ProjectionList projectionList, params Order[] orders) 169 | { 170 | ICriteria crit = RepositoryHelper.GetExecutableCriteria(Session, null, orders); 171 | return DoReportAll(crit, projectionList, distinctResults); 172 | } 173 | 174 | public ICollection ReportAll(DetachedCriteria criteria, ProjectionList projectionList, params Order[] orders) 175 | { 176 | return ReportAll(false, criteria, projectionList, orders); 177 | } 178 | 179 | public ICollection ReportAll(bool distinctResults, DetachedCriteria criteria, ProjectionList projectionList, params Order[] orders) 180 | { 181 | ICriteria crit = RepositoryHelper.GetExecutableCriteria(Session, criteria, orders); 182 | return DoReportAll(crit, projectionList, distinctResults); 183 | } 184 | 185 | private static ProjT DoReportOne(ICriteria criteria, ProjectionList projectionList) 186 | { 187 | BuildProjectionCriteria(criteria, projectionList, false); 188 | return criteria.UniqueResult(); 189 | } 190 | 191 | private static ICollection DoReportAll(ICriteria criteria, ProjectionList projectionList, bool distinctResults) 192 | { 193 | BuildProjectionCriteria(criteria, projectionList, distinctResults); 194 | return criteria.List(); 195 | } 196 | 197 | private static void BuildProjectionCriteria(ICriteria criteria, IProjection projectionList, bool distinctResults) 198 | { 199 | if (distinctResults) 200 | criteria.SetProjection(Projections.Distinct(projectionList)); 201 | else 202 | criteria.SetProjection(projectionList); 203 | 204 | // if we are not returning a tuple, then we need the result transformer 205 | if (typeof(ProjT) != typeof(object[])) 206 | criteria.SetResultTransformer(new TypedResultTransformer()); 207 | } 208 | 209 | public ICollection FindAll(string orderField ) 210 | { 211 | return this.FindAll(null, Order.Asc(orderField)); 212 | } 213 | 214 | public ICollection FindByProperty(string propertyName, object value) 215 | { 216 | return this.FindAll(DetachedCriteria.For().Add(Expression.Eq(propertyName, value))); 217 | } 218 | 219 | /// This is used to convert the resulting tuples into strongly typed objects. 220 | private class TypedResultTransformer : IResultTransformer 221 | { 222 | public object TransformTuple(object[] tuple, string[] aliases) 223 | { 224 | if (tuple.Length == 1) 225 | return tuple[0]; 226 | else 227 | return Activator.CreateInstance(typeof(T1), tuple); 228 | } 229 | 230 | IList IResultTransformer.TransformList(IList collection) 231 | { 232 | return collection; 233 | } 234 | } 235 | } 236 | } 237 | --------------------------------------------------------------------------------