annotate Implab.ServiceHost/Unity/ConfigurationSchema.cs @ 271:d4d437ec4483 v3

Working on Unity xml configuration
author cin
date Wed, 25 Apr 2018 19:23:35 +0300
parents ff581cff7003
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
269
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
1 using System;
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
2 using System.Collections.Generic;
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
3 using System.Reflection;
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
4 using System.Xml;
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
5 using System.Xml.Serialization;
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
6 using Implab.Components;
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
7
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
8 namespace Implab.ServiceHost.Unity {
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
9 public class ConfigurationSchema {
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
10
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
11 public static ConfigurationSchema Default { get; private set; } = CreateDefault();
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
12
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
13 readonly Dictionary<Tuple<string,string>, LazyAndWeak<XmlSerializer>> m_mappings = new Dictionary<Tuple<string, string>, LazyAndWeak<XmlSerializer>>();
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
14
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
15 public void DefineMapping(string name, string ns, Type type) {
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
16 Safe.ArgumentNotEmpty(name, nameof(name));
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
17 Safe.ArgumentNotNull(type, nameof(type));
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
18 ns = ns ?? string.Empty;
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
19 m_mappings[Tuple.Create(name, ns)] = new LazyAndWeak<XmlSerializer>(() => new XmlSerializer(type), true);
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
20 }
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
21
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
22 public void DefineMapping<T>() {
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
23 var xmlRoot = typeof(T).GetCustomAttribute<XmlRootAttribute>();
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
24 var ns = xmlRoot?.Namespace;
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
25 var root = xmlRoot?.ElementName ?? typeof(T).Name;
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
26 DefineMapping(root, ns, typeof(T));
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
27 }
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
28
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
29 public T Deserialize<T>(XmlReader reader) {
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
30 reader.MoveToContent();
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
31 var name = reader.Name;
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
32 var ns = reader.NamespaceURI;
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
33
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
34 return (T)m_mappings[Tuple.Create(name, ns)].Value.Deserialize(reader);
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
35 }
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
36
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
37 static ConfigurationSchema CreateDefault() {
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
38 var schema = new ConfigurationSchema();
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
39
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
40 schema.DefineMapping<RegisterElement>();
271
d4d437ec4483 Working on Unity xml configuration
cin
parents: 269
diff changeset
41 schema.DefineMapping<IncludeElement>();
d4d437ec4483 Working on Unity xml configuration
cin
parents: 269
diff changeset
42 schema.DefineMapping<AssemblyElement>();
d4d437ec4483 Working on Unity xml configuration
cin
parents: 269
diff changeset
43 schema.DefineMapping<NamespaceElement>();
269
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
44
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
45 return schema;
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
46 }
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
47
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
48
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
49 }
ff581cff7003 Working on Unity container xml configuration
cin
parents:
diff changeset
50 }