SO6783136
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 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