Skip to content

Commit

Permalink
Moved to file-scoped namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed Dec 8, 2021
1 parent 5d0f0c6 commit e085e7f
Show file tree
Hide file tree
Showing 17 changed files with 704 additions and 721 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
using System;
using System.Threading.Tasks;

namespace CryptoShredding.IntegrationTests.TestSupport
namespace CryptoShredding.IntegrationTests.TestSupport;

public abstract class Given_WhenAsync_Then_Test
: IDisposable
{
public abstract class Given_WhenAsync_Then_Test
: IDisposable
protected Given_WhenAsync_Then_Test()
{
protected Given_WhenAsync_Then_Test()
{
Task.Run((Func<Task>) (async () => await this.SetupAsync())).Wait();
}
Task.Run((Func<Task>) (async () => await this.SetupAsync())).Wait();
}

private async Task SetupAsync()
{
await Given();
await When();
}
private async Task SetupAsync()
{
await Given();
await When();
}

protected abstract Task Given();
protected abstract Task Given();

protected abstract Task When();
protected abstract Task When();

public void Dispose()
{
Cleanup();
}
public void Dispose()
{
Cleanup();
}

protected virtual void Cleanup()
{
}
protected virtual void Cleanup()
{
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;

namespace CryptoShredding.Attributes
{
/**
namespace CryptoShredding.Attributes;

/**
* Specifies the PII owner (e.g: the person Id)
*/
public class DataSubjectIdAttribute
: Attribute
{
}
public class DataSubjectIdAttribute
: Attribute
{
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;

namespace CryptoShredding.Attributes
{
/**
namespace CryptoShredding.Attributes;

/**
* Specifies the property that holds PII
*/
public class PersonalDataAttribute
: Attribute
{
}
public class PersonalDataAttribute
: Attribute
{
}
7 changes: 3 additions & 4 deletions Crypto_Shredding/.NET/src/CryptoShredding/Contracts/IEvent.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace CryptoShredding.Contracts
namespace CryptoShredding.Contracts;

public interface IEvent
{
public interface IEvent
{
}
}
53 changes: 26 additions & 27 deletions Crypto_Shredding/.NET/src/CryptoShredding/EventConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,35 @@
using CryptoShredding.Serialization;
using EventStore.Client;

namespace CryptoShredding
namespace CryptoShredding;

public class EventConverter
{
public class EventConverter
{
private readonly JsonSerializer _jsonSerializer;
private readonly JsonSerializer _jsonSerializer;

public EventConverter(JsonSerializer jsonSerializer)
{
_jsonSerializer = jsonSerializer;
}
public EventConverter(JsonSerializer jsonSerializer)
{
_jsonSerializer = jsonSerializer;
}

public IEvent ToEvent(ResolvedEvent resolvedEvent)
{
var data = resolvedEvent.Event.Data;
var metadata = resolvedEvent.Event.Metadata;
var eventName = resolvedEvent.Event.EventType;
var persistableEvent = _jsonSerializer.Deserialize(data, metadata, eventName);
return persistableEvent;
}
public IEvent ToEvent(ResolvedEvent resolvedEvent)
{
var data = resolvedEvent.Event.Data;
var metadata = resolvedEvent.Event.Metadata;
var eventName = resolvedEvent.Event.EventType;
var persistableEvent = _jsonSerializer.Deserialize(data, metadata, eventName);
return persistableEvent;
}

public EventData ToEventData(IEvent @event)
{
var eventTypeName = @event.GetType().Name;
var id = Uuid.NewUuid();
var serializedEvent = _jsonSerializer.Serialize(@event);
var contentType = serializedEvent.IsJson ? "application/json" : "application/octet-stream";
var data = serializedEvent.Data;
var metadata = serializedEvent.MetaData;
var eventData = new EventData(id, eventTypeName,data, metadata, contentType);
return eventData;
}
public EventData ToEventData(IEvent @event)
{
var eventTypeName = @event.GetType().Name;
var id = Uuid.NewUuid();
var serializedEvent = _jsonSerializer.Serialize(@event);
var contentType = serializedEvent.IsJson ? "application/json" : "application/octet-stream";
var data = serializedEvent.Data;
var metadata = serializedEvent.MetaData;
var eventData = new EventData(id, eventTypeName,data, metadata, contentType);
return eventData;
}
}
93 changes: 46 additions & 47 deletions Crypto_Shredding/.NET/src/CryptoShredding/EventStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,58 @@
using CryptoShredding.Contracts;
using EventStore.Client;

namespace CryptoShredding
namespace CryptoShredding;

public class EventStore
{
public class EventStore
{
private readonly EventStoreClient _eventStoreClient;
private readonly EventConverter _eventConverter;
private readonly EventStoreClient _eventStoreClient;
private readonly EventConverter _eventConverter;

public EventStore(
EventStoreClient eventStoreClient,
EventConverter eventConverter)
{
_eventStoreClient = eventStoreClient;
_eventConverter = eventConverter;
}
public EventStore(
EventStoreClient eventStoreClient,
EventConverter eventConverter)
{
_eventStoreClient = eventStoreClient;
_eventConverter = eventConverter;
}

public async Task PersistEvents(string streamName, int aggregateVersion, IEnumerable<IEvent> eventsToPersist)
public async Task PersistEvents(string streamName, int aggregateVersion, IEnumerable<IEvent> eventsToPersist)
{
var events = eventsToPersist.ToList();
var count = events.Count;
if (count == 0)
{
var events = eventsToPersist.ToList();
var count = events.Count;
if (count == 0)
{
return;
}

var expectedRevision = GetExpectedRevision(aggregateVersion, count);
var eventsData =
events.Select(x => _eventConverter.ToEventData(x));
if (expectedRevision == null)
await _eventStoreClient.AppendToStreamAsync(streamName, StreamState.NoStream, eventsData);
else
await _eventStoreClient.AppendToStreamAsync(streamName, expectedRevision.Value, eventsData);
return;
}

public async Task<IEnumerable<IEvent>> GetEvents(string streamName)
{
const int start = 0;
const int count = 4096;
const bool resolveLinkTos = false;
var sliceEvents =
_eventStoreClient.ReadStreamAsync(Direction.Forwards, streamName, start, count, resolveLinkTos: resolveLinkTos);
var resolvedEvents = await sliceEvents.ToListAsync();
var events =
resolvedEvents.Select(x => _eventConverter.ToEvent(x));
return events;
}
var expectedRevision = GetExpectedRevision(aggregateVersion, count);
var eventsData =
events.Select(x => _eventConverter.ToEventData(x));
if (expectedRevision == null)
await _eventStoreClient.AppendToStreamAsync(streamName, StreamState.NoStream, eventsData);
else
await _eventStoreClient.AppendToStreamAsync(streamName, expectedRevision.Value, eventsData);
}

public async Task<IEnumerable<IEvent>> GetEvents(string streamName)
{
const int start = 0;
const int count = 4096;
const bool resolveLinkTos = false;
var sliceEvents =
_eventStoreClient.ReadStreamAsync(Direction.Forwards, streamName, start, count, resolveLinkTos: resolveLinkTos);
var resolvedEvents = await sliceEvents.ToListAsync();
var events =
resolvedEvents.Select(x => _eventConverter.ToEvent(x));
return events;
}

private StreamRevision? GetExpectedRevision(int aggregateVersion, int numberOfEvents)
{
var originalVersion = aggregateVersion - numberOfEvents;
var expectedVersion = originalVersion != 0
? StreamRevision.FromInt64(originalVersion - 1)
: (StreamRevision?)null;
return expectedVersion;
}
private StreamRevision? GetExpectedRevision(int aggregateVersion, int numberOfEvents)
{
var originalVersion = aggregateVersion - numberOfEvents;
var expectedVersion = originalVersion != 0
? StreamRevision.FromInt64(originalVersion - 1)
: (StreamRevision?)null;
return expectedVersion;
}
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
using System;
using System.Collections.Generic;

namespace CryptoShredding.Repository
namespace CryptoShredding.Repository;

public class CryptoRepository
{
public class CryptoRepository
private readonly IDictionary<string, EncryptionKey> _cryptoStore;

public CryptoRepository()
{
private readonly IDictionary<string, EncryptionKey> _cryptoStore;
_cryptoStore = new Dictionary<string, EncryptionKey>();
}

public CryptoRepository()
public EncryptionKey GetExistingOrNew(string id, Func<EncryptionKey> keyGenerator)
{
var isExisting = _cryptoStore.TryGetValue(id, out var keyStored);
if (isExisting)
{
_cryptoStore = new Dictionary<string, EncryptionKey>();
return keyStored;
}

public EncryptionKey GetExistingOrNew(string id, Func<EncryptionKey> keyGenerator)
{
var isExisting = _cryptoStore.TryGetValue(id, out var keyStored);
if (isExisting)
{
return keyStored;
}

var newEncryptionKey = keyGenerator.Invoke();
_cryptoStore.Add(id, newEncryptionKey);
return newEncryptionKey;
}
var newEncryptionKey = keyGenerator.Invoke();
_cryptoStore.Add(id, newEncryptionKey);
return newEncryptionKey;
}

public EncryptionKey GetExistingOrDefault(string id)
public EncryptionKey GetExistingOrDefault(string id)
{
var isExisting = _cryptoStore.TryGetValue(id, out var keyStored);
if (isExisting)
{
var isExisting = _cryptoStore.TryGetValue(id, out var keyStored);
if (isExisting)
{
return keyStored;
}

return default;
return keyStored;
}

public void DeleteEncryptionKey(string id)
{
_cryptoStore.Remove(id);
}
return default;
}

public void DeleteEncryptionKey(string id)
{
_cryptoStore.Remove(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
namespace CryptoShredding.Repository
namespace CryptoShredding.Repository;

public class EncryptionKey
{
public class EncryptionKey
{
public byte[] Key { get; }
public byte[] Nonce { get; }
public byte[] Key { get; }
public byte[] Nonce { get; }

public EncryptionKey(
byte[] key,
byte[] nonce)
{
Key = key;
Nonce = nonce;
}
public EncryptionKey(
byte[] key,
byte[] nonce)
{
Key = key;
Nonce = nonce;
}
}
Loading

0 comments on commit e085e7f

Please sign in to comment.