Skip to content

Commit

Permalink
adding more methods
Browse files Browse the repository at this point in the history
  • Loading branch information
abdullin committed Aug 28, 2012
1 parent df03162 commit 86d2af3
Showing 1 changed file with 68 additions and 3 deletions.
71 changes: 68 additions & 3 deletions E004-event-sourcing-basics/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace E003_event_sourcing_basics
Expand Down Expand Up @@ -101,7 +102,8 @@ public class FactoryImplementation3


List<string> _ourListOfEmployeeNames = new List<string>();

IDictionary<string,int> _ourListOfCarParts = new Dictionary<string, int>();
List<CarPart[]> _shipmentsWaitingToBeUnloaded = new List<CarPart[]>();

public void AssignEmployeeToFactory(string employeeName)
{
Expand Down Expand Up @@ -129,13 +131,36 @@ public void AssignEmployeeToFactory(string employeeName)
});
}

public void TransferShipmentToCargoBay(string shipmentName, CarPart[] parts)
{
Print("?> Command: transfer shipment to cargo bay");
if (_ourListOfEmployeeNames.Count == 0)
{
Fail(":> There has to be somebody at factory in order to accept shipment");
return;
}

if (_shipmentsWaitingToBeUnloaded.Count> 2)
{
Fail(":> More than two shipments can't fit into this cargo bay :(");
return;
}
RecordThat(new ShipmentTransferredToCargoBay()
{
ShipmentName = shipmentName,
CarParts = parts
});
}


void DoPaperWork(string workName)
{
Print(" > Work: papers... {0}... ", workName);
Thread.Sleep(2000);

Thread.Sleep(1000);
}
void DoRealWork(string workName)
{
Print(" > Work: heavy stuff... {0}...", workName);
}
void RecordThat(IEvent e)
{
Expand All @@ -151,10 +176,16 @@ void RecordThat(IEvent e)
}



// announcements inside the factory
void AnnounceInsideFactory(EmployeeAssignedToFactory e)
{
_ourListOfEmployeeNames.Add(e.EmployeeName);
}
void AnnounceInsideFactory(ShipmentTransferredToCargoBay e)
{
_shipmentsWaitingToBeUnloaded.Add(e.CarParts);
}
}

public class EmployeeAssignedToFactory : IEvent
Expand All @@ -166,16 +197,45 @@ public override string ToString()
return string.Format("new worker joins our forces: '{0}'", EmployeeName);
}
}
public class ShipmentTransferredToCargoBay : IEvent
{
public string ShipmentName;
public CarPart[] CarParts;

public override string ToString()
{
var builder = new StringBuilder();
builder.AppendFormat("Shipment '{0}' transferred to cargo bay:", ShipmentName).AppendLine();
foreach (var carPart in CarParts)
{
builder.AppendFormat(" {0} {1} pcs", carPart.Name, carPart.Quantity).AppendLine();
}
return builder.ToString();
}
}


// let's run this implementation
public static void RunFactoryImplementation3()
{
var factory = new FactoryImplementation3();

factory.TransferShipmentToCargoBay("chassis", new CarPart[]
{
new CarPart("chassis", 4),
});

factory.AssignEmployeeToFactory("yoda");
factory.AssignEmployeeToFactory("luke");
factory.AssignEmployeeToFactory("yoda");
factory.AssignEmployeeToFactory("bender");

factory.TransferShipmentToCargoBay("model T spare parts", new[]
{
new CarPart("wheels", 20),
new CarPart("engine", 7),
new CarPart("bits and pieces", 2)
});
}


Expand Down Expand Up @@ -232,5 +292,10 @@ public sealed class CarPart
{
public string Name;
public int Quantity;
public CarPart(string name, int quantity)
{
Name = name;
Quantity = quantity;
}
}
}

0 comments on commit 86d2af3

Please sign in to comment.