CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

Chris Demo

354 Views
Copy Code Show/Hide Line Numbers
using System;
using System.Collections.Generic;
using FluentNHibernate.Automapping;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using Xunit;
 
namespace ChrisDemo
{
    public class Program
    {
        [Fact]
        public void ChrisTest()
        {
            var sessionFactory = CreateSessionFactory();
 
            using (var session = sessionFactory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    // create a couple of Stores each with some Products and Employees
                    var barginBasin = new Store { Name = "Bargin Basin" };
                    var superMart = new Store { Name = "SuperMart" };
 
                    var potatoes = new Product { Name = "Potatoes", Price = 3.60 };
                    var fish = new Product { Name = "Fish", Price = 4.49 };
                    var milk = new Product
                                   {
                                       Name = "Milk",
                                       Price =
                                           0.79
                                   };
                    var bread = new Product
                                    {
                                        Name = "Bread",
                                        Price =
                                            1.29
                                    };
                    var cheese = new Product
                                     {
                                         Name = "Cheese",
                                         Price
                                             = 2.10
                                     };
                    var waffles = new Product
                                      {
                                          Name = "Waffles",
                                          Price = 2.41
                                      };
 
                    var daisy = new Employee
                                    {
                                        FirstName = "Daisy",
                                        LastName = "Harrison"
                                    };
                    var jack = new Employee
                                   {
                                       FirstName = "Jack",
                                       LastName = "Torrance"
                                   };
                    var sue = new Employee
                                  {
                                      FirstName = "Sue",
                                      LastName = "Walkters"
                                  };
                    var bill = new Employee { FirstName = "Bill", LastName = "Taft" };
                    var joan = new Employee { FirstName = "Joan", LastName = "Pope" };
 
                    // add products to the stores, there's some crossover in the products in each
                    // store, because the store-product relationship is many-to-many
                    AddProductsToStore(barginBasin, potatoes, fish, milk, bread, cheese);
                    AddProductsToStore(superMart, bread, cheese, waffles);
 
                    // add employees to the stores, this relationship is a one-to-many, so one
                    // employee can only work at one store at a time
                    AddEmployeesToStore(barginBasin, daisy, jack, sue);
                    AddEmployeesToStore(superMart, bill, joan);
                    // save both stores, this saves everything else via cascading
                    session.SaveOrUpdate(barginBasin);
                    session.SaveOrUpdate(superMart);
 
                    transaction.Commit();
                }
 
                // retreive all stores and display them
                using (session.BeginTransaction())
                {
                    IList<Store> stores = session.CreateCriteria(typeof(Store))
                        .List<Store>();
 
                    foreach (Store store in stores)
                    {
                        WriteStorePretty(store);
                    }
                }
 
                Console.ReadKey();
            }
        }
 
        public static void AddEmployeesToStore(Store store, params Employee[] employees)
        {
            foreach (Employee employee in employees)
            {
                store.AddEmployee(employee);
            }
        }
 
        public static ISessionFactory CreateSessionFactory()
        {
            FluentConfiguration idk = Fluently
                .Configure()
                .Database(OracleDataClientConfiguration
                              .Oracle10
                              .UseReflectionOptimizer()
                              .MaxFetchDepth(3)
                              .AdoNetBatchSize(500)
                              .ConnectionString(cs => cs
                                                          .Server("hostname")
                                                          .Port(1521)
                                                          .Instance("dbname")
                                                          .Username("username")
                                                          .Password("password")
                                                          .Pooling(true)
                                                          .StatementCacheSize(100)
                                                          .OtherOptions(
                                                          "Min Pool Size=10;Incr Pool Size=5;Decr Pool Size=2;")
                              )
                // It does this automatically.. but I like to be explicit ;)
                              .ProxyFactoryFactory("NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle")
                // Testing/NHProf stuff
                //.Raw("generate_statistics", "true")
                              .ShowSql()
                )
                .Mappings(mappings => mappings.AutoMappings.Add(AutoMap.AssemblyOf<Program>().IgnoreBase<Program>).ExportTo(@"C:\"));
 
            return idk.BuildSessionFactory();
        }
 
        public static void BuildSchema(Configuration config)
        {
            // this NHibernate tool takes a configuration (with mapping info in)
            // and exports a database schema from it
            new SchemaExport(config)
                .Create(true, false);
        }
 
        public static void WriteStorePretty(Store store)
        {
            Console.WriteLine(store.Name);
            Console.WriteLine(" Products:");
 
            foreach (var product in store.Products)
            {
                Console.WriteLine(" " + product.Name);
            }
 
            Console.WriteLine(" Staff:");
 
            foreach (var employee in store.Staff)
            {
                Console.WriteLine(" " + employee.FirstName + " " + employee.LastName);
            }
 
            Console.WriteLine();
        }
 
        public static void AddProductsToStore(Store store, params Product[] products)
        {
            foreach (Product product in products)
            {
                store.AddProduct(product);
            }
        }
    }
 
    public class Product
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual double Price { get; set; }
    }
 
    public class Employee
    {
        public virtual int Id { get; set; }
        public virtual string LastName { get; set; }
        public virtual string FirstName { get; set; }
    }
 
    public class Store 
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual IList<Employee> Staff { get; set; }
        public virtual IList<Product> Products { get; set; }
 
        public virtual void AddEmployee(Employee employee)
        {
            throw new NotImplementedException();
        }
 
        public virtual void AddProduct(Product product)
        {
            throw new NotImplementedException();
        }
    }
}
by David R. Longnecker
  September 02, 2009 @ 7:35am
Description:
Quick demo for Chris.

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate