zoukankan      html  css  js  c++  java
  • Provicer Pattern In ASP.NET Core

    using System;
    using System.Collections.Generic;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Http;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.DependencyInjection.Extensions;
    using Microsoft.Extensions.Hosting;
    
    namespace Default
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                Host.CreateDefaultBuilder(args)
                    .ConfigureWebHostDefaults(builder =>
                    {
                        builder.ConfigureServices((ctx, svcs) =>
                        {
                            svcs.AddDefault();
                            svcs.AddTransient<IHandler, FooHandler>();
                        }).Configure((ctx, app) =>
                        {
                            app.UseRouting();
                            app.UseEndpoints(endpoints =>
                            {
                                endpoints.MapGet("/", async context =>
                                {
                                    context.RequestServices.GetRequiredService<IService>().Execute();
                                    await context.Response.WriteAsync("Done");
                                });
                            });
                        });
                    })
                    .Build()
                    .Run();
            }
        }
        public interface IHandler
        {
            void Handle();
        }
        public class DefaultHandler : IHandler
        {
            public void Handle()
            {
                Console.WriteLine("DefaultHandler.Handle");
            }
        }
        public interface IHandlerProvider
        {
            IEnumerable<IHandler> GetHandlers();
        }
        public class DefaultHandlerProvider : IHandlerProvider
        {
            private readonly IEnumerable<IHandler> _handlers;
            public DefaultHandlerProvider(IEnumerable<IHandler> handlers)
            {
                _handlers = handlers;
            }
            public IEnumerable<IHandler> GetHandlers()
            {
                return _handlers;
            }
        }
        public interface IService
        {
            void Execute();
        }
        public class DefaultService : IService
        {
            private readonly IHandlerProvider _handlerProvider;
            public DefaultService(IHandlerProvider handlerProvider)
            {
                _handlerProvider = handlerProvider;
            }
            public void Execute()
            {
                var handlers = _handlerProvider.GetHandlers();
                foreach (var handler in handlers)
                {
                    handler.Handle();
                }
            }
        }
        public static class ServiceCollectionExtensions
        {
            public static IServiceCollection AddDefault(this IServiceCollection services)
            {
                services.TryAdd(ServiceDescriptor.Transient<IService, DefaultService>());
                services.TryAdd(ServiceDescriptor.Transient<IHandlerProvider, DefaultHandlerProvider>());
                services.TryAddEnumerable(ServiceDescriptor.Transient<IHandler, DefaultHandler>());
                return services;
            }
        }
        public class FooHandler : IHandler
        {
            public void Handle()
            {
                Console.WriteLine("FooHandler.Handle");
            }
        }
    }
  • 相关阅读:
    中缀表达式转换为后缀表达式
    看4S员工自爆!黑啊,太黑了
    解剖孩子晚上磨牙的6大原因
    2D 3D IMAX 电影座位选择
    蒸鸡蛋羹
    0010 4S店提车注意事项
    2012年北京市车船税基准税额
    火车票预订 电话 和 网站
    远程计算机关机方法
    win7 用户信息丢失
  • 原文地址:https://www.cnblogs.com/xiaowangzhi/p/14067688.html
Copyright © 2011-2022 走看看