简体   繁体   English

我应该如何使用 aspnetboilerplate 创建一个具有跨多个应用程序服务共享的功能的类?

[英]How should I create a class with functions to be shared across multiple application services with aspnetboilerplate?

I have code/common functionality that needs to be re-used in multiple app services (ie say a simple lookup function).我有需要在多个应用程序服务中重复使用的代码/通用功能(例如,一个简单的查找功能)。 I am looking to create a Helper class that isn't really an API, just something for other app services or even the ui mvc controllers to consume.我正在寻找创建一个不是真正 API 的 Helper 类,只是供其他应用程序服务甚至 ui mvc 控制器使用的东西。

I read in the aspnetboilerplate documentation we should not call an app service from another app service which I agree creates a mess.我在 aspnetboilerplate 文档中阅读了我们不应该从另一个应用服务调用应用服务,我同意这会造成混乱。

I know we can implement a custom repository inheriting RepositoryBase within the Entity Framework project with this logic and inject that repository我知道我们可以使用此逻辑在实体框架项目中实现一个自定义存储库继承 RepositoryBase 并注入该存储库

I also know we can create a domain service and put the code in a manager but this is more application logic than domain logic.我也知道我们可以创建一个域服务并将代码放在管理器中,但这更像是应用程序逻辑而不是域逻辑。

What's the best practice for creating a helper class that can be constructor injected into each application service?创建一个可以作为构造函数注入每个应用程序服务的辅助类的最佳实践是什么?

You may be overthinking this.你可能想多了。

What's the best practice for creating a helper class that can be constructor injected into each application service?创建一个可以作为构造函数注入每个应用程序服务的辅助类的最佳实践是什么?

  1. Create a new project / assembly.创建一个新项目/程序集。
  2. Add the class that you want.添加所需的课程。
  3. Reference that project from the projects that wish to use the class.从希望使用该类的项目中引用该项目。
  4. In your composition root, register the class.在您的作文根目录中,注册该类。
  5. Inject as required.根据需要注入。

I read in the aspnetboilerplate documentation we should not call an app service from another app service which I agree creates a mess.我在 aspnetboilerplate 文档中阅读了我们不应该从另一个应用服务调用应用服务,我同意这会造成混乱。

Agreed.同意。 However, as you state, this is not a service as such.但是,正如您所说,这不是一项服务。 It is a utility class and you're not going to be calling a web-api to access the class, you'll be calling the methods directly on the injected class.它是一个实用程序类,您不会调用 web-api 来访问该类,您将直接在注入的类上调用方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在多个类中使用索引器时,应如何使用属性以及类的结构应如何 - How should I use properties and what should be structure of my class for using indexers across multiple classes 如何使用 Entity Framework Core 定义一个实体 class 作为一个集合在多个实体之间共享? - How do I define an entity class to be shared as a collection across multiple entities using Entity Framework Core? 跨多种服务访问通用功能 - Accessing common functions across multiple services 如何跨多个注入服务继承 DatabaseContext 作为基础 class - How to inherit DatabaseContext as a base class across multiple injected services 从 IHttpContextAccessor 创建共享类以在类库之间传递它 - Create shared class from IHttpContextAccessor to pass it across class libraries 我应该在twilio中使用多个Messenger服务吗? - Should I use multiple messenger services in twilio? 如何拥有一个可以在我的班级之间共享但不能在多个Web请求之间共享的Windsor IoC容器? - How can I have a Windsor IoC container that can be shared amongst my classes but not shared across multiple web requests? C#:如何重写一个类中的函数而无需从不同服务进行多重继承 - C#: How to override functions in one class without multiple inheritance from different services 一个实体共享两个Web服务? - One entity shared across two web services? 我应该在哪里找到 MVC Razor 中共享的 @helper 函数 - Where should I locate shared @helper functions in MVC Razor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM