Skip to content

Instantly share code, notes, and snippets.

@codeprogression
Created July 21, 2011 21:58
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save codeprogression/1098337 to your computer and use it in GitHub Desktop.
SO6783136
using System;
using System.Collections.Generic;
using System.Data;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Mapping;
using HibernatingRhinos.Profiler.Appender.NHibernate;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
namespace SO6783136
{
public class Client
{
public Client()
{
Revenue = new List<ClientMonthlyRevenue>();
}
public virtual Guid ClientID { get; set; }
public virtual string ClientName { get; set; }
public virtual IList<ClientMonthlyRevenue> Revenue { get; set; }
public virtual void AddRevenue(ClientMonthlyRevenue revenue)
{
revenue.ParentClient = this;
Revenue.Add(revenue);
}
}
public class ClientMonthlyRevenue : IEquatable<ClientMonthlyRevenue>
{
public ClientMonthlyRevenue()
{
}
public ClientMonthlyRevenue(int year, int month, double revenue)
{
Year = year;
Month = month;
Revenue = revenue;
}
public virtual Client ParentClient { get; set; }
public virtual int Year { get; set; }
public virtual int Month { get; set; }
public virtual double Revenue { get; set; }
public virtual bool Equals(ClientMonthlyRevenue other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Equals(other.ParentClient, ParentClient) && other.Year == Year && other.Month == Month;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof (ClientMonthlyRevenue)) return false;
return Equals((ClientMonthlyRevenue) obj);
}
public override int GetHashCode()
{
unchecked
{
int result = (ParentClient != null ? ParentClient.GetHashCode() : 0);
result = (result*397) ^ Year;
result = (result*397) ^ Month;
return result;
}
}
}
public class ClientMap : ClassMap<Client>
{
public ClientMap()
{
Id(x => x.ClientID).GeneratedBy.Assigned();
Map(x => x.ClientName);
HasMany(x => x.Revenue)
.Table("ClientMonthlyRevenue")
.KeyColumn("ClientID")
.Cascade.All()
.Fetch.Join();
}
}
public class ClientMonthlyRevenueMap : ClassMap<ClientMonthlyRevenue>
{
public ClientMonthlyRevenueMap()
{
CompositeId()
.KeyReference(x => x.ParentClient, "ClientID")
.KeyProperty(x => x.Year)
.KeyProperty(x => x.Month);
Map(x => x.Revenue);
}
}
public class NHibernateConfig
{
public void TestMapping()
{
var sessionFactory = CreateSessionFactory();
var session = sessionFactory.OpenSession();
session.BeginTransaction();
var client = new Client{ClientID = Guid.NewGuid()};
session.SaveOrUpdate(client);
client = session.Get<Client>(client.ClientID);
client.AddRevenue(new ClientMonthlyRevenue(2001,07,1200));
session.Transaction.Commit();
}
public ISessionFactory CreateSessionFactory()
{
NHibernateProfiler.Initialize();
return CreateSessionFactory(GetSqlConfiguration(), BuildSchema);
}
protected virtual Func<IPersistenceConfigurer> GetSqlConfiguration()
{
var msSql2005 = MsSqlConfiguration.MsSql2008;
Action<MsSqlConnectionStringBuilder> expression =
c => c.Server("(local)").Database("StackOverflow").TrustedConnection();
return () => msSql2005.ConnectionString(expression)
.UseReflectionOptimizer()
.IsolationLevel(IsolationLevel.ReadUncommitted)
.ShowSql();
}
public virtual ISessionFactory CreateSessionFactory(Func<IPersistenceConfigurer> configuration,
Action<Configuration> schema)
{
try
{
var configure = Fluently.Configure();
var database = configure.Database(configuration);
var mappings = database.Mappings(CreateMappings());
var fluentConfiguration = mappings.ExposeConfiguration(schema);
return fluentConfiguration.BuildSessionFactory();
}
catch (Exception ex)
{
throw;
}
}
private Action<MappingConfiguration> CreateMappings()
{
return m => m.FluentMappings.Add(typeof(ClientMap)).Add(typeof(ClientMonthlyRevenueMap));
}
protected virtual void BuildSchema(Configuration config)
{
new SchemaExport(config).Create(true, true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment