Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Unity Container with MediatR
public static void RegisterTypes(IUnityContainer container)
{
container.RegisterType<IRepository>(new InjectionFactory(context => new EventStoreRepository(context.Resolve<IStoreEvents>(), new AggregateFactory(), new ConflictDetector())));
container.RegisterType<IMediator>(new InjectionFactory(x => new Mediator(() => new UnityServiceLocator(x))));
container.RegisterTypes(AllClasses.FromAssemblies(typeof(Commands.InventoryCommandHandler).Assembly), WithMappings.FromAllInterfaces, GetName, GetLifetimeManager);
container.RegisterTypes(AllClasses.FromAssemblies(typeof(Queries.InventoryQueryHandler).Assembly), WithMappings.FromAllInterfaces, GetName, GetLifetimeManager);
container.RegisterType<IStoreEvents>(new InjectionFactory(context => Wireup.Init()
.UsingSqlPersistence("EventStore")
.WithDialect(new MySqlDialect())
.EnlistInAmbientTransaction()
.InitializeStorageEngine()
.UsingJsonSerialization()
.UsingSynchronousDispatchScheduler().DispatchTo(new DelegateMessageDispatcher(commit => {
var mediator = context.Resolve<IMediator>();
foreach (var evnt in commit.Events)
{
mediator.PublishAsync(evnt.Body as dynamic);
}
}))
.Build()));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment