Documentation Coverage Report
Current view: top level - Cqrs.EventStore - EventStoreConnectionHelper.cs Hit Total Coverage
Version: 2.2 Artefacts: 6 6 100.0 %
Date: 2018-08-07 15:04:50

          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.Net;
      13             : using System.Threading.Tasks;
      14             : using Cqrs.Configuration;
      15             : using EventStore.ClientAPI;
      16             : 
      17             : namespace Cqrs.EventStore
      18             : {
      19             :         /// <summary>
      20             :         /// Creates instances of <see cref="IEventStoreConnection"/>.
      21             :         /// </summary>
      22             :         /// <typeparam name="TAuthenticationToken">The <see cref="Type"/> of the authentication token.</typeparam>
      23             :         public class EventStoreConnectionHelper<TAuthenticationToken> : IEventStoreConnectionHelper
      24           1 :         {
      25             :                 /// <summary>
      26             :                 /// The <see cref="IEventBuilder{TAuthenticationToken}"/> that is used.
      27             :                 /// </summary>
      28             :                 protected IEventBuilder<TAuthenticationToken> EventBuilder { get; private set; }
      29             : 
      30             :                 /// <summary>
      31             :                 /// The <see cref="IConfigurationManager"/> that is used.
      32             :                 /// </summary>
      33             :                 protected IConfigurationManager ConfigurationManager { get; private set; }
      34             : 
      35             :                 /// <summary>
      36             :                 /// Instantiates a new instance of <see cref="EventStoreConnectionHelper{TAuthenticationToken}"/>
      37             :                 /// </summary>
      38             :                 /// <param name="eventBuilder">The <see cref="IEventBuilder{TAuthenticationToken}"/> that is used.</param>
      39             :                 /// <param name="configurationManager">The <see cref="IConfigurationManager"/> that is used.</param>
      40           1 :                 public EventStoreConnectionHelper(IEventBuilder<TAuthenticationToken> eventBuilder, IConfigurationManager configurationManager)
      41             :                 {
      42             :                         EventBuilder = eventBuilder;
      43             :                         ConfigurationManager = configurationManager;
      44             :                 }
      45             : 
      46             :                 /// <summary>
      47             :                 /// Gets a <see cref="IEventStoreConnection"/>
      48             :                 /// </summary>
      49           1 :                 public virtual IEventStoreConnection GetEventStoreConnection()
      50             :                 {
      51             :                         ConnectionSettings settings = ConnectionSettings.Create();
      52             :                         IPEndPoint endPoint = GetEventStoreIpEndPoint();
      53             :                         IEventStoreConnection connection = EventStoreConnection.Create(settings, endPoint);
      54             :                         Task connecting = connection.ConnectAsync();
      55             :                         connecting.Wait();
      56             : 
      57             :                         EventData connectionEvent = EventBuilder.CreateClientConnectedEvent(GetEventStoreClientName());
      58             :                         Task notify = connection.AppendToStreamAsync(GetEventStoreConnectionLogStreamName(), ExpectedVersion.Any, connectionEvent);
      59             :                         notify.Wait();
      60             : 
      61             :                         return connection;
      62             :                 }
      63             : 
      64             :                 /// <summary>
      65             :                 /// Get the client name from the <see cref="ConfigurationManager"/> that describes the client that will connect to the server.
      66             :                 /// </summary>
      67           1 :                 protected virtual string GetEventStoreClientName()
      68             :                 {
      69             :                         return ConfigurationManager.GetSetting("Cqrs.EventStoreClientName") ?? "Cqrs Default Client";
      70             :                 }
      71             : 
      72             :                 /// <summary>
      73             :                 /// Get the connection stream name from the <see cref="ConfigurationManager"/>.
      74             :                 /// </summary>
      75           1 :                 protected virtual string GetEventStoreConnectionLogStreamName()
      76             :                 {
      77             :                         return ConfigurationManager.GetSetting("Cqrs.EventStoreConnectionLogStreamName") ?? "EventStore Connection Log Stream";
      78             :                 }
      79             : 
      80             :                 /// <summary>
      81             :                 /// Get the IP address of the server from the <see cref="ConfigurationManager"/>.
      82             :                 /// </summary>
      83           1 :                 protected virtual IPEndPoint GetEventStoreIpEndPoint()
      84             :                 {
      85             :                         List<byte> eventStoreIp = (ConfigurationManager.GetSetting("Cqrs.EventStoreIp") ?? "127.0.0.1").Split('.').Select(ipPart => (byte)int.Parse(ipPart)).ToList();
      86             :                         string eventStorePortValue = ConfigurationManager.GetSetting("Cqrs.EventStorePort");
      87             :                         int eventStorePort = 1113;
      88             :                         if (!string.IsNullOrWhiteSpace(eventStorePortValue))
      89             :                                 eventStorePort = int.Parse(eventStorePortValue);
      90             :                         return new IPEndPoint(new IPAddress(new[] { eventStoreIp[0], eventStoreIp[1], eventStoreIp[2], eventStoreIp[3] }), eventStorePort);
      91             :                 }
      92             :         }
      93             : }

Generated by: LCOV version 1.12