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 Answers
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.

- 225,310
- 48
- 427
- 736
-
1You describe the different types of dependencies well, but where do you answer `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`? – jgauffin Dec 08 '11 at 08:33
-
None of the above. I would implement them as Decorators as explained in the link. – Mark Seemann Dec 08 '11 at 09:22
-
@MarkSeemann But what a "Decorators" ? Ive read your blog post and still can't understand what a "Decorator" is. Do you have any concrete example ? – Rushino Dec 08 '11 at 13:57
-
@MarkSeemann Its hard to understand what going on.. are you saying that if you create a class that extend ISpecificRepository you can set something on top on that such IBonusSpecificRepository and each time we access ISpecificRepository.. code in IBonusSpecificRepository will be taken into consideration ? If we use IProductRepository repository = new PerformanceMeasuringProductRepository( new CachingProductRepository( new SqlProductRepository(), new Cache() ), new RealStopwatch() ); var vm = new PrécisViewModel(repository); – Rushino Dec 08 '11 at 14:29
-
@MarkSeemann I think i get it. Interessting approach. But it seem only useful for stuff like cache, logging, etc.. i can't see an account system implementation fit in this. For other stuffs such business logic components i suppose they only injected in business logic layer since its part of that responsability ? – Rushino Dec 08 '11 at 14:52
-
1Yes, Decorators are mostly suitable for Cross-cutting Concerns. Business logic can still be 'normally' injected into consumers. – Mark Seemann Dec 08 '11 at 15:43
-
@MarkSeemann One last thing i am failing to see.. is how the binding look like ? If you bind IRepository to SQLRepository.. how it know which implementation to take ? – Rushino Dec 08 '11 at 19:03
-
1It sounds like you are thinking in the context of a specific DI Container... SO doesn't like long comment discussions, so you should ask a new question about that, stating with which specific container you need help. – Mark Seemann Dec 08 '11 at 19:42
-
@MarkSeemann well even with a non specific DI container i still can't see how you can bind that stuff. – Rushino Dec 09 '11 at 14:33
-
http://stackoverflow.com/questions/8447037/how-the-binding-are-done-with-decorators-using-ninject – Rushino Dec 09 '11 at 14:37
-
@MarkSeemann The problem in the example you showed me is that all the classes inherit from IRepository.. shouldntProductsService containt an ICacheProductRepository and CacheProductsRepository contain an IProductsRepository ? – Rushino Dec 09 '11 at 15:34