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