日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

ASP.NET Core 2.2 WebApi 系列【三】AutoFac 倉(cāng)儲(chǔ)接口的依賴注入

 Coder編程 2020-05-06

一、準(zhǔn)備工作

通過程序包管理器控制臺(tái)安裝AutoFac:

Install-Package Autofac.Extensions.DependencyInjection

創(chuàng)建新類庫(kù)(.NetCore 2.2類庫(kù)),存放接口跟實(shí)現(xiàn)類,命名為NetCoreWebApi.Repository。

創(chuàng)建用戶倉(cāng)儲(chǔ)接口

在類庫(kù)項(xiàng)目上右鍵->添加->新建文件夾,命名為Interface,存放接口類。在Interface文件夾下面新建類:IUserRepository,屬性如下:

using System.Collections.Generic;
using NetCoreWebApi.Model.Models;

namespace NetCoreWebApi.Repository.Interface
{
    /// <summary>
    /// 用戶接口
    /// </summary>
    public interface IUserRepository
    {
        /// <summary>
        /// 添加用戶
        /// </summary>
        /// <param name="entity">實(shí)體對(duì)象</param>
        int Add(TbUser entity);
        /// <summary>
        /// 刪除用戶
        /// </summary>
        /// <param name="entity">實(shí)體對(duì)象</param>
        int Remove(TbUser entity);
        /// <summary>
        /// 編輯用戶
        /// </summary>
        /// <param name="entity">實(shí)體對(duì)象</param>
        /// <returns></returns>
        int Update(TbUser entity);
        /// <summary>
        /// 獲取所有 
        /// </summary>
        /// <returns></returns>
        IList<TbUser> GetAll();
    }
}

創(chuàng)建用戶接口實(shí)現(xiàn)類

在類庫(kù)項(xiàng)目上右鍵->添加->新建文件夾,命名為Implement,存放接口實(shí)現(xiàn)類。在Implement文件夾下面新建類:UserRepository,屬性如下:

using System.Collections.Generic;
using System.Linq;
using NetCoreWebApi.Model;
using NetCoreWebApi.Model.Models;
using NetCoreWebApi.Repository.Interface;

namespace NetCoreWebApi.Repository.Implement
{
    /// <summary>
    /// 業(yè)務(wù)處理
    /// </summary>
    public class UserRepository:IUserRepository
    {
        private readonly MyDbContext _dbContext;
        /// <summary>
        /// 構(gòu)造函數(shù)
        /// </summary>
        /// <param name="dbContext"></param>
        public UserRepository(MyDbContext dbContext)
        {
            _dbContext = dbContext;
        }
        /// <summary>
        /// 添加用戶
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Add(TbUser entity)
        {
            _dbContext.TbUsers.Add(entity);
            return _dbContext.SaveChanges();
        }
        /// <summary>
        /// 刪除用戶
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Remove(TbUser entity)
        {
            _dbContext.TbUsers.Remove(entity);
            return _dbContext.SaveChanges();
        }
        /// <summary>
        /// 編輯用戶
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(TbUser entity)
        {
            return _dbContext.SaveChanges();
        }
        /// <summary>
        /// 查詢用戶
        /// </summary>
        /// <returns></returns>
        public IList<TbUser> GetAll()
        {
            return _dbContext.TbUsers.ToList();
        }
    }
}

二、配置注入

打開Startup.cs類

把ConfigureServices方法的返回值由void變?yōu)镮ServiceProvider

        public static IContainer ApplicationContainer { get; set; }
        /// <summary>
        /// //負(fù)責(zé)注入服務(wù)
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            var connectionStr = Configuration.GetConnectionString("SqlServer");
            services.AddDbContext<MyDbContext>
                (options => options.UseSqlServer(connectionStr,
                    e => e.MigrationsAssembly("NetCoreWebApi.Model")));
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            //初始化容器
            var builder = new ContainerBuilder();
            //管道寄居
            builder.Populate(services);
            //注冊(cè)倉(cāng)儲(chǔ),IUserRepository接口到UserRepository的映射
            builder.RegisterType<UserRepository>().As<IUserRepository>();
            //構(gòu)造
            ApplicationContainer = builder.Build();
            //將AutoFac反饋到管道中
            return new AutofacServiceProvider(ApplicationContainer);
        }

三、測(cè)試

在項(xiàng)目上右鍵->添加->新建文件夾,命名為Controllers,存放相應(yīng)的控制器。在Controllers文件夾下面新建一個(gè)控制器:UserController,如下:

using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using NetCoreWebApi.Model.Models;
using NetCoreWebApi.Repository.Interface;

namespace NetCoreWebApi.Controllers
{
    /// <summary>
    /// 用戶模塊
    /// </summary>
    [Route("api/user")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IUserRepository _userRepository;

        /// <summary>
        /// 構(gòu)造函數(shù)
        /// </summary>
        /// <param name="userRepository"></param>
        public UserController(IUserRepository userRepository)
        {
            _userRepository = userRepository;
        }
        /// <summary>
        /// 創(chuàng)建用戶
        /// </summary>
        /// <returns></returns>
        [Route("createUser")]
        [HttpPost]
        public TbUser CreateUser()
        {
            var user = new TbUser
            {
                UserId = Guid.NewGuid().ToString("N"),
                CreateTime = DateTime.Now,
                UserName = "tenghao",
                Email = "tenghao510@qq.com"
            };
            _userRepository.Add(user);
            return user;
        }
        /// <summary>
        /// 查詢用戶
        /// </summary>
        /// <returns></returns>
        [Route("getUser")]
        [HttpGet]
        public IList<TbUser> GetUser()
        {
            return _userRepository.GetAll();
        }
    }
}

Ctrl+F5 運(yùn)行之后,先用Postman調(diào)創(chuàng)建用戶接口

 

接下來測(cè)試下查詢用戶

好了,你們自己測(cè)下寫的有沒有問題。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多