public static MyServiceCollection AddMyMediator(this MyServiceCollection services, MyServiceLifetime lifetime, params Type[] markers) { var handlersInfo = new Dictionary <Type, Type>(); foreach (Type marker in markers) { var assembly = marker.Assembly; var requests = GetClassesImplementingInterface(assembly, typeof(IRequest <>)); var handlers = GetClassesImplementingInterface(assembly, typeof(IRequestHandler <,>)); foreach (Type request in requests) { Type requestHandler = handlers.SingleOrDefault(h => request == h.GetInterfaces().First().GetGenericArguments().First()); handlersInfo.Add(request, requestHandler); } var serviceDescriptor = handlers.Select(x => new MyServiceDescriptor(x, x, lifetime)); services.AddServiceDescriptor(serviceDescriptor); } services.RegisterSingleton <IMediator>(x => new MyMediator(x.GetService, handlersInfo)); return(services); }
static async Task Main() { // 1. Manual registration //var requestHandlersMappings = new Dictionary<Type, Type> //{ // {typeof(MyRequest), typeof(MyRequestRequestHandler)}, // {typeof(MyAgeRequest), typeof(MyAgeRequestRequestHandler)} //}; //var notificationHandlersMappings = new Dictionary<Type, List<Type>> //{ // {typeof(MyNotification), new List<Type>(2) {typeof(MyNotificationHandler1), typeof(MyNotificationHandler2) }}, //}; //var registrations = new Dictionary<Type, Func<object>> //{ // {typeof(MyRequestRequestHandler), () => new MyRequestRequestHandler()}, // {typeof(MyAgeRequestRequestHandler), () => new MyAgeRequestRequestHandler()}, // {typeof(MyNotificationHandler1), () => new MyNotificationHandler1()}, // {typeof(MyNotificationHandler2), () => new MyNotificationHandler2()}, //}; //IMediator mediator = new MyMediator(requestHandlersMappings, notificationHandlersMappings, registrations); //await mediator.Send(new MyRequest("Hello World!")); //await mediator.Send(new MyAgeRequest(28)); //await mediator.Publish(new MyNotification("notification")); // 2. Dynamic registration with Microsoft DI //var serviceProvider = new ServiceCollection().AddMyMediator(ServiceLifetime.Scoped, typeof(Program)) // .BuildServiceProvider(); //var mediator = serviceProvider.GetService<IMediator>(); //await mediator.Send(new MyRequest("Hello World!")); //await mediator.Send(new MyAgeRequest(28)); // 3.Dynamic registration with My DI var myServiceProvider = new MyServiceCollection().AddMyMediator(MyServiceLifetime.Transient, typeof(Program)) .BuildContainer(); var mediator = myServiceProvider.GetService <IMediator>(); await mediator.Send(new MyRequest("Hello World!")); await mediator.Send(new MyAgeRequest(28)); }
public static int Main(string[] args) { string value = "".PadRightC(10, 'F') .PadRightC(10, 'F') .PadRightC(10, 'F'); IServiceCollection services = null; services.AddSingleton<Temp>(); services.AddSingletonMany<ITemp, Temp>(1); services.AddSpecialSingleton<Temp>(); services.AddSpecialSingleton2<Temp>(1); ServiceCollection serviceColection = null; serviceColection.AddSingleton<Temp>(); serviceColection.AddSingletonMany<ITemp, Temp>(1); serviceColection.AddSpecialSingleton<Temp>(); serviceColection.AddSpecialSingleton2<Temp>(1); MyServiceCollection myServiceColection = null; myServiceColection.AddSingleton<Temp>(); myServiceColection.AddSingletonMany<ITemp, Temp>(1); myServiceColection.AddSpecialSingleton<Temp>(); myServiceColection.AddSpecialSingleton2<Temp>(1); MyOtherServiceCollection myOtherServiceColection = null; myOtherServiceColection.AddSingleton<Temp>(); myOtherServiceColection.AddSingletonMany<ITemp, Temp>(1); myOtherServiceColection.AddSpecialSingleton<Temp>(); myOtherServiceColection.AddSpecialSingleton2<Temp>(1); context.Context.Context.Context.Context.Services.GetService<int>(); object value2 = context.Context.Services.GetService(typeof(int)); context.Context.Services.GetService(typeof(int)); int i = Resolve().GetService<int>().Add(1); ITemp temp = Resolve().GetService<Temp>().MyAwesomeExtension(); ITemp temp2 = Resolve().GetT3<int, bool, Temp>() .MyAwesomeExtension() .MyAwesomeExtension(); 0.IsNumber(MyNumbers.One); 1.IsNumber2(MyConstantNumbers.ConstantOne); 1.IsNumber2(MyConstantNumbers.ConstantTwo); return 0.Increment(); }
static void Main(string[] args) { //var mySimpleContainer = new MySimpleContainer(); //mySimpleContainer.Register<IRepository, Repository>(new Repository()); //mySimpleContainer.Resolve<IRepository>(); var myServiceCollection = new MyServiceCollection(); myServiceCollection.RegisterTransient <IEmailSender, SmtpEmailSender>(); myServiceCollection.RegisterSingleton <IRepository, Repository>(); myServiceCollection.RegisterTransient <LoginController>(); var myContainer = myServiceCollection.BuildContainer(); var hash1 = myContainer.GetService <IRepository>().GetHashCode(); var hash2 = myContainer.GetService <IRepository>().GetHashCode(); var loginController = myContainer.GetService <LoginController>(); }