Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/mediator/Mediator.Step0/ILoggingService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

public interface ILoggingService
{
void Log(string message );
}
12 changes: 12 additions & 0 deletions src/mediator/Mediator.Step0/IMetricService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

namespace Mediator;

public interface IMetricService
{
void ItemDistributed( IWarehouse warehouse, Item item, IStore store );
void ItemReceived( IStore store, Item item );
void ItemReturned( IStore store, Item item, IWarehouse warehouse );

void Print();
}
14 changes: 14 additions & 0 deletions src/mediator/Mediator.Step0/IStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

namespace Mediator;

public interface IStore
{
string Name { get; }

IReadOnlyList<Item> Items { get; }

void ReceiveItem(Item item);

void ReturnItem(Item item);
}
12 changes: 12 additions & 0 deletions src/mediator/Mediator.Step0/IWarehouse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

namespace Mediator;

public interface IWarehouse
{
void AddStore(IStore store );

void DistributeItem(Item item);

void AcceptReturnedItem(Item item, IStore store);
}
8 changes: 8 additions & 0 deletions src/mediator/Mediator.Step0/Item.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

namespace Mediator;

public class Item
{
public string? Kind { get; init; }
}
9 changes: 9 additions & 0 deletions src/mediator/Mediator.Step0/LoggingService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

internal class LoggingService : ILoggingService
{
public void Log(string message)
{
Console.WriteLine(message);
}
}
15 changes: 15 additions & 0 deletions src/mediator/Mediator.Step0/Mediator.Step0.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Mediator</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
</ItemGroup>

</Project>
30 changes: 30 additions & 0 deletions src/mediator/Mediator.Step0/MetricService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

namespace Mediator;

internal class MetricService : IMetricService
{
private int _distributedItems;
private int _returnedItems;
private int _receivedItems;

public void ItemDistributed( IWarehouse warehouse, Item item, IStore store )
{
this._distributedItems++;
}

public void ItemReceived( IStore store, Item item )
{
this._receivedItems++;
}

public void ItemReturned( IStore store, Item item, IWarehouse warehouse )
{
this._returnedItems++;
}

public void Print()
{
Console.WriteLine( $"Distributed: {this._distributedItems}, Received: {this._receivedItems}, Returned: {this._returnedItems}" );
}
}
33 changes: 33 additions & 0 deletions src/mediator/Mediator.Step0/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

using Mediator;
using Microsoft.Extensions.DependencyInjection;

// Initialize services.
var services = new ServiceCollection();

services.AddSingleton<IWarehouse, Warehouse>();
services.AddSingleton<IMetricService, MetricService>();
services.AddSingleton<ILoggingService, LoggingService>();

var serviceProvider = services.BuildServiceProvider();

// Create instances of the classes.
var w = serviceProvider.GetRequiredService<IWarehouse>();
var s1 = ActivatorUtilities.CreateInstance<Store>( serviceProvider, "store1" );
var s2 = ActivatorUtilities.CreateInstance<Store>( serviceProvider, "store2" );

var item1 = new Item { Kind = "item1" };
var item2 = new Item { Kind = "item2" };

// Add stores.
w.AddStore( s1 );
w.AddStore( s2 );

// Run the scenario.
w.DistributeItem( item1 );
w.DistributeItem( item2 );
s1.ReturnItem( s1.Items[0] );

// Print metrics.
serviceProvider.GetRequiredService<IMetricService>().Print();
37 changes: 37 additions & 0 deletions src/mediator/Mediator.Step0/Store.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

namespace Mediator;

internal class Store : IStore
{
private readonly IWarehouse _warehouse;
private readonly IMetricService _metricService;
private readonly ILoggingService _loggingService;
private readonly List<Item> _items;

public string Name { get; }
public IReadOnlyList<Item> Items => this._items;

public Store(string name, IWarehouse warehouse, IMetricService metricService, ILoggingService loggingService )
{
this.Name = name;
this._warehouse = warehouse;
this._metricService = metricService;
this._loggingService = loggingService;
this._items = new List<Item>();
}

public void ReceiveItem(Item item)
{
this._items.Add( item );
this._metricService.ItemReceived( this, item );
this._loggingService.Log( $"{this.Name} received {item.Kind}." );
}

public void ReturnItem(Item item)
{
this._items.Remove( item );
this._loggingService.Log( $"{this.Name} returned {item.Kind}." );
this._warehouse.AcceptReturnedItem( item, this );
}
}
49 changes: 49 additions & 0 deletions src/mediator/Mediator.Step0/Warehouse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

