|
From: <fab...@us...> - 2010-08-03 13:04:44
|
Revision: 5104
http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5104&view=rev
Author: fabiomaulo
Date: 2010-08-03 13:04:37 +0000 (Tue, 03 Aug 2010)
Log Message:
-----------
Fix NH-2103
Modified Paths:
--------------
trunk/nhibernate/src/NHibernate/Cfg/Configuration.cs
trunk/nhibernate/src/NHibernate/NHibernate.csproj
trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj
Added Paths:
-----------
trunk/nhibernate/src/NHibernate/Cfg/BindMappingEventArgs.cs
trunk/nhibernate/src/NHibernate.Test/CfgTest/ConfigurationAddMappingEvents.cs
Added: trunk/nhibernate/src/NHibernate/Cfg/BindMappingEventArgs.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Cfg/BindMappingEventArgs.cs (rev 0)
+++ trunk/nhibernate/src/NHibernate/Cfg/BindMappingEventArgs.cs 2010-08-03 13:04:37 UTC (rev 5104)
@@ -0,0 +1,19 @@
+using System;
+using NHibernate.Cfg.MappingSchema;
+
+namespace NHibernate.Cfg
+{
+ public class BindMappingEventArgs: EventArgs
+ {
+ public BindMappingEventArgs(Dialect.Dialect dialect, HbmMapping mapping, string fileName)
+ {
+ Dialect = dialect;
+ Mapping = mapping;
+ FileName = fileName;
+ }
+
+ public Dialect.Dialect Dialect { get; private set; }
+ public HbmMapping Mapping { get; private set; }
+ public string FileName { get; private set; }
+ }
+}
\ No newline at end of file
Modified: trunk/nhibernate/src/NHibernate/Cfg/Configuration.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Cfg/Configuration.cs 2010-08-03 11:41:25 UTC (rev 5103)
+++ trunk/nhibernate/src/NHibernate/Cfg/Configuration.cs 2010-08-03 13:04:37 UTC (rev 5104)
@@ -496,6 +496,9 @@
AddDeserializedMapping(doc.Document, doc.Name);
}
+ public event EventHandler<BindMappingEventArgs> BeforeBindMapping;
+ public event EventHandler<BindMappingEventArgs> AfterBindMapping;
+
/// <summary>
/// Add mapping data using deserialized class.
/// </summary>
@@ -510,9 +513,11 @@
try
{
Dialect.Dialect dialect = Dialect.Dialect.GetDialect(properties);
+ OnBeforeBindMapping(new BindMappingEventArgs(dialect, mappingDocument, documentFileName));
Mappings mappings = CreateMappings(dialect);
new MappingRootBinder(mappings, dialect).Bind(mappingDocument);
+ OnAfterBindMapping(new BindMappingEventArgs(dialect, mappingDocument, documentFileName));
}
catch (Exception e)
{
@@ -523,6 +528,24 @@
}
}
+ private void OnAfterBindMapping(BindMappingEventArgs bindMappingEventArgs)
+ {
+ var handler = AfterBindMapping;
+ if (handler != null)
+ {
+ handler(this, bindMappingEventArgs);
+ }
+ }
+
+ private void OnBeforeBindMapping(BindMappingEventArgs bindMappingEventArgs)
+ {
+ var handler = BeforeBindMapping;
+ if(handler != null)
+ {
+ handler(this, bindMappingEventArgs);
+ }
+ }
+
/// <summary>
/// Create a new <see cref="Mappings" /> to add classes and collection
/// mappings to.
Modified: trunk/nhibernate/src/NHibernate/NHibernate.csproj
===================================================================
--- trunk/nhibernate/src/NHibernate/NHibernate.csproj 2010-08-03 11:41:25 UTC (rev 5103)
+++ trunk/nhibernate/src/NHibernate/NHibernate.csproj 2010-08-03 13:04:37 UTC (rev 5104)
@@ -467,6 +467,7 @@
<Compile Include="Bytecode\ProxyFactoryFactoryNotConfiguredException.cs" />
<Compile Include="Bytecode\UnableToLoadProxyFactoryFactoryException.cs" />
<Compile Include="Cache\FakeCache.cs" />
+ <Compile Include="Cfg\BindMappingEventArgs.cs" />
<Compile Include="Cfg\EntityCacheUsage.cs" />
<Compile Include="Cfg\FilterSecondPassArgs.cs" />
<Compile Include="Cfg\Hbm2ddlKeyWords.cs" />
Added: trunk/nhibernate/src/NHibernate.Test/CfgTest/ConfigurationAddMappingEvents.cs
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/CfgTest/ConfigurationAddMappingEvents.cs (rev 0)
+++ trunk/nhibernate/src/NHibernate.Test/CfgTest/ConfigurationAddMappingEvents.cs 2010-08-03 13:04:37 UTC (rev 5104)
@@ -0,0 +1,76 @@
+using System.Collections.Generic;
+using System.Linq;
+using NHibernate.Cfg;
+using NHibernate.Cfg.Loquacious;
+using NHibernate.Dialect;
+using NUnit.Framework;
+using SharpTestsEx;
+
+namespace NHibernate.Test.CfgTest
+{
+ public class ConfigurationAddMappingEvents
+ {
+ private const string ProductLineMapping =
+ @"<?xml version='1.0' encoding='utf-8' ?>
+<hibernate-mapping xmlns='urn:nhibernate-mapping-2.2'>
+ <class entity-name='ProductLine'>
+ <id name='Id' type='int'>
+ <generator class='hilo'/>
+ </id>
+ <property name='Description' not-null='true' length='200' type='string'/>
+ <bag name='Models' cascade='all' inverse='true'>
+ <key column='productId'/>
+ <one-to-many class='Model'/>
+ </bag>
+ </class>
+</hibernate-mapping>
+";
+ private const string ModelMapping =
+ @"<?xml version='1.0' encoding='utf-8' ?>
+<hibernate-mapping xmlns='urn:nhibernate-mapping-2.2'>
+ <class entity-name='Model'>
+ <id name='Id' type='int'>
+ <generator class='hilo'/>
+ </id>
+
+ <property name='Name' not-null='true' length='25' type='string'/>
+ <property name='Description' not-null='true' length='200' type='string'/>
+ <many-to-one name='ProductLine' column='productId' not-null='true' class='ProductLine'/>
+ </class>
+</hibernate-mapping>
+";
+ [Test]
+ public void WhenSubscribedToBeforeBindThenRaiseEventForEachMapping()
+ {
+ var listOfCalls = new List<BindMappingEventArgs>();
+ var configuration = new Configuration();
+ configuration.DataBaseIntegration(x => x.Dialect<MsSql2008Dialect>());
+ configuration.BeforeBindMapping += (sender, args) => { sender.Should().Be.SameInstanceAs(configuration); listOfCalls.Add(args); };
+
+ configuration.AddXmlString(ProductLineMapping);
+ configuration.AddXmlString(ModelMapping);
+
+ listOfCalls.Count.Should().Be(2);
+ listOfCalls.Select(x => x.FileName).All(x => x.Satisfy(filename => filename != null));
+ listOfCalls.Select(x => x.Mapping).All(x => x.Satisfy(mappingDoc => mappingDoc != null));
+ listOfCalls.Select(x => x.Dialect).All(x => x.Satisfy(dialect => dialect.GetType() == typeof(MsSql2008Dialect)));
+ }
+
+ [Test]
+ public void WhenSubscribedToAfterBindThenRaiseEventForEachMapping()
+ {
+ var listOfCalls = new List<BindMappingEventArgs>();
+ var configuration = new Configuration();
+ configuration.DataBaseIntegration(x => x.Dialect<MsSql2008Dialect>());
+ configuration.AfterBindMapping += (sender, args) => { sender.Should().Be.SameInstanceAs(configuration); listOfCalls.Add(args); };
+
+ configuration.AddXmlString(ProductLineMapping);
+ configuration.AddXmlString(ModelMapping);
+
+ listOfCalls.Count.Should().Be(2);
+ listOfCalls.Select(x => x.FileName).All(x => x.Satisfy(filename => filename != null));
+ listOfCalls.Select(x => x.Mapping).All(x => x.Satisfy(mappingDoc => mappingDoc != null));
+ listOfCalls.Select(x => x.Dialect).All(x => x.Satisfy(dialect => dialect.GetType() == typeof(MsSql2008Dialect)));
+ }
+ }
+}
\ No newline at end of file
Modified: trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2010-08-03 11:41:25 UTC (rev 5103)
+++ trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2010-08-03 13:04:37 UTC (rev 5104)
@@ -109,6 +109,7 @@
<Compile Include="Cascade\JobBatch.cs" />
<Compile Include="Cascade\RefreshFixture.cs" />
<Compile Include="CfgTest\AccessorsSerializableTest.cs" />
+ <Compile Include="CfgTest\ConfigurationAddMappingEvents.cs" />
<Compile Include="CfgTest\ConfigurationFixture.cs" />
<Compile Include="CfgTest\ConfigurationSchemaFixture.cs" />
<Compile Include="CfgTest\ConfigurationSerializationTests.cs" />
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|