Skip to content

Commit

Permalink
Bug with Session of NHibernate
Browse files Browse the repository at this point in the history
by [[email protected]] in [2011-07-13 16:28:54 (-03:00)] - hg hash {48fa9391d6b2}
  • Loading branch information
darakeon committed Sep 3, 2017
0 parents commit 244609b
Show file tree
Hide file tree
Showing 107 changed files with 90,841 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
syntax: glob
*ReSharper*
bin
obj
*.suo
*.user
.hg*
95 changes: 95 additions & 0 deletions DFM.Core/DFM.Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{AEA621F8-5F62-4E7B-BCE8-EB629E8C4FC7}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>DFM.Core</RootNamespace>
<AssemblyName>DFM.Core</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Ak.Generic, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Library\Ak\Ak.Generic.dll</HintPath>
</Reference>
<Reference Include="FluentNHibernate">
<HintPath>..\Library\NHibernate\FluentNHibernate.dll</HintPath>
</Reference>
<Reference Include="NHibernate">
<HintPath>..\Library\NHibernate\NHibernate.dll</HintPath>
</Reference>
<Reference Include="NHibernate.ByteCode.Castle">
<HintPath>..\Library\NHibernate\NHibernate.ByteCode.Castle.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Database\AccountData.cs" />
<Compile Include="Database\TransferData.cs" />
<Compile Include="Database\CategoryData.cs" />
<Compile Include="Database\MoveData.cs" />
<Compile Include="Database\UserData.cs" />
<Compile Include="Database\BaseData.cs" />
<Compile Include="Entities\Account.cs" />
<Compile Include="Entities\Detail.cs" />
<Compile Include="Entities\Category.cs" />
<Compile Include="Entities\IEntity.cs" />
<Compile Include="Entities\Move.cs" />
<Compile Include="Entities\Transfer.cs" />
<Compile Include="Enums\AccountNature.cs" />
<Compile Include="Enums\MoveNature.cs" />
<Compile Include="Entities\User.cs" />
<Compile Include="Helpers\CoreValidationException.cs" />
<Compile Include="Mappings\AccountMap.cs" />
<Compile Include="Mappings\TransferMap.cs" />
<Compile Include="Mappings\UserMap.cs" />
<Compile Include="Mappings\MoveMap.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Database\NHManager.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\00 - Lib\Ak\Ak.DataAccess\Ak.DataAccess.csproj">
<Project>{DFC0990E-F17C-414B-BB07-10DC86835EC1}</Project>
<Name>Ak.DataAccess</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
89 changes: 89 additions & 0 deletions DFM.Core/Database/AccountData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.Linq;
using DFM.Core.Entities;
using DFM.Core.Helpers;

namespace DFM.Core.Database
{
public class AccountData : BaseData<Account>
{
public Account SaveOrUpdate(Account account, User user)
{
Validate(account, user);

account.BeginDate = DateTime.Now;
account.User = user;

return base.SaveOrUpdate(account);
}

public void Validate(Account account, User user)
{
if (SelectByName(account.Name, user) != null)
{
throw new CoreValidationException("Already Exists.");
}
}

public override Account SaveOrUpdate(Account obj)
{
throw new CoreValidationException("Use the option with parameter User");
}



public Account SelectByName(string name, User user)
{
IList<Account> userList = Session
.CreateCriteria(typeof(Account))
.List<Account>()
.Where(a => a.Name == name)
.ToList();

if (userList.Count > 1)
throw new CoreValidationException("There is more than one account.");

return userList.SingleOrDefault();
}


public IList<Move> GetMonthReport(Int32 id, Int32 month, Int32 year)
{
var account = SelectById(id);

return account.MoveList
.Where(m => m.Date.Month == month
&& m.Date.Year == year)
.ToList();
}


public IDictionary<String, Double> GetYearReport(int id, int? year)
{
var moveSumList = new Dictionary<String, Double>();


var account = SelectById(id);

var moveList = account.MoveList
.Where(m => m.Date.Year == year)
.Select(m => m)
.ToList();


foreach (var move in moveList)
{
var sum = move.DetailList.Sum(d => d.Value);

if (moveSumList.ContainsKey(move.Month))
moveSumList[move.Month] += sum;
else
moveSumList.Add(move.Month, sum);
}


return moveSumList;
}
}
}
48 changes: 48 additions & 0 deletions DFM.Core/Database/BaseData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using DFM.Core.Entities;
using NHibernate;

namespace DFM.Core.Database
{
public abstract class BaseData<T>
where T : IEntity
{
protected ISession Session
{
get
{
return NHManager.Session;
}
}

public virtual T SaveOrUpdate(T obj)
{
if (obj.ID == 0)
Session.SaveOrUpdate(obj);
else
Session.Merge(obj);

return obj;
}

public virtual void Delete(T obj)
{
Session.Delete(obj);
}

public virtual T SelectById(int id)
{
return Session.Get<T>(id);
}

public virtual IList<T> Select()
{
return Session
.CreateCriteria(typeof(T))
.List<T>();
}
}
}
8 changes: 8 additions & 0 deletions DFM.Core/Database/CategoryData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using DFM.Core.Entities;

namespace DFM.Core.Database
{
public class CategoryData : BaseData<Category>
{
}
}
47 changes: 47 additions & 0 deletions DFM.Core/Database/MoveData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Linq;
using DFM.Core.Entities;
using DFM.Core.Enums;
using System;
using DFM.Core.Helpers;

namespace DFM.Core.Database
{
public class MoveData : BaseData<Move>
{
public Move SaveOrUpdate(Move move, Account accountToTransfer = null)
{
Validate(move);

MakeTransfer(move, accountToTransfer);

return base.SaveOrUpdate(move);
}

public void MakeTransfer(Move move, Account accountToTransfer)
{
if (move.Nature == MoveNature.Transfer)
{
if (accountToTransfer == null)
throw new CoreValidationException("Another Account is required to create a Transfer Move.");

move.Transfer = new Transfer(move, accountToTransfer);
}
}

public void Validate(Move move)
{
if (!move.DetailList.Any())
throw new CoreValidationException("At least one value required.");


foreach (var detail in move.DetailList)
{
if (detail.Value < 0)
detail.Value = -detail.Value;

if (detail.Move == null)
detail.Move = move;
}
}
}
}
41 changes: 41 additions & 0 deletions DFM.Core/Database/NHManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Ak.DataAccess.NHibernate;
using DFM.Core.Entities;
using DFM.Core.Mappings;
using NHibernate;

namespace DFM.Core.Database
{
public class NHManager
{
public static ISession Session { get; private set; }

public static void Initialize()
{
var mapInfo = new AutoMappingInfo<UserMap, User>();

SessionBuilder.Initialize(mapInfo);
}

public static void Open()
{
SessionBuilder.Open();

Session = SessionBuilder.Session;
}

public static void Close()
{
SessionBuilder.Close();
}

public static void NhInitialize(object obj)
{
SessionBuilder.NhInitialize(obj);
}

public static void End()
{
SessionBuilder.End();
}
}
}
27 changes: 27 additions & 0 deletions DFM.Core/Database/TransferData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using DFM.Core.Entities;
using DFM.Core.Enums;

namespace DFM.Core.Database
{
public class TransferData : BaseData<Transfer>
{
public override Transfer SaveOrUpdate(Transfer transfer)
{
Validate(transfer);

return base.SaveOrUpdate(transfer);
}

public void Validate(Transfer transfer)
{
var movesIsRight = transfer.In.Nature == MoveNature.In
&& transfer.Out.Nature == MoveNature.Out;

if (!movesIsRight)
{
throw new ApplicationException("Moves are with wrong Nature.");
}
}
}
}
Loading

0 comments on commit 244609b

Please sign in to comment.