简体   繁体   English

角度服务中的业务规则?

[英]Business rules in angular services?

I will apply Business rules on my Angular service . 我将在Angular service上应用Business rules This one is to display news created today only. 这是只显示今天创建的新闻。

I parsed the response of my call like Array<News> . 我解析了Array<News>类的呼叫响应。

My question is : 我的问题是:

Can I directly apply my business rules in my service? 我可以在服务中直接应用业务规则吗? Or I need to apply this in component (after parsing)? 还是我需要在组件中应用它(解析后)? Or by other way? 还是通过其他方式?

Better would be to perform all complicated process in Service Layer and let Component handle the view ( DOM manipulation, animation etc). 更好的方法是在Service层中执行所有复杂的过程,并让Component处理视图(DOM操作,动画等)。

You can have more than one Service Layer to handle different level of task. 您可以具有多个服务层来处理不同级别的任务。

  1. Service Level 1 - Handle HTTP Call 服务等级1-处理HTTP呼叫
  2. Service Level 2 - Parsing and manipulating 服务级别2-解析和操作
  3. Component Level - Display parsed content. 组件级别 -显示已解析的内容。

Angular provides you with multiple ways to act here, The most important thing to remember is that Angular is wired with DI , meaning: Angular为您提供了多种操作方法,最重要的是Angular与DI相连 ,这意味着:

  • You create a service 您创建服务
  • Register it as a provider 将其注册为提供者
  • Inject it into a component using the @Injectable decorator to expose functionality between component and service. 使用@Injectable装饰器将其注入组件,以暴露组件和服务之间的功能。

There are two ways from here: 从这里有两种方法:

One: 一:

You write your business logic in your component, using your service as data transporter - The service will take params, pass it to the server and will return response with params from the server. 您可以使用服务作为数据传输器 ,在组件中编写业务逻辑-该服务将获取参数,并将其传递给服务器,并将返回来自服务器的带有参数的响应。

Two: 二:

The component layer will take care for the client logic and will only use the service methods, the service will be responsible for the business logic and all the component will do is pass params for the requests, receive params from the response and handle them according to the client's needs. 组件层将照顾客户端逻辑,并且仅使用服务方法,服务将负责业务逻辑,而所有组件将要做的就是传递请求的参数,从响应接收参数并根据客户的需求。

What is right? 什么是对的?

This will depend on the architecture and the course of action you decide to take. 这将取决于您决定采取的体系结构和操作过程。 Both of them are correct, it is up to you to decide which one will benefit you more. 两者都是正确的,由您来决定哪一个将使您受益更多。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM