comparison Implab.ServiceHost/Unity/UnityContainerExtensions.cs @ 279:8714471e8d78 v3

Container configuration cleanup, RC2
author cin
date Fri, 04 May 2018 18:12:42 +0300
parents
children
comparison
equal deleted inserted replaced
278:6691aff01de1 279:8714471e8d78
1 using System.IO;
2 using Unity;
3
4 namespace Implab.ServiceHost.Unity
5 {
6 public static class UnityContainerExtensions
7 {
8 public static IUnityContainer LoadXmlConfiguration(this IUnityContainer container, string file, ContainerConfigurationSchema schema) {
9 Safe.ArgumentNotNull(container, nameof(container));
10 var builder = new ContainerBuilder(container,schema);
11 builder.LoadConfig(file);
12 return builder.Container;
13 }
14
15 public static IUnityContainer LoadXmlConfiguration(this IUnityContainer container, Stream stream, ContainerConfigurationSchema schema) {
16 Safe.ArgumentNotNull(container, nameof(container));
17 Safe.ArgumentNotNull(stream, nameof(stream));
18
19 if (schema == null)
20 schema = ContainerConfigurationSchema.Default;
21
22 var builder = new ContainerBuilder(container,schema);
23 var config = (ContainerElement)schema.Serializer.Deserialize(stream);
24 if (config.Items != null) {
25 foreach(var item in config.Items)
26 item?.Visit(builder);
27 }
28
29 return builder.Container;
30 }
31
32 public static IUnityContainer LoadXmlConfiguration(this IUnityContainer container, Stream stream) {
33 return LoadXmlConfiguration(container, stream, ContainerConfigurationSchema.Default);
34 }
35
36 public static IUnityContainer LoadXmlConfiguration(this IUnityContainer container, string file) {
37 return LoadXmlConfiguration(container, file, ContainerConfigurationSchema.Default);
38 }
39 }
40 }