Created
July 3, 2012 18:10
-
-
Save brianium/3041464 to your computer and use it in GitHub Desktop.
NHibernate simplified with helper class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
//Create Session Provider With CurrentSessionContext | |
var provider = new SessionFactoryProvider<ThreadStaticSessionContext>( | |
MsSqlConfiguration.MsSql2008.ConnectionString("server=.\\SQLEXPRESS;database=NH3BeginnersGuide;Integrated Security=SSPI"), | |
typeof(SessionFactoryProvider<>), | |
(cfg) => { | |
var schemaExport = new SchemaExport(cfg); | |
schemaExport.Drop(false, true); | |
schemaExport.Create(false,true); | |
} | |
); | |
//Bind a new session to the current session context | |
provider.BindNew(); | |
//Use current session to query a product | |
provider.GetCurrentSession().Save(new Product() {Name = "Pop Tart"}); | |
provider.GetCurrentSession().Get<Product>(1).Name.Dump(); | |
//Cleanup | |
provider.Unbind(); | |
} | |
// Define other methods and classes here | |
public class SessionFactoryProvider<T> where T: ICurrentSessionContext | |
{ | |
public FluentConfiguration Config {get; private set;} | |
public ISessionFactory SessionFactory {get; private set;} | |
public SessionFactoryProvider(IPersistenceConfigurer config, Type mappingAssemblyType, Action<Configuration> configHook = null) | |
{ | |
Config = Fluently.Configure(); | |
Config = Config.Database(config); | |
Config.Mappings(m => m.FluentMappings.AddFromAssembly(mappingAssemblyType.Assembly)); | |
if(configHook != null) | |
Config.ExposeConfiguration(configHook).BuildConfiguration(); | |
SessionFactory = Config.CurrentSessionContext<T>().BuildSessionFactory(); | |
} | |
public void BindNew() | |
{ | |
CurrentSessionContext.Bind(OpenSession()); | |
} | |
public void Unbind() | |
{ | |
var sess = CurrentSessionContext.Unbind(SessionFactory); | |
sess.Dispose(); | |
} | |
public ISession OpenSession() | |
{ | |
return SessionFactory.OpenSession(); | |
} | |
public ISession GetCurrentSession() | |
{ | |
return SessionFactory.GetCurrentSession(); | |
} | |
} | |
public class Category | |
{ | |
public virtual int Id {get;set;} | |
public virtual string Name {get;set;} | |
public virtual string Description {get;set;} | |
} | |
public class Product | |
{ | |
public virtual int Id {get;set;} | |
public virtual string Name {get;set;} | |
public virtual string Description {get;set;} | |
public virtual Category Category {get;set;} | |
public virtual decimal UnitPrice {get;set;} | |
public virtual int ReorderLevel {get;set;} | |
public virtual bool Discontinued {get;set;} | |
} | |
public class CategoryMap : ClassMap<Category> | |
{ | |
public CategoryMap() | |
{ | |
Id(x => x.Id); | |
Map(x => x.Name); | |
Map(x => x.Description); | |
} | |
} | |
public class ProductMap : ClassMap<Product> | |
{ | |
public ProductMap() | |
{ | |
Id(p => p.Id); | |
Map(p => p.Name); | |
Map(p => p.Description); | |
Map(p => p.UnitPrice); | |
Map(p => p.ReorderLevel); | |
Map(p => p.Discontinued); | |
References(x => x.Category); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment