应放入系统样的服务在基类中去注射? (相对于静态类) [英] Should thoses kind of service go injected in a base class ? (versus static classes)

查看:71
本文介绍了应放入系统样的服务在基类中去注射? (相对于静态类)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道..如果我有这样的服务ILoggingService,IMailerService,ICacheService。这些都是莫名其妙地基础设施的一部分。然而,你会让他们作为一个静态类,或者你会注入他们在一个基类,使所有派生类可以访问他们作为单身?你如何处理?

I was wondering.. if i have services such ILoggingService, IMailerService, ICacheService. Those are part of the infrastructure somehow. However, would you make them as a static class or would you inject them in a base class so that all derived classes get access to them as singleton ? How do you handle them ?

推荐答案

依赖往往的再present基础设施组件。不仅发送电子邮件,但是任何类型的数据的访问可以被认为是基础设施的一部分。这样的服务是最好的保存为'适当'的依赖,因为他们实现应用程序的行为。你可以说,他们针对应用程序的功能需求。

Dependencies often represent infrastructure components. Not only emailing, but data access of any kind can be considered part of the infrastructure. Such Services are best kept as 'proper' dependencies, since they implement the behavior of your application. You could say that they address the functional requirements of an application.

有一个功能上的差异的电子邮件是否被发送,或记录保存在数据库中。

There's a functional difference whether or not an email was sent, or a record saved in a database.

然而,像缓存重新present非功能性需求。该应用程序工作没有它,但它会很慢。有时记录属于这一类,而在其他时候它是一个功能性的要求。

However, things like caching represent non-functional requirements. The application works without it, although it'd be slower. Logging sometimes falls into this category, while at other times it's a functional requirement.

许多基础架构组件(如日志记录,审计,缓存,授权,计量等)的横切关注点所以往往更好地处理为的装饰,可有时对AOP 一概而论。

Many infrastructure components (such as logging, auditing, caching, authorization, metering, etc.) are Cross-cutting Concerns so are often better addressed as Decorators, which can sometimes be generalized towards AOP.

这篇关于应放入系统样的服务在基类中去注射? (相对于静态类)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