我如何解决第三层或第四层接口的依赖性

湿软件系统私人有限公司

我正在创建一个体系结构Dotnet Core 3.1

我已经创建了图层

API

->控制器

->服务界面。(这将由控制器层使用)

->服务实施

->数据接口。(这将由服务实现层用作依赖项)

->数据实施

我不想将数据实现公开给控制器层,但必须在服务实现层的构造函数中使用它。

问题是:

如何解析数据实现类?

以及如何在IServiceCollection中注册这些类?

你说过Alehosaini

您可以通过以下方式做到这一点:

  1. 为每个层定义单独的项目。
  2. 根据需要设置各层之间的依赖关系。
  3. 模型映射应该Service Layer在控制器层中,而不是在控制器层中(通常应该在控制器层中,但在您的方案中不适用)。
  4. Microsoft.Extensions.DependencyInjection在您的计算机上安装NuGetService Layer
  5. 定义扩展方法DependencyInjectionService Layer
  6. 在中调用扩展方法 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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Laravel的第三层关系的延迟渴望加载

来自分类Dev

LINQ-从包含三层的列表中检索对象的第三层列表

来自分类Dev

如何使第三层菜单与该项目内联打开?

来自分类Dev

CSS下拉菜单中第三层的位置不正确

来自分类Dev

使用视差CSS的HREF在第三层上不起作用

来自分类Dev

选择第三层ActiveRecord_Relation-Rails,ActiveRecord,HABTM

来自分类Dev

使用视差CSS的HREF在第三层上不起作用

来自分类Dev

CakePHP 3 - 模型 - 从数组的第三层访问数据

来自分类Dev

如何解决转速依赖性

来自分类Dev

三层应用

来自分类Dev

DDD中的UI层依赖性

来自分类Dev

Python中的三层相等性比较

来自分类Dev

如何解决指针数组中的数据依赖性?

来自分类Dev

如何解决VLC的依赖性“ vlc-nox”?

来自分类Dev

如何解决包裹问题/依赖性

来自分类Dev

如何在python中定义三层字典?

来自分类Dev

这种依赖性为我提供了一个罐子的两个版本。我该如何解决?

来自分类Dev

遍历三层嵌套字典

来自分类Dev

三层架构中的外部通讯

来自分类Dev

三层嵌套字典理解?

来自分类Dev

Kramdown中的三层嵌套列表

来自分类Dev

三层部分嵌套模型

来自分类Dev

三层嵌套字典理解?

来自分类Dev

Java 中的三层类调用

来自分类Dev

如何解决npm WARN可选跳过跳过的依赖性:[email protected]

来自分类Dev

在Ubuntu 14.04上安装Cinnamon时如何解决未满足的依赖性?

来自分类Dev

升级到Ubuntu 18.04失败后如何解决软件包依赖性?

来自分类Dev

在Linux上安装软件时如何解决循环依赖性?

来自分类Dev

如何解决visual studio 2017中system.web的程序集依赖性?