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.ServiceModel;
12 : using cdmdotnet.Logging;
13 : using Cqrs.Authentication;
14 : using Cqrs.Events;
15 : using Cqrs.Messages;
16 :
17 : namespace Cqrs.Services
18 : {
19 : /// <summary>
20 : /// A WCF <see cref="ServiceContractAttribute">ServiceContract</see> that provides read-only access to <see cref="IEventStore{TAuthenticationToken}"/> <see cref="IEvent{TAuthenticationToken}">events</see>
21 : /// raised with the same <see cref="IMessage.CorrelationId"/>.
22 : /// </summary>
23 : /// <typeparam name="TAuthenticationToken">The <see cref="Type"/> of authentication token.</typeparam>
24 : public abstract class EventService<TAuthenticationToken> : IEventService<TAuthenticationToken>
25 1 : {
26 : /// <summary>
27 : /// Instantiates a new instance of <see cref="EventService{TAuthenticationToken}"/>.
28 : /// </summary>
29 1 : protected EventService(IEventStore<TAuthenticationToken> eventStore, ILogger logger, ICorrelationIdHelper correlationIdHelper, IAuthenticationTokenHelper<TAuthenticationToken> authenticationTokenHelper)
30 : {
31 : EventStore = eventStore;
32 : Logger = logger;
33 : CorrelationIdHelper = correlationIdHelper;
34 : AuthenticationTokenHelper = authenticationTokenHelper;
35 : }
36 :
37 : /// <summary>
38 : /// Gets or sets the <see cref="IEventStore{TAuthenticationToken}"/>.
39 : /// </summary>
40 : protected virtual IEventStore<TAuthenticationToken> EventStore { get; private set; }
41 :
42 : /// <summary>
43 : /// Gets or sets the <see cref="IAuthenticationTokenHelper{TAuthenticationToken}"/>.
44 : /// </summary>
45 : protected IAuthenticationTokenHelper<TAuthenticationToken> AuthenticationTokenHelper { get; private set; }
46 :
47 : /// <summary>
48 : /// Gets or sets the <see cref="ILogger"/>.
49 : /// </summary>
50 : protected ICorrelationIdHelper CorrelationIdHelper { get; private set; }
51 :
52 : /// <summary>
53 : /// Gets or sets the <see cref="ILogger"/>.
54 : /// </summary>
55 : protected ILogger Logger { get; private set; }
56 :
57 : /// <summary>
58 : /// Get all <see cref="IEvent{TAuthenticationToken}">events</see>
59 : /// raised with the same <see cref="IMessage.CorrelationId"/>.
60 : /// </summary>
61 : /// <param name="serviceRequest">The <see cref="IMessage.CorrelationId"/> of the <see cref="IEvent{TAuthenticationToken}">events</see> to find.</param>
62 1 : public virtual IServiceResponseWithResultData<IEnumerable<EventData>> GetEventData(IServiceRequestWithData<TAuthenticationToken, Guid> serviceRequest)
63 : {
64 : AuthenticationTokenHelper.SetAuthenticationToken(serviceRequest.AuthenticationToken);
65 : CorrelationIdHelper.SetCorrelationId(serviceRequest.CorrelationId);
66 :
67 : OnGetEventData(serviceRequest);
68 : IEnumerable<EventData> results = EventStore.Get(serviceRequest.Data);
69 : results = OnGotEventData(serviceRequest, results);
70 :
71 : return CompleteResponse
72 : (
73 : new ServiceResponseWithResultData<IEnumerable<EventData>>
74 : {
75 : State = ServiceResponseStateType.Succeeded,
76 : ResultData = results,
77 : }
78 : );
79 : }
80 :
81 : /// <summary>
82 : /// Executed before calling the <see cref="IEventStore{TAuthenticationToken}.Get(System.Type,System.Guid,bool,int)"/> method on <see cref="EventStore"/>
83 : /// in <see cref="GetEventData"/>.
84 : /// </summary>
85 : /// <param name="serviceRequest">The original <see cref="IServiceRequestWithData{TAuthenticationToken,Guid}"/>.</param>
86 1 : protected virtual void OnGetEventData(IServiceRequestWithData<TAuthenticationToken, Guid> serviceRequest) { }
87 :
88 : /// <summary>
89 : /// Executed after calling the <see cref="IEventStore{TAuthenticationToken}.Get(System.Type,System.Guid,bool,int)"/> method on <see cref="EventStore"/>
90 : /// in <see cref="GetEventData"/>.
91 : /// </summary>
92 : /// <param name="serviceRequest">The original <see cref="IServiceRequestWithData{TAuthenticationToken,Guid}"/>.</param>
93 : /// <param name="results">The collection of <see cref="IEvent{TAuthenticationToken}">events</see> from the <see cref="EventStore"/>.</param>
94 1 : protected virtual IEnumerable<EventData> OnGotEventData(IServiceRequestWithData<TAuthenticationToken, Guid> serviceRequest, IEnumerable<EventData> results)
95 : {
96 : return results;
97 : }
98 :
99 : /// <summary>
100 : /// Sets the <see cref="IServiceResponse.CorrelationId"/> on the provided <paramref name="serviceResponse"/>.
101 : /// </summary>
102 1 : protected virtual TServiceResponse CompleteResponse<TServiceResponse>(TServiceResponse serviceResponse)
103 : where TServiceResponse : IServiceResponse
104 : {
105 : serviceResponse.CorrelationId = CorrelationIdHelper.GetCorrelationId();
106 : return serviceResponse;
107 : }
108 : }
109 : }
|