Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="Chinchilla Software Limited">
4 : // // Copyright Chinchilla Software Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System;
10 : using System.Collections.Generic;
11 : using System.Linq;
12 : using System.Runtime.Serialization;
13 :
14 : namespace Cqrs.Services
15 : {
16 : /// <summary>
17 : /// Configuration information for setting up WCF <see cref="DataContractResolver">resolvers</see>.
18 : /// </summary>
19 : public class WcfDataContractResolverConfiguration
20 1 : {
21 : /// <summary>
22 : /// The current instance of <see cref="WcfDataContractResolverConfiguration"/> to use.
23 : /// </summary>
24 : public static WcfDataContractResolverConfiguration Current { get; protected set; }
25 :
26 : /// <summary>
27 : /// Gets or set the data contracts for the system to use.
28 : /// </summary>
29 : protected IDictionary<Type, IDictionary<string, Type>> DataContracts { get; private set; }
30 :
31 : /// <summary>
32 : /// Instantiates a new instance of <see cref="WcfDataContractResolverConfiguration"/>
33 : /// </summary>
34 1 : public WcfDataContractResolverConfiguration()
35 : {
36 : DataContracts = new Dictionary<Type, IDictionary<string, Type>>();
37 : }
38 :
39 : static WcfDataContractResolverConfiguration()
40 : {
41 : Current = new WcfDataContractResolverConfiguration();
42 : }
43 :
44 : /// <summary>
45 : /// Register the <typeparamref name="TDataContract"/> to use for the operation named <paramref name="operationName"/> for the <typeparamref name="TService"/>.
46 : /// </summary>
47 : /// <typeparam name="TService">The <see cref="Type"/> of service.</typeparam>
48 : /// <typeparam name="TDataContract">The <see cref="Type"/> of the resolver.</typeparam>
49 : /// <param name="operationName">The name of the operation.</param>
50 : /// <param name="registrationHandling">Defaults to <see cref="RegistrationHandling.Replace"/></param>
51 1 : public virtual void RegisterDataContract<TService, TDataContract>(string operationName, RegistrationHandling registrationHandling = RegistrationHandling.Replace)
52 : where TDataContract : new ()
53 : {
54 : Type serviceType = typeof (TService);
55 : IDictionary<string, Type> dataContracts;
56 : if (!DataContracts.TryGetValue(serviceType, out dataContracts))
57 : {
58 : lock (DataContracts)
59 : {
60 : if (!DataContracts.TryGetValue(serviceType, out dataContracts))
61 : {
62 : dataContracts = new Dictionary<string, Type>();
63 : DataContracts.Add(serviceType, dataContracts);
64 : }
65 : }
66 : }
67 : if (dataContracts.ContainsKey(operationName))
68 : {
69 : switch (registrationHandling)
70 : {
71 : case RegistrationHandling.ThrowExceptionOnDuplicate:
72 : dataContracts.Add(operationName, typeof(TDataContract));
73 : break;
74 : case RegistrationHandling.Replace:
75 : dataContracts[operationName] = typeof(TDataContract);
76 : break;
77 : case RegistrationHandling.Nothing:
78 : return;
79 : }
80 : }
81 : else
82 : dataContracts.Add(operationName, typeof(TDataContract));
83 : }
84 :
85 : /// <summary>
86 : /// Gets the <see cref="Type"/> of <see cref="DataContractResolver"/> for the operation named <paramref name="operationName"/>
87 : /// of the <typeparamref name="TService"/>
88 : /// </summary>
89 : /// <typeparam name="TService">The <see cref="Type"/> of service.</typeparam>
90 : /// <param name="operationName">The name of the operation.</param>
91 1 : public virtual Type GetDataContracts<TService>(string operationName)
92 : {
93 : Type serviceType = typeof (TService);
94 : IDictionary<string, Type> dataContracts;
95 : if (!DataContracts.TryGetValue(serviceType, out dataContracts))
96 : {
97 : lock (DataContracts)
98 : {
99 : if (!DataContracts.TryGetValue(serviceType, out dataContracts))
100 : {
101 : dataContracts = new Dictionary<string, Type>();
102 : DataContracts.Add(serviceType, dataContracts);
103 : }
104 : }
105 : }
106 :
107 : Type dataContractType;
108 : if (dataContracts.TryGetValue(operationName, out dataContractType))
109 : return dataContractType;
110 : if (operationName == "GetEventData")
111 : return dataContracts.Values.FirstOrDefault();
112 : return null;
113 : }
114 :
115 : /// <summary>
116 : /// The type of registration action to take
117 : /// </summary>
118 : public enum RegistrationHandling
119 1 : {
120 : /// <summary>
121 : /// Add any new, and replace any existing.
122 : /// </summary>
123 : Replace = 0,
124 :
125 : /// <summary>
126 : /// Throw an <see cref="Exception"/> if one already exists.
127 : /// </summary>
128 : ThrowExceptionOnDuplicate = 1,
129 :
130 : /// <summary>
131 : /// Keep the existing one and don't do anything.
132 : /// </summary>
133 : Nothing = 2
134 : }
135 : }
136 : }
|