我正在创建一个体系结构Dotnet Core 3.1
。
我已经创建了图层
API
->控制器
->服务界面。(这将由控制器层使用)
->服务实施
->数据接口。(这将由服务实现层用作依赖项)
->数据实施
我不想将数据实现公开给控制器层,但必须在服务实现层的构造函数中使用它。
问题是:
如何解析数据实现类?
以及如何在IServiceCollection中注册这些类?
您可以通过以下方式做到这一点:
Service Layer
在控制器层中,而不是在控制器层中(通常应该在控制器层中,但在您的方案中不适用)。Microsoft.Extensions.DependencyInjection
在您的计算机上安装NuGetService Layer
DependencyInjection
在Service Layer
。Startup.cs
我准备了一个解释答案的例子。
这个例子:
控制器类代码:
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using TestProj.AppServices.Interfaces;
namespace TestProj.Api.Controllers
{
[ApiController]
[Route("[controller]")]
public class ProductsController : ControllerBase
{
private readonly ILogger<ProductsController> _logger;
private readonly IProductService _service;
public ProductsController(ILogger<ProductsController> logger,
IProductService service)
{
_logger = logger;
_service = service;
}
[HttpGet("{id=1}")] //Set default value for example only, it should be [HttpGet("{id}")]
public IActionResult Get(int id)
{
return Ok(_service.GetById(id));
}
}
}
产品服务
using TestProj.AppServices.Interfaces;
using TestProj.AppServices.Models;
using TestProj.Data.Interfaces;
namespace TestProj.AppServices.AppServices
{
public class ProductService : IProductService
{
private readonly IProductRepository _repository;
public ProductService(IProductRepository repository)
{
_repository = repository;
}
public Product GetById(int id)
{
//Subject code here
//Dummy code:
var productFromDataLayer = _repository.GetById(id);
//Mapping (You can use AutoMapper NuGet)
var product = new Product
{
Id = productFromDataLayer.Id,
Name = productFromDataLayer.Name
};
return product;
}
}
}
产品服务
using TestProj.AppServices.Models;
namespace TestProj.AppServices.Interfaces
{
public interface IProductService
{
Product GetById(int id);
}
}
产品服务层模型
namespace TestProj.AppServices.Models
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
}
产品资料库
using TestProj.Data.Interfaces;
using TestProj.Data.Model;
namespace TestProj.Data.Data
{
public class ProductRepository : IProductRepository
{
public Product GetById(int id)
{
//Subject code here
//Dummy code:
var product = new Product
{
Id = 1,
Name = "Product 1"
};
return product;
}
}
}
IProduct存储库
using TestProj.Data.Model;
namespace TestProj.Data.Interfaces
{
public interface IProductRepository
{
Product GetById(int id);
}
}
数据层产品模型(实体)
namespace TestProj.Data.Model
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
}
依赖注入扩展类
using Microsoft.Extensions.DependencyInjection;
using TestProj.AppServices.AppServices;
using TestProj.AppServices.Interfaces;
using TestProj.Data.Data;
using TestProj.Data.Interfaces;
namespace TestProj.AppServices.Others
{
public static class DependencyInjection
{
public static void AddProjectServicesAndRepositoresDependencyInjection(this IServiceCollection services)
{
//Services
services.AddTransient<IProductService, ProductService>();
//Data
services.AddTransient<IProductRepository, ProductRepository>();
}
}
}
创业班
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using TestProj.AppServices.Others;
namespace TestProj.Api
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddProjectServicesAndRepositoresDependencyInjection();
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
我已将示例的源代码上传到GitHub https://github.com/ualehosaini/LayeredArchitecturePreparedToAnswerForAStackOverflowQuestion。您可以将其用于项目,也可以定义/添加所需的任何组件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句