Created
August 26, 2009 23:29
-
-
Save cprieto/175945 to your computer and use it in GitHub Desktop.
This file contains 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
using System; | |
using FluentNHibernate; | |
using FluentNHibernate.Cfg.Db; | |
using FluentNHibernate.Mapping; | |
using NHibernate; | |
using NHibernate.Cfg; | |
using NHibernate.Tool.hbm2ddl; | |
using Xunit; | |
using NHibernate.ByteCode.LinFu; | |
namespace ClassLibrary2 | |
{ | |
// Simple Entity | |
public class SimpleEntityMapping : ClassMap<SimpleEntity> | |
{ | |
public SimpleEntityMapping() | |
{ | |
Id(x => x.Id) | |
.GeneratedBy.Native(); | |
Map(x => x.Name) | |
.Not.Nullable() | |
.Length(50); | |
} | |
} | |
// Simple Entity | |
public class SimpleEntity : IEquatable<SimpleEntity> | |
{ | |
public virtual int Id { get; set; } | |
public virtual string Name { get; set; } | |
protected SimpleEntity() { } | |
public SimpleEntity(string name) | |
{ | |
Name = name; | |
} | |
public virtual bool Equals(SimpleEntity other) | |
{ | |
return Id.Equals(other.Id); | |
} | |
} | |
// Simple Entity Facts | |
public class SimpleEntityFact | |
{ | |
public ISession Session { get; set; } | |
public Configuration Configuration { get; set; } | |
private void BuildSchema() | |
{ | |
var builder = new SchemaExport(Configuration); | |
builder.Drop(true, true); | |
builder.Execute(true, true, false, Session.Connection, null); | |
} | |
public SimpleEntityFact() | |
{ | |
var config = SQLiteConfiguration | |
.Standard | |
.InMemory() | |
.ShowSql() | |
.ProxyFactoryFactory<ProxyFactoryFactory>(); | |
Configuration = new Configuration(); | |
Configuration.AddProperties(config.ToProperties()); | |
var model = new PersistenceModel(); | |
model.AddMappingsFromAssembly(typeof(SimpleEntity).Assembly); | |
model.Configure(Configuration); | |
Session = Configuration.BuildSessionFactory().OpenSession(); | |
BuildSchema(); | |
} | |
[Fact] | |
public void Should_insert_a_record_into_database() | |
{ | |
var entity = new SimpleEntity("my name"); | |
using (var tx = Session.BeginTransaction()) { | |
Session.Save(entity); | |
tx.Commit(); | |
} | |
Assert.True(entity.Id > 0); | |
entity.Name = "other name"; | |
using (var tx = Session.BeginTransaction()) { | |
Session.Update(entity); | |
tx.Commit(); | |
} | |
using (var tx = Session.BeginTransaction()) { | |
var other = Session.Get<SimpleEntity>(entity.Id); | |
Assert.Equal(entity.Name, other.Name); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment