Skip to content

Commit

Permalink
Added BeginTransaction, Commit, and Rollback methods to Repository cl…
Browse files Browse the repository at this point in the history
…ass and IRepository interface; Added calls to new StoreRepository methods in Test and Seed actions of HomeController; Changed AddProductsToStore and AddEmployeesToStore methods from public static to private; Improved indentation on Index view; Updated CreateSessionFactory method of NHibernateSessionPerRequest to use strongly typed CurrentSessionContext extension method; Updated tests
  • Loading branch information
davidkennedy85 committed Nov 28, 2012
1 parent fa065c9 commit d8e98d8
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 17 deletions.
18 changes: 18 additions & 0 deletions CastleFluentNHibernateMvc3.Tests/Controllers/HomeControllerTest.cs
Expand Up @@ -64,10 +64,16 @@ public void Test()
{
store
};

storeRepositoryMock.Setup( s => s.BeginTransaction() )
.Verifiable();

storeRepositoryMock.Setup( s => s.Get( It.IsAny<Expression<Func<Store, bool>>>() ) )
.Returns( stores.AsQueryable() )
.Verifiable();

storeRepositoryMock.Setup( s => s.Commit() )
.Verifiable();

// Act
var result = controller.Test();
Expand All @@ -86,10 +92,16 @@ public void Test_NotFound()
var controller = GetHomeController();

var stores = new List<Store>();

storeRepositoryMock.Setup( s => s.BeginTransaction() )
.Verifiable();

storeRepositoryMock.Setup( s => s.Get( It.IsAny<Expression<Func<Store, bool>>>() ) )
.Returns( stores.AsQueryable() )
.Verifiable();

storeRepositoryMock.Setup( s => s.Rollback() )
.Verifiable();

// Act
var result = controller.Test();
Expand Down Expand Up @@ -117,10 +129,16 @@ public void Seed()
Name = "Bar Bohemia"
}
};

storeRepositoryMock.Setup( s => s.BeginTransaction() )
.Verifiable();

storeRepositoryMock.Setup( s => s.SaveOrUpdateAll( It.IsAny<Store>(), It.IsAny<Store>() ) )
.Returns( stores.AsEnumerable() )
.Verifiable();

storeRepositoryMock.Setup( s => s.Commit() )
.Verifiable();

// Act
var result = controller.Seed();
Expand Down
40 changes: 34 additions & 6 deletions CastleFluentNHibernateMvc3/Controllers/HomeController.cs
Expand Up @@ -27,16 +27,31 @@ public ActionResult Index()
// Gets and modifies a single store from our database
public ActionResult Test()
{
StoreRepository.BeginTransaction();

var barginBasin = StoreRepository.Get( s => s.Name == "Bargin Basin" ).SingleOrDefault();

if (barginBasin == null)
{
StoreRepository.Rollback();

return RedirectToAction( "Index" );
}

barginBasin.Name = "Bargain Basin";
try
{
barginBasin.Name = "Bargain Basin";

StoreRepository.Commit();

return RedirectToAction( "Index" );
}
catch
{
StoreRepository.Rollback();

return RedirectToAction( "Index" );
return RedirectToAction( "Error" );
}
}

// Adds sample data to our database
Expand Down Expand Up @@ -68,14 +83,27 @@ public ActionResult Seed()
// The Store-Employee relationship is one-to-many
AddEmployeesToStore( barginBasin, daisy, jack, sue );
AddEmployeesToStore( superMart, bill, joan );

StoreRepository.BeginTransaction();

StoreRepository.SaveOrUpdateAll( barginBasin, superMart );
try
{
StoreRepository.SaveOrUpdateAll( barginBasin, superMart );

StoreRepository.Commit();

return RedirectToAction( "Index" );
return RedirectToAction( "Index" );
}
catch
{
StoreRepository.Rollback();

return RedirectToAction( "Error" );
}
}

// Adds any products that we pass in to the store that we pass in
public static void AddProductsToStore( Store store, params Product[] products )
private void AddProductsToStore( Store store, params Product[] products )
{
foreach (var product in products)
{
Expand All @@ -84,7 +112,7 @@ public static void AddProductsToStore( Store store, params Product[] products )
}

// Adds any employees that we pass in to the store that we pass in
public static void AddEmployeesToStore( Store store, params Employee[] employees )
private void AddEmployeesToStore( Store store, params Employee[] employees )
{
foreach (var employee in employees)
{
Expand Down
3 changes: 3 additions & 0 deletions CastleFluentNHibernateMvc3/Repositories/IRepository.cs
Expand Up @@ -7,6 +7,9 @@ namespace CastleFluentNHibernateMvc3.Repositories
{
public interface IRepository<T>
{
void BeginTransaction();
void Commit();
void Rollback();
IQueryable<T> GetAll();
IQueryable<T> Get( Expression<Func<T, bool>> predicate );
IEnumerable<T> SaveOrUpdateAll( params T[] entities );
Expand Down
15 changes: 15 additions & 0 deletions CastleFluentNHibernateMvc3/Repositories/Repository.cs
Expand Up @@ -17,6 +17,21 @@ public Repository( ISession session )
Session = session;
}

public void BeginTransaction()
{
Session.BeginTransaction();
}

public void Commit()
{
Session.Transaction.Commit();
}

public void Rollback()
{
Session.Transaction.Rollback();
}

public IQueryable<T> GetAll()
{
return Session.Query<T>();
Expand Down
22 changes: 12 additions & 10 deletions CastleFluentNHibernateMvc3/Views/Home/Index.cshtml
Expand Up @@ -9,17 +9,19 @@
<ul>
@foreach ( var store in Model ) {
<li>@store.Name</li>
<li>Products:</li>
<ul>
@foreach ( var product in store.Products ) {
<li>@product.Name</li>
}
</ul>
<li>Staff:</li>
<ul>
@foreach ( var employee in store.Staff ) {
<li>@employee.FirstName @employee.LastName</li>
}
<li>Products:</li>
<ul>
@foreach ( var product in store.Products ) {
<li>@product.Name</li>
}
</ul>
<li>Staff:</li>
<ul>
@foreach ( var employee in store.Staff ) {
<li>@employee.FirstName @employee.LastName</li>
}
</ul>
</ul>
}
</ul>
3 changes: 2 additions & 1 deletion CastleFluentNHibernateMvc3/Windsor/PersistenceFacility.cs
Expand Up @@ -6,6 +6,7 @@
using FluentNHibernate.Conventions.Helpers;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Context;
using NHibernate.Tool.hbm2ddl;

namespace CastleFluentNHibernateMvc3.Windsor
Expand Down Expand Up @@ -37,7 +38,7 @@ private static ISessionFactory CreateSessionFactory()
.ExposeConfiguration( c =>
{
BuildSchema( c );
c.Properties[ NHibernate.Cfg.Environment.CurrentSessionContextClass ] = "web";
c.CurrentSessionContext<WebSessionContext>();
} )
.BuildSessionFactory();
}
Expand Down

0 comments on commit d8e98d8

Please sign in to comment.