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.Text;
11 : using EventStore.ClientAPI;
12 :
13 : namespace Cqrs.EventStore.Bus
14 : {
15 : /// <summary>
16 : /// A collection of utility functions.
17 : /// </summary>
18 : public static class EventStoreUtilities
19 1 : {
20 : /// <summary>
21 : /// Converts the provide <paramref name="position"/> into a string representation of itself.
22 : /// </summary>
23 1 : public static string PositionToFormattedString(Position position)
24 : {
25 : return position.CommitPosition + "/" + position.PreparePosition;
26 : }
27 :
28 : /// <summary>
29 : /// Converts the provided <paramref name="formattedPosition">string representation</paramref> of a <see cref="Position"/> back into a <see cref="Position"/>.
30 : /// </summary>
31 1 : public static Position FormattedStringToPosition(string formattedPosition)
32 : {
33 : Position position = Position.End;
34 :
35 : if (!string.IsNullOrEmpty(formattedPosition))
36 : {
37 : string[] substrings = formattedPosition.Split('/');
38 : long commitPosition, preparePosition;
39 : if (long.TryParse(substrings[0], out commitPosition) &&
40 : long.TryParse(substrings[1], out preparePosition))
41 : {
42 : position = new Position(commitPosition, preparePosition);
43 : }
44 : else
45 : {
46 : throw new InvalidLastEventProcessedException(formattedPosition);
47 : }
48 : }
49 :
50 : return position;
51 : }
52 :
53 : /// <summary>
54 : /// Converts the provided <paramref name="value"/> into a <see cref="byte"/> <see cref="Array"/>.
55 : /// </summary>
56 1 : public static byte[] StringToByteArray(string value)
57 : {
58 : byte[] rawEventData = Encoding.UTF8.GetBytes(value);
59 : return rawEventData;
60 : }
61 :
62 : /// <summary>
63 : /// Converts the provided <paramref name="value">byte array</paramref> into a string.
64 : /// </summary>
65 1 : public static string ByteArrayToString(byte[] value)
66 : {
67 : return Encoding.UTF8.GetString(value);
68 : }
69 : }
70 : }
|