namespace Mediator;

internal class Warehouse : IWarehouse
{
private int _nextStore;
private readonly List<IStore> _stores;
private readonly IMetricService _metricService;
private readonly ILoggingService _loggingService;

public Warehouse( IMetricService metricService, ILoggingService loggingService )
{
this._stores = new List<IStore>();
this._metricService = metricService;
this._loggingService = loggingService;
}

public void DistributeItem( Item item )
{
var store = this._stores[this._nextStore];
this._loggingService.Log( $"Distributed {item.Kind} to {store.Name}." );
store.ReceiveItem(item);
this._metricService.ItemDistributed( this, item, store );
this.ToNextStore();
}

public void AddStore( IStore store )
{
this._stores.Add( store );
}

public void AcceptReturnedItem( Item item, IStore store )
{
this._metricService.ItemReturned( store, item, this );

if ( this._stores[this._nextStore] == store)
{
this.ToNextStore();
}

this.DistributeItem( item );
}

private void ToNextStore()
{
this._nextStore = (this._nextStore + 1) % this._stores.Count;
}
}
50 changes: 50 additions & 0 deletions src/mediator/Mediator.Step1/DistributionMediator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

namespace Mediator;

public class DistributionMediator : IDistributionMediator
{
private int _nextStore;
private readonly Lazy<IWarehouse> _warehouse;
private readonly List<IStore> _stores = new();
private readonly IMetricService _metricService;
private readonly ILoggingService _loggingService;

public DistributionMediator( Lazy<IWarehouse> warehouse, IMetricService metricService, ILoggingService loggingService )
{
this._warehouse = warehouse;
this._metricService = metricService;
this._loggingService = loggingService;
}

public void Distribute( Item item )
{
var store = this._stores[this._nextStore];
this._metricService.ItemDistributed( this._warehouse.Value, item, store );
this._loggingService.Log( $"Distributed {item.Kind} to {store.Name}." );
store.ReceiveItem( item );
this._loggingService.Log( $"{store.Name} received {item.Kind}." );
this._metricService.ItemReceived( store, item );
this.ToNextStore();
}

public void AddStore( IStore store )
{
this._stores.Add( store );
}

public void Redistribute( Item item, IStore store )
{
if ( this._stores[this._nextStore] == store )
{
this.ToNextStore();
}

this.Distribute( item );
}

private void ToNextStore()
{
this._nextStore = (this._nextStore + 1) % this._stores.Count;
}
}
10 changes: 10 additions & 0 deletions src/mediator/Mediator.Step1/IDistributionMediator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

namespace Mediator;

public interface IDistributionMediator
{
void Distribute( Item item );
void AddStore( IStore store );
void Redistribute( Item item, IStore store );
}
6 changes: 6 additions & 0 deletions src/mediator/Mediator.Step1/ILoggingService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

public interface ILoggingService
{
void Log(string message );
}
12 changes: 12 additions & 0 deletions src/mediator/Mediator.Step1/IMetricService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

namespace Mediator;

public interface IMetricService
{
void ItemDistributed( IWarehouse warehouse, Item item, IStore store );
void ItemReceived( IStore store, Item item );
void ItemReturned( IStore store, Item item, IWarehouse warehouse );

void Print();
}
8 changes: 8 additions & 0 deletions src/mediator/Mediator.Step1/IReturnMediator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

namespace Mediator;

public interface IReturnMediator
{
void ReturnItem( Item item, IStore store );
}
14 changes: 14 additions & 0 deletions src/mediator/Mediator.Step1/IStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

namespace Mediator;

public interface IStore
{
string Name { get; }

IReadOnlyList<Item> Items { get; }

void ReceiveItem(Item item);

void ReturnItem(Item item);
}
10 changes: 10 additions & 0 deletions src/mediator/Mediator.Step1/IWarehouse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

namespace Mediator;

public interface IWarehouse
{
void DistributeItem(Item item);

void AcceptReturnedItem(Item item, IStore store);
}
8 changes: 8 additions & 0 deletions src/mediator/Mediator.Step1/Item.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

namespace Mediator;

public class Item
{
public string? Kind { get; init; }
}
9 changes: 9 additions & 0 deletions src/mediator/Mediator.Step1/LoggingService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.

internal class LoggingService : ILoggingService
{
public void Log(string message)
{
Console.WriteLine(message);
}
}
15 changes: 15 additions & 0 deletions src/mediator/Mediator.Step1/Mediator.Step1.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Mediator</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
</ItemGroup>

</Project>
Loading