diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Implab.ServiceHost/Unity/UnityContainerExtensions.cs	Fri May 04 18:12:42 2018 +0300
@@ -0,0 +1,40 @@
+using System.IO;
+using Unity;
+
+namespace Implab.ServiceHost.Unity
+{
+    public static class UnityContainerExtensions
+    {
+        public static IUnityContainer LoadXmlConfiguration(this IUnityContainer container, string file, ContainerConfigurationSchema schema) {
+            Safe.ArgumentNotNull(container, nameof(container));
+            var builder = new ContainerBuilder(container,schema);
+            builder.LoadConfig(file);
+            return builder.Container;
+        }
+
+        public static IUnityContainer LoadXmlConfiguration(this IUnityContainer container, Stream stream, ContainerConfigurationSchema schema) {
+            Safe.ArgumentNotNull(container, nameof(container));
+            Safe.ArgumentNotNull(stream, nameof(stream));
+            
+            if (schema == null)
+                schema = ContainerConfigurationSchema.Default;
+
+            var builder = new ContainerBuilder(container,schema);
+            var config = (ContainerElement)schema.Serializer.Deserialize(stream);
+            if (config.Items != null) {
+                foreach(var item in config.Items)
+                    item?.Visit(builder);
+            }
+
+            return builder.Container;
+        }
+
+        public static IUnityContainer LoadXmlConfiguration(this IUnityContainer container, Stream stream) {
+            return LoadXmlConfiguration(container, stream, ContainerConfigurationSchema.Default);
+        }
+
+        public static IUnityContainer LoadXmlConfiguration(this IUnityContainer container, string file) {
+            return LoadXmlConfiguration(container, file, ContainerConfigurationSchema.Default);
+        }
+    }
+}
\ No newline at end of file