public void NHUnitOfWorkOperationsTest() { //CREATE = SAVE AND COMMIT using (var uow = new NHUnitOfWork(_sessionFactory)) { // populate the database var input = new List<IMeasure> { new SimpleMeasure("ReteA", 10U) }; var loc = _entitiesFactory.CreateLocation("Location1", input); loc.TotalTime = TimeVal; var input2 = new List<IMeasure> { new SimpleMeasure("ReteB", 50U), new SimpleMeasure("ReteC", 100U) }; var loc2 = _entitiesFactory.CreateLocation("Location2", input2); loc2.TotalTime = TimeVal1; //this saves everything else via cascading uow.Save(loc); uow.Save(loc2); uow.Commit(); } // READ = // Get, GetLocationByName and GetAll using (var uow = new NHUnitOfWork(_sessionFactory)) { var locA = uow.GetLocationByName("Location1"); Assert.AreEqual(locA.Name, "Location1"); Assert.AreEqual(locA.TotalTime, TimeVal); var locB = uow.Get<Location>(locA.Id); Assert.AreEqual(locB.Name, "Location1"); Assert.AreEqual(locB.TotalTime, TimeVal); IList<Location> locations = uow.GetAll<Location>(); Assert.AreEqual(locations.Count, 2); foreach (var location in locations) { //For a visual feedback Console.WriteLine(location.ToString()); if (location.Name == "Location1") { Assert.AreEqual(location.TotalTime, TimeVal); } else if (location.Name == "Location2") { Assert.AreEqual(location.TotalTime, TimeVal1); } else { Log.Debug(location.Name); Assert.Fail("Location name not matching"); } } var networks = uow.GetAll<Network>(); Assert.AreEqual(networks.Count, 3); uow.Commit(); } Location longLivedLocation; const string locName = "Location1"; // UPDATE = SAVE AND COMMIT // update within a single unit of work using (var uow = new NHUnitOfWork(_sessionFactory)) { longLivedLocation = uow.GetLocationByName(locName); longLivedLocation.TotalTime = TimeVal2; uow.Commit(); } using (var uow = new NHUnitOfWork(_sessionFactory)) { var locB = uow.Get<Location>(longLivedLocation.Id); Assert.AreEqual(locB.TotalTime, TimeVal2); } // update of an entity retrieved in a unit of work // BEWARE OF DIRTY WRITES!!! THEY ARE STILL DIFFERENT TRANSACTIONS!!! using (var uow = new NHUnitOfWork(_sessionFactory)) { longLivedLocation.TotalTime = TimeVal3; uow.Save(longLivedLocation); uow.Commit(); } using (var uow = new NHUnitOfWork(_sessionFactory)) { var tmp = uow.GetLocationByName(locName); Assert.AreEqual(tmp.TotalTime, TimeVal3); uow.Commit(); } // ROLLBACK using (var uow = new NHUnitOfWork(_sessionFactory)) { longLivedLocation = uow.GetLocationByName("Location1"); longLivedLocation.TotalTime = TimeVal4; var input = new List<IMeasure> { new SimpleMeasure("ReteC", 10U) }; var loc = _entitiesFactory.CreateLocation("Location3", input); loc.TotalTime = TimeVal; uow.Save(loc); uow.Rollback(); } using (var uow = new NHUnitOfWork(_sessionFactory)) { var locB = uow.Get<Location>(longLivedLocation.Id); //old value Assert.AreEqual(locB.TotalTime, TimeVal3); var locations = uow.GetAll<Location>(); Assert.AreEqual(locations.Count, 2); foreach (var location in locations) { if(location.Name!="Location1" && location.Name!="Location2") { Log.Debug(location.Name); Assert.Fail("Location added in rollbacked unit of work"); } } } // LAZY LOADING // It seems it does not work (always eager loading). Less performances, zero problems... // Check it by hand, looking at logs Log.Debug("LAZY LOADING"); Location locLazy; using (var uow = new NHUnitOfWork(_sessionFactory)) { locLazy = uow.GetLocationByName("Location1"); } Assert.AreEqual(locLazy.Name, "Location1"); Assert.AreEqual(locLazy.TotalTime, TimeVal3); var up = new List<IMeasure> { new SimpleMeasure("ReteA", 20U) }; locLazy.UpdateStats(up); Log.Debug(locLazy.ToString()); IList<Location> locLazies; using (var uow = new NHUnitOfWork(_sessionFactory)) { locLazies = uow.GetAll<Location>(); } up = new List<IMeasure> { new SimpleMeasure("ReteA", 10U), new SimpleMeasure("ReteB", 40U) }; locLazies[1].UpdateStats(up); Log.Debug(locLazies[1].ToString()); // DELETE using (var uow = new NHUnitOfWork(_sessionFactory)) { var locations = uow.GetAll<Location>(); foreach (var location in locations) { uow.Delete(location); } uow.Commit(); } // check deletion cascade using (var uow = new NHUnitOfWork(_sessionFactory)) { var locations = uow.GetAll<Location>(); Assert.AreEqual(locations.Count, 0); var networks = uow.GetAll<Network>(); Assert.AreEqual(networks.Count, 0); } }