Line data Source code
1 : #region Copyright
2 : // -----------------------------------------------------------------------
3 : // <copyright company="cdmdotnet Limited">
4 : // Copyright cdmdotnet Limited. All rights reserved.
5 : // </copyright>
6 : // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System;
10 : using System.Runtime.Serialization;
11 : using System.Xml;
12 : using Cqrs.Authentication;
13 :
14 : namespace Cqrs.Services
15 : {
16 : /// <summary>
17 : /// A <see cref="DataContractResolver"/> for use via WCF
18 : /// </summary>
19 : public abstract class ServiceParameterResolver<TServiceParameter, TSingleSignOnTokenResolver> : DataContractResolver, IServiceParameterResolver
20 : where TSingleSignOnTokenResolver : ISingleSignOnToken
21 1 : {
22 0 : protected ServiceParameterResolver(ISingleSignOnTokenResolver singleSignOnTokenResolver, IEventDataResolver eventDataResolver)
23 : {
24 : SingleSignOnTokenResolver = singleSignOnTokenResolver;
25 : EventDataResolver = eventDataResolver;
26 : ServiceNamespace = string.Format("https://getcqrs.net/{0}", typeof(TServiceParameter).FullName);
27 : }
28 :
29 : protected ISingleSignOnTokenResolver SingleSignOnTokenResolver { get; private set; }
30 :
31 : protected IEventDataResolver EventDataResolver { get; private set; }
32 :
33 : protected string ServiceNamespace { get; private set; }
34 :
35 0 : public override bool TryResolveType(Type dataContractType, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
36 : {
37 : if (dataContractType == typeof(ServiceResponse))
38 : {
39 : XmlDictionary dictionary = new XmlDictionary();
40 : typeName = dictionary.Add("ServiceResponse");
41 : typeNamespace = dictionary.Add(ServiceNamespace);
42 : return true;
43 : }
44 :
45 : if (dataContractType == typeof(ServiceRequest<TSingleSignOnTokenResolver>))
46 : {
47 : XmlDictionary dictionary = new XmlDictionary();
48 : typeName = dictionary.Add("ServiceRequest");
49 : typeNamespace = dictionary.Add(ServiceNamespace);
50 : return true;
51 : }
52 :
53 : bool result = SingleSignOnTokenResolver.TryResolveType(dataContractType, declaredType, knownTypeResolver, out typeName, out typeNamespace);
54 : if (result)
55 : return true;
56 :
57 : result = EventDataResolver.TryResolveType(dataContractType, declaredType, knownTypeResolver, out typeName, out typeNamespace);
58 : if (result)
59 : return true;
60 :
61 : result = TryResolveUnResolvedType(dataContractType, declaredType, knownTypeResolver, ref typeName, ref typeNamespace);
62 : if (result)
63 : return true;
64 :
65 : // Defer to the known type resolver
66 : return knownTypeResolver.TryResolveType(dataContractType, declaredType, null, out typeName, out typeNamespace);
67 : }
68 :
69 0 : protected abstract bool TryResolveUnResolvedType(Type dataContractType, Type declaredType, DataContractResolver knownTypeResolver, ref XmlDictionaryString typeName, ref XmlDictionaryString typeNamespace);
70 :
71 : /// <summary>
72 : /// Override this method to map the specified xsi:type name and namespace to a data contract type during deserialization.
73 : /// </summary>
74 : /// <returns>
75 : /// The type the xsi:type name and namespace is mapped to.
76 : /// </returns>
77 : /// <param name="typeName">The xsi:type name to map.</param><param name="typeNamespace">The xsi:type namespace to map.</param><param name="declaredType">The type declared in the data contract.</param><param name="knownTypeResolver">The known type resolver.</param>
78 1 : public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
79 : {
80 : if (typeNamespace == ServiceNamespace)
81 : {
82 : if (typeName == "ServiceResponse")
83 : {
84 : return typeof(ServiceResponse);
85 : }
86 :
87 : if (typeName == "ServiceRequest")
88 : {
89 : return typeof(ServiceRequest<TSingleSignOnTokenResolver>);
90 : }
91 : }
92 :
93 : Type result = SingleSignOnTokenResolver.ResolveName(typeName, typeNamespace, declaredType, knownTypeResolver);
94 : if (result != null)
95 : return result;
96 :
97 : result = EventDataResolver.ResolveName(typeName, typeNamespace, declaredType, knownTypeResolver);
98 : if (result != null)
99 : return result;
100 :
101 : result = ResolveUnResolvedName(typeName, typeNamespace, declaredType, knownTypeResolver);
102 : if (result != null)
103 : return result;
104 :
105 : // Defer to the known type resolver
106 : return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null);
107 : }
108 :
109 0 : protected abstract Type ResolveUnResolvedName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver);
110 : }
111 : }
|