Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Client.Framework;
- using NHibernate;
- namespace Example.Specs.Creators
- {
- // Creator<T> provides an framework for defining streamlined, *test-only* factories for domain entities..
- // it mostly provides needed infrastructure that a test harness can hook into to make it possible to
- // create and persist Nhib entities in a test environment.
- public class Creator<T>
- {
- // the only thing worth knowing about ISessionProvider is that it wraps an NHib ISession
- ISessionProvider _sessionProvider;
- // This represents the various databases that the client has
- DatabaseName _db;
- // we keep an instance of the FluentFixtures ourselves as a member here so that a Creator
- // has access to New, the same as the test code shown in fluent_fixtures_example.cs
- FluentFixtures _fixtures;
- public FluentFixtures New { get { return _fixtures; } }
- public Creator(ISessionProvider sessionProvider, DatabaseName db,FluentFixtures fixtures)
- {
- _sessionProvider = sessionProvider;
- _db = db;
- _fixtures = fixtures;
- }
- public void Flush()
- {
- Session.Flush();
- }
- public void Evict()
- {
- Session.Evict(Creation);
- }
- public void Save()
- {
- Session.Save(Creation);
- // we're flushing left-and-right for convenience.. not performant at all and
- // obviously not aprop. for production use.
- Flush();
- }
- public K SimpleChange<K>(Action<T> input) where K: Creator<T>
- {
- input(Creation);
- Save();
- return (K)this;
- }
- public T Creation { get; set; }
- public ISession Session { get { return _sessionProvider.CurrentSessionOn(_db); } }
- public static implicit operator T (Creator<T> input)
- {
- return input.Creation;
- }
- }
- }
Add Comment
Please, Sign In to add comment