Skip to content

Commit e61274a

Browse files
fixup! Replace IObjectsFactory with IServiceProvider interface
Throw explicitly on an error case.
1 parent 79b5c5a commit e61274a

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed
Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
2+
using NHibernate.Bytecode;
63

74
namespace NHibernate.Util
85
{
96
internal static class ServiceProviderExtensions
107
{
8+
/// <summary>
9+
/// Get a service, throwing if it cannot be resolved.
10+
/// </summary>
11+
/// <param name="serviceProvider">The service provider.</param>
12+
/// <param name="serviceType">The service interface, base class or concrete implementation class.</param>
13+
/// <returns>The service instance.</returns>
14+
/// <exception cref="ArgumentNullException">thrown if <paramref name="serviceType"/> is <see langword="null" />.</exception>
15+
/// <exception cref="HibernateServiceProviderException">thrown if the service cannot be resolved.</exception>
1116
public static object GetInstance(this IServiceProvider serviceProvider, System.Type serviceType)
1217
{
13-
// 6.0 TODO throw a meaningful exception instead of using the Activator
14-
return serviceProvider.GetService(serviceType) ?? Activator.CreateInstance(serviceType);
18+
if (serviceType == null)
19+
throw new ArgumentNullException(nameof(serviceType));
20+
var service = serviceProvider.GetService(serviceType);
21+
22+
if (service == null)
23+
throw new HibernateServiceProviderException(
24+
$"Unable to resolve an instance for {serviceType.AssemblyQualifiedName}");
25+
return service;
1526
}
1627
}
1728
}

0 commit comments

Comments
 (0)