-
Notifications
You must be signed in to change notification settings - Fork 0
/
Service.cs
186 lines (146 loc) · 6.16 KB
/
Service.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//
// Juhta.NET, Copyright (c) 2017-2018 Juha Lähteenmäki
//
// This source code may be used, modified and distributed under the terms of
// the MIT license. Please refer to the LICENSE.txt file for details.
//
using Juhta.Net.Common;
using Juhta.Net.Extensions;
using Juhta.Net.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
namespace Juhta.Net.Services
{
/// <summary>
/// Defines a class that encapsulates the metadata of a dependency injection service.
/// </summary>
public class Service
{
#region Public Methods
/// <summary>
/// Creates an instance of the encapsulated dependency injection service.
/// </summary>
/// <typeparam name="TService">Specifies a service type.</typeparam>
/// <returns>Returns the created instance casted to the specified service type.</returns>
public TService CreateInstance<TService>() where TService : class
{
try
{
return(ObjectFactory.CreateInstance<TService>(m_classId, GetConstructorParams()));
}
catch (Exception ex)
{
throw new ServiceCreationException(LibraryMessages.Error004.FormatMessage(this.Id.Value), ex);
}
}
#endregion
#region Public Properties
/// <summary>
/// Gets the identifier of the class that implements the dependency injection service.
/// </summary>
public ClassId ClassId
{
get {return(m_classId);}
}
/// <summary>
/// Gets an array of <see cref="ConstructorParam"/> objects specifying the constructor parameters for the
/// dependency injection service. Can be null.
/// </summary>
public ConstructorParam[] ConstructorParams
{
get {return(m_constructorParams);}
}
/// <summary>
/// Gets the identifier of the dependency injection service.
/// </summary>
public ServiceId Id
{
get {return(m_id);}
}
#endregion
#region Internal Constructors
/// <summary>
/// Initializes a new instance.
/// </summary>
/// <param name="serviceFactory">Specifies a <see cref="ServiceFactory"/> object that will be set to own the
/// instance.</param>
/// <param name="serviceNode">Specifies a service XML node based on which to initialize the instance.</param>
internal Service(ServiceFactory serviceFactory, XmlNode serviceNode)
{
XmlNode constructorParamsNode;
XmlNamespaceManager namespaceManager = FrameworkConfig.CreateNamespaceManager(GetType().Assembly.GetFileName(), serviceNode.OwnerDocument, "v1");
List<ConstructorParam> constructorParams = new List<ConstructorParam>();
m_serviceFactory = serviceFactory;
m_id = new ServiceId(serviceNode.GetAttribute("id"));
try
{
m_classId = new ClassId(serviceNode.GetAttribute("class"), FrameworkInfo.BinDirectory);
constructorParamsNode = serviceNode.SelectSingleNode("ns:constructorParams", namespaceManager);
if (constructorParamsNode == null)
return;
foreach (XmlNode paramNode in constructorParamsNode.ChildNodes)
constructorParams.Add(new ConstructorParam(paramNode));
if (constructorParams.Count == 0)
return;
m_constructorParamObjs = new object[constructorParams.Count];
for (int i = 0; i < m_constructorParamObjs.Length; i++)
m_constructorParamObjs[i] = constructorParams[i].Value;
m_constructorParams = constructorParams.ToArray();
m_hasServiceParams = m_constructorParams.Where(x => x.Type == ConstructorParamType.Service).Count() > 0;
}
catch (Exception ex)
{
throw new ServiceInitializationException(LibraryMessages.Error005.FormatMessage(this.Id.Value), ex);
}
}
#endregion
#region Private Methods
/// <summary>
/// Gets an array of the constructor parameter objects required for creating an instance of the service.
/// </summary>
/// <returns>Returns an array of the required constructor parameter objects.</returns>
private object[] GetConstructorParams()
{
object[] constructorParams;
if (!m_hasServiceParams)
return(m_constructorParamObjs);
constructorParams = new object[m_constructorParams.Length];
for (int i = 0; i < m_constructorParams.Length; i++)
if (m_constructorParams[i].Type != ConstructorParamType.Service)
constructorParams[i] = m_constructorParams[i].Value;
else
constructorParams[i] = m_serviceFactory.CreateService<object>((ServiceId)m_constructorParams[i].Value);
return(constructorParams);
}
#endregion
#region Private Fields
/// <summary>
/// Stores the <see cref="ClassId"/> property.
/// </summary>
private ClassId m_classId;
/// <summary>
/// Stores the <see cref="ConstructorParams"/> property.
/// </summary>
private ConstructorParam[] m_constructorParams;
/// <summary>
/// Specifies an array of the constructor parameters for creating instances of the service. Can be null.
/// </summary>
private object[] m_constructorParamObjs;
/// <summary>
/// Specifies whether the constructor parameters of this service contain service type parameters.
/// </summary>
private bool m_hasServiceParams;
/// <summary>
/// Specifies the <see cref="ServiceFactory"/> object that owns this <see cref="Service"/> instance.
/// </summary>
private ServiceFactory m_serviceFactory;
/// <summary>
/// Stores the <see cref="Id"/> property.
/// </summary>
private ServiceId m_id;
#endregion
}
}