##// END OF EJS Templates
Working on Unity xml configuration
cin -
r271:d4d437ec4483 v3
parent child
Show More
@@ -1,54 +1,58
1 using System;
1 using System;
2 using System.Diagnostics;
2 using System.Diagnostics;
3 using Implab.Diagnostics;
3 using Implab.Diagnostics;
4 using Implab.ServiceHost.Unity;
4 using Implab.ServiceHost.Unity;
5 using Implab.Xml;
5 using Implab.Xml;
6 using Unity;
6 using Unity;
7 using Unity.Injection;
7 using Unity.Injection;
8
8
9 namespace Implab.Playground {
9 namespace Implab.Playground {
10
10
11 public class Foo {
11 public class Foo {
12
12
13 public class Bar {
13 public class Bar {
14
14
15 }
15 }
16
16
17 public string Name { get; set; }
17 public string Name { get; set; }
18
18
19 public int IntValue { get; set; }
19 public int IntValue { get; set; }
20
20
21 public string StringValue { get; set; }
21 public string StringValue { get; set; }
22
22
23 }
23 }
24
24
25 public class Container<T> {
25 public class Container<T> {
26 public Container() {
26 public Container() {
27
27
28 }
28 }
29
29
30 public Container(T instance) {
30 public Container(T instance) {
31 Instance = instance;
31 Instance = instance;
32 }
32 }
33
33
34 public T Instance { get; set; }
34 public T Instance { get; set; }
35
35
36 public void SetInstance(T value) {
36 public void SetInstance(T value) {
37 Instance = value;
37 Instance = value;
38 }
38 }
39 }
39 }
40
40
41 public class Program {
41 public class Program {
42
42
43 static void Main(string[] args) {
43 static void Main(string[] args) {
44 var container = new UnityContainer();
44 var container = new UnityContainer();
45
45
46 var ctx = new ConfigurationContext(container);
47
46 var conf = SerializationHelpers.DeserializeFromFile<ContainerElement>("data/sample.xml");
48 var conf = SerializationHelpers.DeserializeFromFile<ContainerElement>("data/sample.xml");
47
49
50 ctx.Visit(conf);
51
48 Console.WriteLine($"Registrations: {conf.Items.Count}");
52 Console.WriteLine($"Registrations: {conf.Items.Count}");
49
53
50 }
54 }
51
55
52
56
53 }
57 }
54 }
58 }
@@ -1,22 +1,34
1 using System;
1 using System;
2 using System.Xml.Serialization;
2 using System.Xml.Serialization;
3 using Unity.Lifetime;
3 using Unity.Lifetime;
4 using Unity.Registration;
4 using Unity.Registration;
5
5
6 namespace Implab.ServiceHost.Unity
6 namespace Implab.ServiceHost.Unity
7 {
7 {
8 public abstract class AbstractRegistration : IConfigurationElement {
8 public abstract class AbstractRegistration : IConfigurationElement {
9
9
10 /// <summary>
10 /// <summary>
11 /// An optional name for a registration in the container
11 /// An optional name for a registration in the container
12 /// </summary>
12 /// </summary>
13 [XmlAttribute("name")]
13 [XmlAttribute("name")]
14 public string Name {
14 public string Name {
15 get; set;
15 get; set;
16 }
16 }
17
17
18 [XmlElement("signleton", typeof(SimgletonLifetimeElement))]
19 [XmlElement("context", typeof(ContextLifetimeElement))]
20 [XmlElement("container", typeof(ContainerLifetimeElement))]
21 [XmlElement("hierarchy", typeof(HierarchicalLifetimeElement))]
22 public LifetimeElement Lifetime {get; set;}
23
24 /// <summary>
25 /// A type specification for the service registration,
26 /// </summary>
27 [XmlAttribute("provides")]
28 public string ProvidesType { get; set; }
29
18 public void Visit(ConfigurationContext context) {
30 public void Visit(ConfigurationContext context) {
19 context.Visit(this);
31 context.Visit(this);
20 }
32 }
21 }
33 }
22 } No newline at end of file
34 }
@@ -1,47 +1,50
1 using System;
1 using System;
2 using System.Collections.Generic;
2 using System.Collections.Generic;
3 using System.Reflection;
3 using System.Reflection;
4 using System.Xml;
4 using System.Xml;
5 using System.Xml.Serialization;
5 using System.Xml.Serialization;
6 using Implab.Components;
6 using Implab.Components;
7
7
8 namespace Implab.ServiceHost.Unity {
8 namespace Implab.ServiceHost.Unity {
9 public class ConfigurationSchema {
9 public class ConfigurationSchema {
10
10
11 public static ConfigurationSchema Default { get; private set; } = CreateDefault();
11 public static ConfigurationSchema Default { get; private set; } = CreateDefault();
12
12
13 readonly Dictionary<Tuple<string,string>, LazyAndWeak<XmlSerializer>> m_mappings = new Dictionary<Tuple<string, string>, LazyAndWeak<XmlSerializer>>();
13 readonly Dictionary<Tuple<string,string>, LazyAndWeak<XmlSerializer>> m_mappings = new Dictionary<Tuple<string, string>, LazyAndWeak<XmlSerializer>>();
14
14
15 public void DefineMapping(string name, string ns, Type type) {
15 public void DefineMapping(string name, string ns, Type type) {
16 Safe.ArgumentNotEmpty(name, nameof(name));
16 Safe.ArgumentNotEmpty(name, nameof(name));
17 Safe.ArgumentNotNull(type, nameof(type));
17 Safe.ArgumentNotNull(type, nameof(type));
18 ns = ns ?? string.Empty;
18 ns = ns ?? string.Empty;
19 m_mappings[Tuple.Create(name, ns)] = new LazyAndWeak<XmlSerializer>(() => new XmlSerializer(type), true);
19 m_mappings[Tuple.Create(name, ns)] = new LazyAndWeak<XmlSerializer>(() => new XmlSerializer(type), true);
20 }
20 }
21
21
22 public void DefineMapping<T>() {
22 public void DefineMapping<T>() {
23 var xmlRoot = typeof(T).GetCustomAttribute<XmlRootAttribute>();
23 var xmlRoot = typeof(T).GetCustomAttribute<XmlRootAttribute>();
24 var ns = xmlRoot?.Namespace;
24 var ns = xmlRoot?.Namespace;
25 var root = xmlRoot?.ElementName ?? typeof(T).Name;
25 var root = xmlRoot?.ElementName ?? typeof(T).Name;
26 DefineMapping(root, ns, typeof(T));
26 DefineMapping(root, ns, typeof(T));
27 }
27 }
28
28
29 public T Deserialize<T>(XmlReader reader) {
29 public T Deserialize<T>(XmlReader reader) {
30 reader.MoveToContent();
30 reader.MoveToContent();
31 var name = reader.Name;
31 var name = reader.Name;
32 var ns = reader.NamespaceURI;
32 var ns = reader.NamespaceURI;
33
33
34 return (T)m_mappings[Tuple.Create(name, ns)].Value.Deserialize(reader);
34 return (T)m_mappings[Tuple.Create(name, ns)].Value.Deserialize(reader);
35 }
35 }
36
36
37 static ConfigurationSchema CreateDefault() {
37 static ConfigurationSchema CreateDefault() {
38 var schema = new ConfigurationSchema();
38 var schema = new ConfigurationSchema();
39
39
40 schema.DefineMapping<RegisterElement>();
40 schema.DefineMapping<RegisterElement>();
41 schema.DefineMapping<IncludeElement>();
42 schema.DefineMapping<AssemblyElement>();
43 schema.DefineMapping<NamespaceElement>();
41
44
42 return schema;
45 return schema;
43 }
46 }
44
47
45
48
46 }
49 }
47 } No newline at end of file
50 }
@@ -1,28 +1,31
1 using Implab.Xml;
1 using Implab.Xml;
2 using System.Collections.Generic;
2 using System.Collections.Generic;
3 using System.Xml;
3 using System.Xml;
4 using System.Xml.Schema;
4 using System.Xml.Schema;
5 using System.Xml.Serialization;
5 using System.Xml.Serialization;
6
6
7 namespace Implab.ServiceHost.Unity {
7 namespace Implab.ServiceHost.Unity {
8 [XmlRoot("container", Namespace = Schema.ContainerConfigurationNamespace)]
8 [XmlRoot("container", Namespace = Schema.ContainerConfigurationNamespace)]
9 public class ContainerElement : IXmlSerializable {
9 public class ContainerElement : IXmlSerializable {
10
10
11 public List<IConfigurationElement> Items {get; set; } = new List<IConfigurationElement>();
11 public List<IConfigurationElement> Items {get; set; } = new List<IConfigurationElement>();
12
12
13 public XmlSchema GetSchema() {
13 public XmlSchema GetSchema() {
14 return null;
14 return null;
15 }
15 }
16
16
17 public void ReadXml(XmlReader reader) {
17 public void ReadXml(XmlReader reader) {
18 while(reader.Read() && reader.NodeType != XmlNodeType.EndElement) {
18 while(reader.Read() && reader.NodeType != XmlNodeType.EndElement) {
19 var registration = ConfigurationSchema.Default.Deserialize<IConfigurationElement>(reader);
19 var registration = ConfigurationSchema.Default.Deserialize<IConfigurationElement>(reader);
20 Items.Add(registration);
20 Items.Add(registration);
21 }
21 }
22 }
22 }
23
23
24 public void WriteXml(XmlWriter writer) {
24 public void WriteXml(XmlWriter writer) {
25 throw new System.NotImplementedException();
25 foreach(var item in Items) {
26 var serializer = new XmlSerializer(item.GetType());
27 serializer.Serialize(writer, item);
28 }
26 }
29 }
27 }
30 }
28 } No newline at end of file
31 }
@@ -1,36 +1,24
1 using System;
1 using System;
2 using System.Xml.Serialization;
2 using System.Xml.Serialization;
3 using Unity.Lifetime;
3 using Unity.Lifetime;
4 using Unity.Registration;
4 using Unity.Registration;
5
5
6 namespace Implab.ServiceHost.Unity {
6 namespace Implab.ServiceHost.Unity {
7
7
8 [XmlRoot("register", Namespace = Schema.ContainerConfigurationNamespace)]
8 [XmlRoot("register", Namespace = Schema.ContainerConfigurationNamespace)]
9 public class RegisterElement : AbstractRegistration {
9 public class RegisterElement : AbstractRegistration {
10
10
11 /// <summary>
11 /// <summary>
12 /// An optional type specification for the service registration,
12 /// An optional type which is registered as a service in the container, must be assignable to <see cref="ProvidesType">.
13 /// must be assignable from the type specified by <see cref="ImplementedType"/>
14 /// </summary>
15 [XmlAttribute("provides")]
16 public string ProvidesType { get; set; }
17
18 /// <summary>
19 /// The type which is registered as a service in the container.
20 /// </summary>
13 /// </summary>
21 [XmlAttribute("type")]
14 [XmlAttribute("type")]
22 public string ImplementedType { get; set; }
15 public string ImplementedType { get; set; }
23
16
24 [XmlElement("signleton", typeof(SimgletonLifetimeElement))]
25 [XmlElement("context", typeof(ContextLifetimeElement))]
26 [XmlElement("container", typeof(ContainerLifetimeElement))]
27 [XmlElement("hierarchy", typeof(HierarchicalLifetimeElement))]
28 public LifetimeElement Lifetime {get; set;}
29
17
30 [XmlElement("constructor", typeof(ConstructorInjectionElement))]
18 [XmlElement("constructor", typeof(ConstructorInjectionElement))]
31 [XmlElement("property", typeof(PropertyInjectionElement))]
19 [XmlElement("property", typeof(PropertyInjectionElement))]
32 [XmlElement("method", typeof(MethodInjectionElement))]
20 [XmlElement("method", typeof(MethodInjectionElement))]
33 public AbstractInjectionElement[] Injectors { get; set; }
21 public AbstractInjectionElement[] Injectors { get; set; }
34 }
22 }
35
23
36 } No newline at end of file
24 }
@@ -1,7 +1,12
1 using System.Xml;
2 using System.Xml.Schema;
3 using System.Xml.Serialization;
4
1 namespace Implab.ServiceHost.Unity
5 namespace Implab.ServiceHost.Unity
2 {
6 {
3 public class SerializedParameterElement : InjectionParameterElement
7 public class SerializedParameterElement : InjectionParameterElement {
4 {
8
5
9 [XmlAnyElement]
10 public XmlElement[] Content { get; set; }
6 }
11 }
7 } No newline at end of file
12 }
General Comments 3
Under Review
author

Auto status change to "Under Review"

Approved
author

ok, latest stable version should be in default

You need to be logged in to leave comments. Login now