Skip to content

Instantly share code, notes, and snippets.

@syed-afraz-ali
Last active December 9, 2016 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save syed-afraz-ali/367a53604b03cfd073fe to your computer and use it in GitHub Desktop.
Save syed-afraz-ali/367a53604b03cfd073fe to your computer and use it in GitHub Desktop.
Simple CQRS using MediatR
using System;
using MediatR;
namespace MediatR_101
{
// The Query GetWeather that we will use to get weather status of any city
public class GetWeather : IRequest<Weather>
{
public GetWeather(String city)
{
City = city;
}
public String City { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using MediatR;
using StructureMap;
using StructureMap.Graph;
namespace MediatR_101
{
// Initializes Structuremap as the default DI container and sets up MediatR
public class IoC
{
public static IContainer GetContainer()
{
return new Container(cfg =>
{
cfg.Scan(scanner =>
{
scanner.TheCallingAssembly();
scanner.AssemblyContainingType<IMediator>();
scanner.WithDefaultConventions();
scanner.ConnectImplementationsToTypesClosing(typeof(IRequestHandler<,>));
scanner.ConnectImplementationsToTypesClosing(typeof(IAsyncRequestHandler<,>));
scanner.ConnectImplementationsToTypesClosing(typeof(INotificationHandler<>));
scanner.ConnectImplementationsToTypesClosing(typeof(IAsyncNotificationHandler<>));
});
cfg.For<SingleInstanceFactory>().Use<SingleInstanceFactory>(ctx => t => ctx.GetInstance(t));
cfg.For<MultiInstanceFactory>().Use<MultiInstanceFactory>(ctx => t => ctx.GetAllInstances(t));
});
}
}
}
using System;
using MediatR;
using StructureMap;
namespace MediatR_101
{
class Program
{
private static IContainer _container;
static void Main(string[] args)
{
_container = IoC.GetContainer();
//-------------------------------------------
// Example of using MediatR to Query for data
//-------------------------------------------
// Get an instance of MediatR from DI Container
var mediatR = _container.GetInstance<IMediator>();
// Send a Query to get Weather Status for a city
var weather = mediatR.Send(new GetWeather("Kuala Lumpur"));
Console.WriteLine(weather.Summary);
//-------------------------------------------
// Example of using MediatR to Send
// one way 'Command' or 'Message'
//-------------------------------------------
// Send a Command to Update Weather of Kuala Lumpur to 25 C
mediatR.Send(new UpdateWeather("Kuala Lumpur", 25));
Console.ReadLine();
}
}
}
using System;
using MediatR;
namespace MediatR_101
{
// This class automatically handles the command for UpdateWeather sent to MediatR
// In real world project this class can be a Service, repository or any other componenet that will be interested to a particular message or command
public class UpdateHandler : RequestHandler<UpdateWeather>
{
protected override void HandleCore(UpdateWeather message)
{
Console.WriteLine("Weather for {0} was updated and temperature set to {1}", message.City,
message.Temperature);
}
}
}
using System;
using MediatR;
namespace MediatR_101
{
// Update Weather Command, this is a one way Command and we will not get any response against it
public class UpdateWeather : IRequest
{
public UpdateWeather(String city, Int32 temperature)
{
City = city;
Temperature = temperature;
}
public String City { get; set; }
public Int32 Temperature { get; set; }
}
}
using System;
namespace MediatR_101
{
// Weather Message that will be returned when some one queries for it
public class Weather
{
public String Summary { get; set; }
}
}
using System;
using MediatR;
namespace MediatR_101
{
// This class automatically handles the request for GetWeather query is sent to MediatR
// In real world project this class can be a Service, repository or any other componenet that will respond to a particular query
public class WeatherHandler : IRequestHandler<GetWeather, Weather>
{
public Weather Handle(GetWeather query)
{
return new Weather()
{
Summary = String.Format("Forecast for {0}, Partially cloudy.", query.City)
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment