public
Last active

SO6783136

  • Download Gist
SO6783136.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
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);
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.