从干净体系结构中的应用程序层中的基础架构项目中调用方法C#

发布于 2025-01-31 13:07:22 字数 1310 浏览 3 评论 0 原文

我对 c#中的清洁体系结构。

基于细节,我的理解是所有业务逻辑都必须在应用程序层中,并且任何基础架构级别的内容(DB CRUD操作,消息处理程序)都必须在基础架构层中。

因此,我已经开始开发,但是发现应用程序层中基础架构层没有依赖性。即使是为什么他们要说的是将业务逻辑保留在该层的应用程序层和下行操作中。

没有这种依赖性,我如何访问下层中的CRUD操作。

同样,我不理解的是基础架构层中的依赖性。

有人请帮忙,因为我真的很困扰。我的应用程序层中有很少的方法,我也需要将输出写入数据库,而我不知道该如何实现。

如此简单,它就是这样的基于描述


应用程序层

imessageBroker.cs

Public class IMessageBroker
{
    Task<int> pushtoqueue(string queue, string message);
}

myService.cs

public class myservice
{
   public void MyMethod
   {
      //Here I need to call the pushtoqueue method which I dont know
   }
}

基础结构层

< em> MessageBroker.cs

public class messagebroker :Imessagebroker
{
   public task<int> pushtoqueue(string queue, string message)
   {
     // here the queue pushing code comes
   }
}

dependencyInjection.cs

services.Addscoped<IMessageBroker, MessageBroker>();

请分享您的想法..

环境

.net 6

Azure功能应用4.0

I am pretty new to Clean Architecture in C#.

Based on the details my understanding was all the business logic must be in Application Layer and any infrastructure level things (DB Crud operations, Message handlings) must be in infrastructure layer.

So I have started the development, but found there is no dependency with infrastructure layer in application layer. Even why they are saying keep the business logic in application layer and infra operations in that layer.

Without that dependency how can I access the CRUD operations in infra layer.

Also there is dependency with Applicaiton layer in infrastructure layer which is I didnt understand.

Someone please help because I really got stuck with it. I have few methods in my application layer and I need to write the output into DB as well, which I dont know how to implement.

So simple it would be like this based on the description


Application Layer

IMessageBroker.cs

Public class IMessageBroker
{
    Task<int> pushtoqueue(string queue, string message);
}

myservice.cs

public class myservice
{
   public void MyMethod
   {
      //Here I need to call the pushtoqueue method which I dont know
   }
}

Infrastructure Layer

MessageBroker.cs

public class messagebroker :Imessagebroker
{
   public task<int> pushtoqueue(string queue, string message)
   {
     // here the queue pushing code comes
   }
}

DependencyInjection.cs

services.Addscoped<IMessageBroker, MessageBroker>();

Please share your thoughts..

Environment

.NET 6

Azure Function Apps 4.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

段念尘 2025-02-07 13:07:22

您链接到的样本使用称为“控制倒置”(IOC)的概念。该概念适应您的样本并以(冗长的)句子定义,是关于将依赖性从应用程序层到基础架构层的依赖性,以便应用程序层将其对基础结构层的要求定义(以接口的形式),并可以与实现这些接口的任何基础架构层。

如果您查看概述部分中的图表,则有一个从基础架构层到应用程序层的箭头。这表达了从应用程序层上的基础架构层的依赖关系。

在您的情况下,组件可用于以下目的:

  • iMessageBroker :应用程序层中的接口可以弄清基础架构层必须满足的要求。
  • MyService :使用基础结构组件的服务类。类型 iMessageBroker 的实例应注入类,以访问消息代理。
  • Message Broker iMessageBroker的具体实现

这种方法的好处是,您可以轻松地替换基础架构组件而无需更改应用程序层。从一个消息经纪更改为另一个消息代理可能很少,但是在测试 myService 时,能够使用模拟对象是无价的。

要回答原始问题:如何调用 pushtoqueue 方法?

只需将 iMessageBroker 的实例注入 myService 并在此处使用:

public class MyService
{
  private readonly IMessageBroker _msgBroker;

  public MyService(IMessageBroker msgBroker) 
  {
    _msgBroker = msgBroker;
  }
 
  public void MyMethod
  {
    //Here I need to call the pushtoqueue method which I dont know
    _msgBroker.PushToQueue("queue", "message");
  }
}

在示例中,您已经将 imessageBroker 的注册添加到IOC容器中,因此,当创建 MyService 时,将注入实例。


附带说明:命名惯例后,提高了程序员的可读性。我从这个问题中更改了一些名称。有关c#命名约定的概述,请参阅此

The sample you linked to uses a concept called "inversion of control" (IoC). Adapted to your sample and defined in a (lengthy) sentence, this concept is about inverting the dependency from the Application layer to the Infrastructure layer so that the Application layer defines its requirements towards the Infrastructure layer (in form of interfaces) and can work with any Infrastructure layer that implements these interfaces.

If you look at the diagram in the overview section, there is an arrow from the Infrastructure layer to the Application layer. This expresses the dependency from the Infrastructure layer on the Application layer.

In your case, the components serve the following purpuse:

  • IMessageBroker: interface in Application layer that defindes the requirements that the Infrastructure layer has to fulfill.
  • MyService: service class that uses infrastructure components. An instance of type IMessageBroker should be injected into the class in order to access the message broker.
  • MessageBroker: concrete implementation of IMessageBroker.

The benefit of this approach is that you can easily replace infrastructure components without changing the application layer. While changing from one message broker to another might be comparatively seldom, being able to use mock objects when testing MyService is invaluable.

To answer the original question: how can you call the pushtoqueue method?

Just inject an instance of IMessageBroker into MyService and use it there:

public class MyService
{
  private readonly IMessageBroker _msgBroker;

  public MyService(IMessageBroker msgBroker) 
  {
    _msgBroker = msgBroker;
  }
 
  public void MyMethod
  {
    //Here I need to call the pushtoqueue method which I dont know
    _msgBroker.PushToQueue("queue", "message");
  }
}

In your sample, you already add a registration of IMessageBroker to the IoC container, so the instance is injected when creating MyService.


As a side note: following naming conventions increases readability for fellow programmers. I've changed some of the names from the question. For an overview of the C# naming conventions see this link.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文