Should thoses kind of service go injected in a base class ? (versus static classes)

瘦欲@ 提交于 2020-01-04 15:15:20

问题


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 ?


回答1:


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.

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.

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.



来源:https://stackoverflow.com/questions/8425668/should-thoses-kind-of-service-go-injected-in-a-base-class-versus-static-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!