Skip to content

Commit 9529bc4

Browse files
committed
Moving DataContractUtils to common client
1 parent 6d361ab commit 9529bc4

File tree

2 files changed

+98
-97
lines changed

2 files changed

+98
-97
lines changed

src/ServiceManagement/RecoveryServices/Commands.RecoveryServices/PSRecoveryServicesClient/PSRecoveryServicesClient.cs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
using Microsoft.WindowsAzure.Management.SiteRecovery;
3535
using Microsoft.WindowsAzure.Management.SiteRecovery.Models;
3636
using Microsoft.WindowsAzure.Commands.Utilities.Common.Authentication;
37+
using System.Net.Security;
3738

3839
namespace Microsoft.Azure.Commands.RecoveryServices
3940
{
@@ -262,4 +263,101 @@ private SiteRecoveryManagementClient GetSiteRecoveryClient()
262263
return siteRecoveryClient;
263264
}
264265
}
266+
267+
/// <summary>
268+
/// Helper around serialization/deserialization of objects. This one is a thin wrapper around
269+
/// DataContractUtils template class which is the one doing the heavy lifting.
270+
/// </summary>
271+
[SuppressMessage(
272+
"Microsoft.StyleCop.CSharp.MaintainabilityRules",
273+
"SA1402:FileMayOnlyContainASingleClass",
274+
Justification = "Keeping all contracts together.")]
275+
public static class DataContractUtils
276+
{
277+
/// <summary>
278+
/// Serializes the supplied object to the string.
279+
/// </summary>
280+
/// <typeparam name="T">The object type.</typeparam>
281+
/// <param name="obj">Object to serialize</param>
282+
/// <returns>Serialized string.</returns>
283+
public static string Serialize<T>(T obj)
284+
{
285+
return DataContractUtils<T>.Serialize(obj);
286+
}
287+
288+
/// <summary>
289+
/// Deserialize the string to the expected object type.
290+
/// </summary>
291+
/// <typeparam name="T">The object type</typeparam>
292+
/// <param name="xmlString">Serialized string</param>
293+
/// <param name="result">Deserialized object</param>
294+
public static void Deserialize<T>(string xmlString, out T result)
295+
{
296+
result = DataContractUtils<T>.Deserialize(xmlString);
297+
}
298+
}
299+
300+
/// <summary>
301+
/// Template class for DataContractUtils.
302+
/// </summary>
303+
/// <typeparam name="T">The object type</typeparam>
304+
[SuppressMessage(
305+
"Microsoft.StyleCop.CSharp.MaintainabilityRules",
306+
"SA1402:FileMayOnlyContainASingleClass",
307+
Justification = "Keeping all contracts together.")]
308+
public static class DataContractUtils<T>
309+
{
310+
/// <summary>
311+
/// Serializes the propertyBagContainer to the string.
312+
/// </summary>
313+
/// <param name="propertyBagContainer">Property bag</param>
314+
/// <returns>Serialized string </returns>
315+
public static string Serialize(T propertyBagContainer)
316+
{
317+
var serializer = new DataContractSerializer(typeof(T));
318+
string xmlString;
319+
StringWriter sw = null;
320+
try
321+
{
322+
sw = new StringWriter();
323+
using (var writer = new XmlTextWriter(sw))
324+
{
325+
// Indent the XML so it's human readable.
326+
writer.Formatting = Formatting.Indented;
327+
serializer.WriteObject(writer, propertyBagContainer);
328+
writer.Flush();
329+
xmlString = sw.ToString();
330+
}
331+
}
332+
finally
333+
{
334+
if (sw != null)
335+
{
336+
sw.Close();
337+
}
338+
}
339+
340+
return xmlString;
341+
}
342+
343+
/// <summary>
344+
/// Deserialize the string to the propertyBagContainer.
345+
/// </summary>
346+
/// <param name="xmlString">Serialized string</param>
347+
/// <returns>Deserialized object</returns>
348+
public static T Deserialize(string xmlString)
349+
{
350+
T propertyBagContainer;
351+
using (Stream stream = new MemoryStream())
352+
{
353+
byte[] data = System.Text.Encoding.UTF8.GetBytes(xmlString);
354+
stream.Write(data, 0, data.Length);
355+
stream.Position = 0;
356+
DataContractSerializer deserializer = new DataContractSerializer(typeof(T));
357+
propertyBagContainer = (T)deserializer.ReadObject(stream);
358+
}
359+
360+
return propertyBagContainer;
361+
}
362+
}
265363
}

