annotate Implab.ServiceHost/Unity/ContainerConfigurationSchema.cs @ 279:8714471e8d78 v3

Container configuration cleanup, RC2
author cin
date Fri, 04 May 2018 18:12:42 +0300
parents 6691aff01de1
children f07be402ab02
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
273
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
1 using System;
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
2 using System.Collections.Generic;
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
3 using System.IO;
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
4 using System.Reflection;
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
5 using System.Xml;
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
6 using System.Xml.Serialization;
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
7 using Implab.Components;
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
8
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
9 namespace Implab.ServiceHost.Unity {
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
10 public class ContainerConfigurationSchema {
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
11
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
12 public static ContainerConfigurationSchema Default { get; private set; } = CreateDefault();
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
13
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
14 readonly LazyAndWeak<XmlSerializer> m_seralizer;
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
15
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
16 readonly XmlAttributeOverrides m_overrides = new XmlAttributeOverrides();
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
17
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
18 readonly XmlAttributes m_containerItems = new XmlAttributes();
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
19
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
20 public XmlSerializer Serializer {
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
21 get {
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
22 return m_seralizer.Value;
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
23 }
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
24 }
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
25
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
26 public ContainerConfigurationSchema() {
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
27 m_overrides.Add(typeof(ContainerElement), nameof(ContainerElement.Items), m_containerItems);
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
28
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
29 m_seralizer = new LazyAndWeak<XmlSerializer>(() => new XmlSerializer(typeof(ContainerElement), m_overrides));
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
30 }
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
31
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
32 public void RegisterContainerElement(Type type, string name) {
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
33 Safe.ArgumentNotNull(type, nameof(type));
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
34 Safe.ArgumentNotEmpty(name, nameof(name));
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
35
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
36 if(!type.IsSubclassOf(typeof(AbstractContainerItem)))
963b17c275be Refactoring
cin
parents: 274
diff changeset
37 throw new Exception($"RegisterContainerElement '{name}': {type} must be subclass of {typeof(AbstractContainerItem)}");
273
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
38
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
39 m_containerItems.XmlElements.Add(
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
40 new XmlElementAttribute(name, type)
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
41 );
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
42 }
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
43
277
963b17c275be Refactoring
cin
parents: 274
diff changeset
44 public void RegisterContainerElement<T>(string name) where T : AbstractContainerItem {
273
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
45 RegisterContainerElement(typeof(T), name);
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
46 }
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
47
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
48 public ContainerElement LoadFile(string file) {
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
49 using (var reader = XmlReader.Create(file)) {
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
50 return (ContainerElement)Serializer.Deserialize(reader);
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
51 }
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
52 }
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
53
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
54 static ContainerConfigurationSchema CreateDefault() {
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
55 var schema = new ContainerConfigurationSchema();
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
56
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
57 schema.RegisterContainerElement<RegisterElement>("register");
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 277
diff changeset
58 schema.RegisterContainerElement<FactoryElement>("factory");
274
22629bf26121 Unity xml configuration, alpha2
cin
parents: 273
diff changeset
59 schema.RegisterContainerElement<SerializedElement>("serialized");
22629bf26121 Unity xml configuration, alpha2
cin
parents: 273
diff changeset
60 schema.RegisterContainerElement<ValueElement>("value");
273
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
61 schema.RegisterContainerElement<IncludeElement>("include");
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
62 schema.RegisterContainerElement<AssemblyElement>("assembly");
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
63 schema.RegisterContainerElement<NamespaceElement>("namespace");
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
64
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
65 return schema;
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
66 }
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
67
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
68
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
69 }
79110a16cab7 Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff changeset
70 }