学海无涯

导航

配置文件Configuration依赖注入

 

集中管理依赖注入

using AnotherWayToImplementRedis.Interfaces;
using AnotherWayToImplementRedis.Repositories;

namespace AnotherWayToImplementRedis.Configuration
{
    public static class DependencyInjectionSettings
    {
        public static void DependencyInjection(this IServiceCollection services)
        {
            services.AddScoped<IUserRepository, UserRepository>();
        }
    }
}

  Program.cs中添加调用

builder.Services.DependencyInjection();
builder.Services.ServiceExtensionSettings();

  

using System.Text.Json.Serialization;

namespace AnotherWayToImplementRedis.Configuration
{
    public static class ServiceExtension
    {
        public static void ServiceExtensionSettings(this IServiceCollection services)
        {
            services.AddControllers()
                    .AddJsonOptions(options =>
                    {
                        options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
                        options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault;
                    });
        }
    }
}

  

 

posted on 2022-09-15 15:33  宁静致远.  阅读(129)  评论(0编辑  收藏  举报