Skip to content

Tutorial 0: Step 4.1: An advanced enhancement

cdmdotnet edited this page Apr 17, 2016 · 1 revision

Tutorial 0 Step 4

This is an advanced handling example that extends the fourth step.

Eventing

For anyone who's familiar with event based systems, you make a request, get acknowledgement that the request was received successfully, then wait and see what events occur. They may be events telling you there was an error or events telling you that everything went as expected.

In the example thus far, you'll notice that there was no error handling, in-fact no response was read when making the request to create the order. Depending on the settings you use, the event(s) you are wanting to receive, might arrive after the WCF API responds. The reason for this, is that the WCF API response simply acknowledges that the request to do something was received, not that it has been execute yet.

If you rely on process the events raised by the CQRS platform, then the below is just one possible way to wait and query for the events to see what has happened.

Hooking the WCF application into the demo

Open the OrdersController.cs file in the Northwind.Web.Dashboard\Controllers\OrdersController.cs folder. Locate the Orders_Create method and replace the body of the method with the following content:

		if (ModelState.IsValid)
		{
			order.OrderID = new Random(Guid.NewGuid().GetHashCode()).Next();
			var orderServiceFactory = new HttpOrderServiceChannelFactory();

			IOrderService service = orderServiceFactory.CreateChannel();
			var request2 = new ServiceRequestWithData<ISingleSignOnToken, OrderEntity>
			{
				AuthenticationToken = new SingleSignOnToken(),
				CorrelationId = Guid.NewGuid(),
				Data = new OrderEntity
				{
					OrderId = order.OrderID,
					CustomerId = order.CustomerID,
					EmployeeId = order.EmployeeID,
					OrderDate = order.OrderDate,
					ShipCountry = order.ShipCountry,
					ShipViaId = order.ShipVia,
					ShippedDate = order.ShippedDate,
					ShipName = order.ShipName,
					ShipAddress = order.ShipAddress,
					ShipCity = order.ShipCity,
					ShipPostalCode = order.ShipPostalCode
				}
			};

			// Act
			IServiceResponseWithResultData<OrderEntity> response2 = service.CreateOrder(request2);
			Guid correlationId = response2.CorrelationId;

			var request3 = new ServiceRequestWithData<ISingleSignOnToken, Guid>
			{
				AuthenticationToken = new SingleSignOnToken(),
				Data = correlationId
			};

			IEnumerable<EventData> result;
			int loopCount = 0;

			// We're using an in-process bus so this isn't actually necassary, but it demonstrates how to wait for the command and event bus to complete their processes
			do
			{
				// Wait 0.5 of a second and ask again
				if (loopCount > 0)
					System.Threading.Thread.Sleep(500);

				IServiceResponseWithResultData<IEnumerable<EventData>> response3 = service.GetEventData(request3);
				result = response3.ResultData;
				result = result.Where(x => x.EventType == typeof (OrderCreated).AssemblyQualifiedName);
			} while (!result.Any() && loopCount++ < 10);

		}
		return Json(new[] { order }.ToDataSourceResult(request, ModelState));

Compile and Run

Once compiled, you should now be able to start up the Northwind Dashboard Application. Once running, visit the url /Home/ProductsAndOrders of the Northwind Dashboard Application. At the top of the orders grid is a button Create new order, clicking on that will activate the new order widget, which, when the Update button is clicked, the above new code will be executed, adding a new order.

Clone this wiki locally