使用微软依赖注入器Microsoft.Extensions.DependencyInjection

目录

    • 注册服务到容器
    • 调用
    • ASP.NET Core MVC调用参考,构造函数注入

注册服务到容器

using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WebNetCore5_Img_Storage.BLL;
using WebNetCore5_Img_Storage.DAL;
using WebNetCore5_Img_Storage.IBLL;
using WebNetCore5_Img_Storage.IDAL;

namespace XUnitTestProject1
{
    /// 
    /// 初始化依赖注入
    /// 
    public class BaseTest
    {
        /// 
        /// 服务集合
        /// 
        public readonly static IServiceCollection services = null;

        /// 
        /// 服务提供
        /// 
        public readonly static ServiceProvider  serviceProvider = null;

        static BaseTest()
        {
            services = new Microsoft.Extensions.DependencyInjection.ServiceCollection();
            //servers.AddScoped();
            //servers.AddScoped();

            try
            {
                //业务层注入  
                string path = AppDomain.CurrentDomain.BaseDirectory;
                Assembly bll_impl = Assembly.LoadFrom(path + "WebNetCore5_Img_Storage.BLL.dll");
                Assembly bll_interface = Assembly.LoadFrom(path + "WebNetCore5_Img_Storage.IBLL.dll");
                //Assembly bll_impl = Assembly.Load("WebNetCore5_Img_Storage.BLL");
                //Assembly bll_interface = Assembly.Load("WebNetCore5_Img_Storage.IBLL");
                var typesInterface = bll_interface.GetTypes();
                var typesImpl = bll_impl.GetTypes();
                foreach (var item in typesInterface)
                {
                    var name = item.Name.Substring(1);
                    string implBLLImpName = name + "Impl";
                    var impl = typesImpl.FirstOrDefault(w => w.Name.Equals(implBLLImpName));
                    if (impl != null)
                    {
                        //services.AddTransient(item, impl);
                        //services.AddSingleton(item, impl);
                        services.AddScoped(item, impl);
                    }
                }

                //数据层注册
                //Assembly dalAssemblys = Assembly.Load("WebNetCore5_Img_Storage.DAL");
                //Assembly dalInterface = Assembly.Load("WebNetCore5_Img_Storage.IDAL");
                Assembly dalAssemblys = Assembly.LoadFrom(path + "WebNetCore5_Img_Storage.DAL.dll");
                Assembly dalInterface = Assembly.LoadFrom(path + "WebNetCore5_Img_Storage.IDAL.dll");
                var dalTypesImpl = dalAssemblys.GetTypes();
                var dalTypesInterface = dalInterface.GetTypes();
                foreach (var item in dalTypesInterface)
                {
                    var name = item.Name.Substring(1);
                    string implDalName = name + "Impl";
                    var impl = dalTypesImpl.FirstOrDefault(w => w.Name.Equals(implDalName));
                    if (impl != null)
                    {
                        //services.AddTransient(item, impl);
                        services.AddScoped(item, impl);
                    }
                }

                serviceProvider = services.BuildServiceProvider();
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
            }
        }

    }
}

调用


using Microsoft.Extensions.DependencyInjection;
using System;
using System.Text.Json;
using System.Threading.Tasks;
using Xunit;
using System.Collections;
using System.Collections.Generic;

  //测试EF并发查询
        [Fact]
        public async Task Test5()
        {
		    //提取注册的服务接口
            ILoginUserBLL userbll = serviceProvider.GetService<ILoginUserBLL>();
            IImgCategoryBLL   categoryBLL  = serviceProvider.GetService<IImgCategoryBLL>();
            try
            {
               var list = await userbll.PageUserAsync(null, 1, 10);
               var page=await categoryBLL.PageImgCategoryAsync(null ,1,10);
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
            }        
        }

ASP.NET Core MVC调用参考,构造函数注入

using Microsoft.AspNetCore.Mvc;
using MvcSimplePager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebNetCore5_Img_Storage.Handler;
using WebNetCore5_Img_Storage.IBLL;
using WebNetCore5_Img_Storage.Model;

public class CategoryController : BaseController
    {
        private readonly IImgCategoryBLL imgCategoryBLL;
        private readonly IAtlasTagBLL atlasTagBLL;
        private readonly IImgBLL imgBLL;

        public CategoryController(IImgCategoryBLL _imgCategoryBLL, IAtlasTagBLL _atlasTagBLL, IImgBLL _imgBLL)
        {
            imgCategoryBLL = _imgCategoryBLL;
            atlasTagBLL = _atlasTagBLL;
            imgBLL = _imgBLL;
        }

        //view 图册列表
        public async Task<IActionResult> CategoryList(Img_category img_Category, int p = 1, int rows = 10)
        {
            //读取标签列表
            var pageTag = await atlasTagBLL.PageAtlas_tag(null, 1, 200);
            ViewBag.tagList = pageTag.List;
            return View();
        }
}

你可能感兴趣的:(C#,asp.net,core,Injection,依赖注入,控制反转,ASP.NET,core,ioc)