Mercurial > pub > ImplabNet
annotate Implab.ServiceHost/Unity/ContainerConfigurationSchema.cs @ 276:b4e0f81c7425 v3
container configuration docs
author | cin |
---|---|
date | Sat, 28 Apr 2018 00:11:38 +0300 |
parents | 22629bf26121 |
children | 963b17c275be |
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 |
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff
changeset
|
36 if(!type.IsSubclassOf(typeof(ContainerItemElement))) |
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff
changeset
|
37 throw new Exception($"RegisterContainerElement '{name}': {type} must be subclass of {typeof(ContainerItemElement)}"); |
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 |
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff
changeset
|
44 public void RegisterContainerElement<T>(string name) where T : ContainerItemElement { |
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"); |
274 | 58 schema.RegisterContainerElement<SerializedElement>("serialized"); |
59 schema.RegisterContainerElement<ValueElement>("value"); | |
273
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff
changeset
|
60 schema.RegisterContainerElement<IncludeElement>("include"); |
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff
changeset
|
61 schema.RegisterContainerElement<AssemblyElement>("assembly"); |
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff
changeset
|
62 schema.RegisterContainerElement<NamespaceElement>("namespace"); |
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff
changeset
|
63 |
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff
changeset
|
64 return schema; |
79110a16cab7
Working on Unity xml configuration: Refactoring in progress
cin
parents:
diff
changeset
|
65 } |
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 } |