Skip to content

Commit 353c2ea

Browse files
committed
Replace IObjectsFactory with IServiceProvider interface.
1 parent 09d9902 commit 353c2ea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+753
-339
lines changed

src/NHibernate.Test/Bytecode/ActivatorObjectFactoryFixture.cs

Lines changed: 0 additions & 79 deletions
This file was deleted.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using NHibernate.Bytecode;
3+
using NUnit.Framework;
4+
5+
namespace NHibernate.Test.Bytecode
6+
{
7+
[TestFixture]
8+
public class DefaultServiceProviderFixture
9+
{
10+
public class WithOutPublicParameterLessCtor
11+
{
12+
public string Something { get; set; }
13+
protected WithOutPublicParameterLessCtor() { }
14+
15+
public WithOutPublicParameterLessCtor(string something)
16+
{
17+
Something = something;
18+
}
19+
}
20+
21+
public class PublicParameterLessCtor
22+
{
23+
}
24+
25+
protected virtual IServiceProvider GetServiceProvider()
26+
{
27+
return new DefaultServiceProvider();
28+
}
29+
30+
[Test]
31+
public void CreateInstanceDefCtor()
32+
{
33+
var sp = GetServiceProvider();
34+
Assert.Throws<ArgumentNullException>(() => sp.GetService(null));
35+
Assert.Throws<MissingMethodException>(() => sp.GetService(typeof(WithOutPublicParameterLessCtor)));
36+
var instance = sp.GetService(typeof(PublicParameterLessCtor));
37+
Assert.That(instance, Is.Not.Null);
38+
Assert.That(instance, Is.InstanceOf<PublicParameterLessCtor>());
39+
}
40+
41+
42+
[Test]
43+
public void RegisterService()
44+
{
45+
var sp = new DefaultServiceProvider();
46+
47+
Assert.That(sp.GetService(typeof(IInterceptor)), Is.Null);
48+
49+
sp.Register<IInterceptor, EmptyInterceptor>();
50+
var instance = sp.GetService(typeof(IInterceptor));
51+
Assert.That(instance, Is.Not.Null);
52+
Assert.That(instance, Is.InstanceOf<EmptyInterceptor>());
53+
54+
Assert.Throws<InvalidOperationException>(() => sp.Register<IInterceptor, EmptyInterceptor>(), "service should not be registered twice.");
55+
Assert.Throws<InvalidOperationException>(() => sp.Register<Dialect.Dialect, Dialect.Dialect>(), "non concrete implementation type should not be permitted.");
56+
Assert.Throws<InvalidOperationException>(() => sp.Register(typeof(Dialect.Dialect), typeof(EmptyInterceptor)), "concrete implementation type should derive from service type.");
57+
}
58+
59+
[Test]
60+
public void RegisterServiceCreator()
61+
{
62+
var sp = new DefaultServiceProvider();
63+
64+
Assert.That(sp.GetService(typeof(IInterceptor)), Is.Null);
65+
66+
sp.Register<IInterceptor>(() => new EmptyInterceptor());
67+
var instance = sp.GetService(typeof(IInterceptor));
68+
Assert.That(instance, Is.Not.Null);
69+
Assert.That(instance, Is.InstanceOf<EmptyInterceptor>());
70+
71+
Assert.Throws<InvalidOperationException>(() => sp.Register<IInterceptor>(() => new EmptyInterceptor()), "service should not be registered twice.");
72+
}
73+
74+
}
75+
}

src/NHibernate.Test/CfgTest/ConfigurationSchemaFixture.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ public void IgnoreSystemOutOfAppConfig()
7373
}
7474

7575
[Test]
76-
public void ObjectsFactory()
76+
public void ServiceProvider()
7777
{
7878
Assume.That(TestsContext.ExecutingWithVsTest, Is.False);
7979

8080
var xml =
8181
@"<?xml version='1.0' encoding='utf-8' ?>
8282
<hibernate-configuration xmlns='urn:nhibernate-configuration-2.2'>
83-
<objects-factory type='test'/>
83+
<service-provider type='test'/>
8484
<session-factory>
8585
</session-factory>
8686
</hibernate-configuration>";
@@ -89,11 +89,11 @@ public void ObjectsFactory()
8989
using (var xtr = new XmlTextReader(xml, XmlNodeType.Document, null))
9090
{
9191
hc = new HibernateConfiguration(xtr);
92-
Assert.That(hc.ObjectsFactoryType, Is.Null);
92+
Assert.That(hc.ServiceProviderType, Is.Null);
9393
}
9494

9595
hc = HibernateConfiguration.FromAppConfig(xml);
96-
Assert.That(hc.ObjectsFactoryType, Is.EqualTo("test"));
96+
Assert.That(hc.ServiceProviderType, Is.EqualTo("test"));
9797
}
9898

9999
[Test]

src/NHibernate.Test/CfgTest/CustomObjectsFactoryTest.cs

Lines changed: 0 additions & 66 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using NHibernate.Bytecode;
4+
using NUnit.Framework;
5+
using Environment = NHibernate.Cfg.Environment;
6+
7+
namespace NHibernate.Test.CfgTest
8+
{
9+
[TestFixture]
10+
public class CustomServiceProviderTest
11+
{
12+
private class MyServiceProvider : IServiceProvider
13+
{
14+
public object GetService(System.Type serviceType)
15+
{
16+
throw new NotImplementedException();
17+
}
18+
}
19+
private class InvalidServiceProvider
20+
{
21+
}
22+
private class InvalidNoCtorServiceProvider : MyServiceProvider
23+
{
24+
public InvalidNoCtorServiceProvider(string pizza) {}
25+
}
26+
27+
[Test]
28+
public void WhenNoShortCutUsedThenCanBuildServiceProvider()
29+
{
30+
var properties = new Dictionary<string, string> { { Environment.PropertyBytecodeProvider, typeof(MyServiceProvider).AssemblyQualifiedName } };
31+
Assert.That(() => Environment.BuildServiceProvider(properties), Throws.Nothing);
32+
}
33+
34+
[Test]
35+
public void WhenNoShortCutUsedThenCanBuildInstanceOfConfiguredServiceProvider()
36+
{
37+
var properties = new Dictionary<string, string> { { Environment.PropertyServiceProvider, typeof(MyServiceProvider).AssemblyQualifiedName } };
38+
Assert.That(Environment.BuildServiceProvider(properties), Is.InstanceOf<MyServiceProvider>());
39+
}
40+
41+
[Test]
42+
public void WhenInvalidThenThrow()
43+
{
44+
var properties = new Dictionary<string, string> { { Environment.PropertyServiceProvider, typeof(InvalidServiceProvider).AssemblyQualifiedName } };
45+
Assert.That(() => Environment.BuildServiceProvider(properties), Throws.TypeOf<HibernateServiceProviderException>());
46+
}
47+
48+
[Test]
49+
public void WhenNoDefaultCtorThenThrow()
50+
{
51+
var properties = new Dictionary<string, string> { { Environment.PropertyServiceProvider, typeof(InvalidNoCtorServiceProvider).AssemblyQualifiedName } };
52+
Assert.That(() => Environment.BuildServiceProvider(properties), Throws.TypeOf<HibernateServiceProviderException>()
53+
.And.InnerException.Message.Contains("constructor was not found"));
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)