Skip to content

Instantly share code, notes, and snippets.

@nelsonlaquet
Created May 30, 2012 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nelsonlaquet/2839523 to your computer and use it in GitHub Desktop.
Save nelsonlaquet/2839523 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using HibernatingRhinos.Profiler.Appender.NHibernate;
using NHibernate.Cfg;
using NHibernate.Mapping.ByCode;
using NHibernate.Linq;
using NHibernate.Mapping.ByCode.Conformist;
namespace ConsoleApplication155
{
class Person
{
public virtual int Id { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual int Age { get; set; }
public virtual IList<Address> Addresses { get; set; }
public Person()
{
Addresses = new List<Address>();
}
}
class PersonMap : ClassMapping<Person>
{
public PersonMap()
{
Id(i => i.Id, x => x.Generator(Generators.Identity));
Property(x => x.FirstName, x => x.NotNullable(true));
Property(x => x.LastName, x => x.NotNullable(true));
Property(x => x.Age, x => x.NotNullable(true));
Bag(x => x.Addresses, i =>
{
i.Key(x => x.Column("PersonId"));
i.Cascade(Cascade.All | Cascade.DeleteOrphans);
}, x => x.OneToMany());
}
}
class Address
{
public virtual int Id { get; set; }
public virtual string AddressLine { get; set; }
public virtual string AddressLine2 { get; set; }
public virtual string City { get; set; }
public virtual string State { get; set; }
}
class AddressMap : ClassMapping<Address>
{
public AddressMap()
{
Id(i => i.Id, x => x.Generator(Generators.Identity));
Property(x => x.AddressLine, x => x.NotNullable(true));
Property(x => x.AddressLine2);
Property(x => x.City, x => x.NotNullable(true));
Property(x => x.State, x => x.NotNullable(true));
}
}
class Program
{
static void Main()
{
// Initializes profiler if using one
NHibernateProfiler.Initialize();
// Session Factory Factory, config is then used to open session
var config = new Configuration();
// Instructs Nhibernate to look at config file
config.Configure();
// ModelMapper used to convert class mappings into Nhibernate mapping
var mapper = new ModelMapper();
mapper.AddMapping<AddressMap>();
mapper.AddMapping<PersonMap>();
// Telling your configuration about your mappings
config.AddMapping(mapper.CompileMappingForAllExplicitlyAddedEntities());
// Session Factory is the entry point into Nhibernate
// one Session Factory should be used for each data base
using (var sessionFactory = config.BuildSessionFactory())
{
while (true)
{
// Session is what you use to access your data base
using (var session = sessionFactory.OpenSession())
{
Console.WriteLine("[0] - show");
Console.WriteLine("[1] - insert");
Console.WriteLine("[2] - delete");
Console.WriteLine("[3] - update");
var choice = Console.ReadKey(true).KeyChar - '0';
using (var trans = session.BeginTransaction())
{
if (choice == 0)
{
// EXAMPLE: Grab all people with ages greater than 5
var people = session.Query<Person>();
foreach (var person in people)
Console.WriteLine("{0} {1} is {2} years old", person.FirstName, person.LastName, person.Age);
}
else if (choice == 1)
{
var person = new Person
{
FirstName = GetString("First Name"),
LastName = GetString("Last Name"),
Age = GetInt("Age")
};
session.Save(person);
}
else if (choice == 2)
{
foreach (var person in session.Query<Person>())
{
Console.WriteLine("[{0}] - {1} {2}", person.Id, person.FirstName, person.LastName);
}
Console.Write("Delete: ");
var id = int.Parse(Console.ReadLine());
var toDelete = session.Load<Person>(id);
session.Delete(toDelete);
}
else if (choice == 3)
{
foreach (var person in session.Query<Person>())
{
Console.WriteLine("[{0}] - {1} {2}", person.Id, person.FirstName, person.LastName);
}
Console.Write("Update: ");
var id = int.Parse(Console.ReadLine());
var toUpdate = session.Load<Person>(id);
toUpdate.FirstName = GetString("New First Name");
toUpdate.LastName = GetString("New Last Name");
toUpdate.Age = GetInt("New Age");
session.Update(toUpdate);
}
trans.Commit();
}
}
Console.WriteLine("--------------------------------------");
}
}
}
static string GetString(string prompt)
{
Console.Write("{0}: ", prompt);
return Console.ReadLine();
}
static int GetInt(string prompt)
{
return int.Parse(GetString(prompt));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment