iT邦幫忙

0

.Net 6 Context獨立至ClassLibrary

  • 分享至 

  • xImage
  •  

延續 .NET 6 WEB API 如何安裝DB First
如何將EninyFrameWork 獨立至一個專案,
而不需透過Controller 去得到Contenxt

1.在UserManageRepository專案加入BuilderExtender.cs

using UserManageRepository.Context;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace UserManageRepository
{
    public class BuilderExtender
    {
        public static void AddDbContexts(WebApplicationBuilder? builder)
        {
            builder.Services.AddDbContext<dbCustomDbSampleContext>();
        }
    }
}

2.在UserManageRepository專案加入cs
這個.cs是用來做Models做的增刪茶改等動作,
並且在此引入contenxt的 這樣就不需要再cotroller引入Context了

(1)IUserRepositoryEnity.cs

using Microsoft.EntityFrameworkCore;
using UserManageRepository.Context;
using UserManageRepository.Interfaces;
using UserManageRepository.Models.Data;

namespace UserManageRepository.Repository
{
    public class UserRepositoryEnity: IUserRepositoryEnity
    {
        private readonly dbCustomDbSampleContext _dbCustomDbSampleContext;
        public UserRepositoryEnity(dbCustomDbSampleContext context) {
            _dbCustomDbSampleContext = context;
        }

        public async Task<IList<User>> GetUser()
        {
            return await _dbCustomDbSampleContext.Users.ToListAsync();
        }
    }
}

(2)IUserRepositoryEnity.cs
=>這個Interface因為繼承了UserRepositoryEnity,所以後續再controller只要用呼叫就可以取得 他所隊的Repository了

using UserManageRepository.Models.Data;

namespace UserManageRepository.Interfaces
{
    public interface IUserRepositoryEnity
    {
        Task<IList<User>> GetUser();
    }
}

3.在Program.cs 中的加入

builder.Services.AddScoped<IUserRepositoryEnity, UserRepositoryEnity>();//這邊怎樣把 DbContext 移到 Repository
BuilderExtender.AddDbContexts(builder);//將註冊地方更改為UserManageRepository專案

範例如下

using UserManageRepository.Interfaces;
using UserManageRepository.Repository;
using UserManageRepository;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddScoped<IUserRepositoryEnity, UserRepositoryEnity>();//這邊怎樣把 DbContext 移到 Repository
BuilderExtender.AddDbContexts(builder);//將註冊地方更改為UserManageRepository專案

var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

完成 如此便可在Controller較用

4.Controller叫用

using Microsoft.AspNetCore.Mvc;
using UserManageRepository.Context;
using UserManageRepository.Interfaces;

namespace PJUserManage.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class UserManageController : ControllerBase
    {
        private readonly IUserRepositoryEnity _IUserRepository;
        public UserManageController(dbCustomDbSampleContext context, IUserRepositoryEnity users)
        {
            _IUserRepository = users;
        }

        [HttpGet]
        public async Task<IActionResult> GetUser()
        {
            return Ok(await _IUserRepository.GetUser());
        }
    }
}


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言