src/ServiceManagement/RecoveryServices/Commands.RecoveryServices/PSRecoveryServicesClient/PSRecoveryServicesNetworkMappingClient.cs

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -42,103 +42,6 @@ public enum NetworkTargetType
4242
Azure,
4343
}
4444

45-
/// <summary>
46-
/// Helper around serialization/deserialization of objects. This one is a thin wrapper around
47-
/// DataContractUtils template class which is the one doing the heavy lifting.
48-
/// </summary>
49-
[SuppressMessage(
50-
"Microsoft.StyleCop.CSharp.MaintainabilityRules",
51-
"SA1402:FileMayOnlyContainASingleClass",
52-
Justification = "Keeping all contracts together.")]
53-
public static class DataContractUtils
54-
{
55-
/// <summary>
56-
/// Serializes the supplied object to the string.
57-
/// </summary>
58-
/// <typeparam name="T">The object type.</typeparam>
59-
/// <param name="obj">Object to serialize</param>
60-
/// <returns>Serialized string.</returns>
61-
public static string Serialize<T>(T obj)
62-
{
63-
return DataContractUtils<T>.Serialize(obj);
64-
}
65-
66-
/// <summary>
67-
/// Deserialize the string to the expected object type.
68-
/// </summary>
69-
/// <typeparam name="T">The object type</typeparam>
70-
/// <param name="xmlString">Serialized string</param>
71-
/// <param name="result">Deserialized object</param>
72-
public static void Deserialize<T>(string xmlString, out T result)
73-
{
74-
result = DataContractUtils<T>.Deserialize(xmlString);
75-
}
76-
}
77-
78-
/// <summary>
79-
/// Template class for DataContractUtils.
80-
/// </summary>
81-
/// <typeparam name="T">The object type</typeparam>
82-
[SuppressMessage(
83-
"Microsoft.StyleCop.CSharp.MaintainabilityRules",
84-
"SA1402:FileMayOnlyContainASingleClass",
85-
Justification = "Keeping all contracts together.")]
86-
public static class DataContractUtils<T>
87-
{
88-
/// <summary>
89-
/// Serializes the propertyBagContainer to the string.
90-
/// </summary>
91-
/// <param name="propertyBagContainer">Property bag</param>
92-
/// <returns>Serialized string </returns>
93-
public static string Serialize(T propertyBagContainer)
94-
{
95-
var serializer = new DataContractSerializer(typeof(T));
96-
string xmlString;
97-
StringWriter sw = null;
98-
try
99-
{
100-
sw = new StringWriter();
101-
using (var writer = new XmlTextWriter(sw))
102-
{
103-
// Indent the XML so it's human readable.
104-
writer.Formatting = Formatting.Indented;
105-
serializer.WriteObject(writer, propertyBagContainer);
106-
writer.Flush();
107-
xmlString = sw.ToString();
108-
}
109-
}
110-
finally
111-
{
112-
if (sw != null)
113-
{
114-
sw.Close();
115-
}
116-
}
117-
118-
return xmlString;
119-
}
120-
121-
/// <summary>
122-
/// Deserialize the string to the propertyBagContainer.
123-
/// </summary>
124-
/// <param name="xmlString">Serialized string</param>
125-
/// <returns>Deserialized object</returns>
126-
public static T Deserialize(string xmlString)
127-
{
128-
T propertyBagContainer;
129-
using (Stream stream = new MemoryStream())
130-
{
131-
byte[] data = System.Text.Encoding.UTF8.GetBytes(xmlString);
132-
stream.Write(data, 0, data.Length);
133-
stream.Position = 0;
134-
DataContractSerializer deserializer = new DataContractSerializer(typeof(T));
135-
propertyBagContainer = (T)deserializer.ReadObject(stream);
136-
}
137-
138-
return propertyBagContainer;
139-
}
140-
}
141-
14245
/// <summary>
14346
/// Create network mapping input.
14447
/// </summary>

0 commit comments

Comments
 (0)