Decorators in .NET Core with Dependency Injection - Online Free Computer Tutorials.

'Software Development, Games Development, Mobile Development, iOS Development, Android Development, Window Phone Development. Dot Net, Window Services,WCF Services, Web Services, MVC, MySQL, SQL Server and Oracle Tutorials, Articles and their Resources

Wednesday, February 13, 2019

Decorators in .NET Core with Dependency Injection

What is a Decorator? Feel free to skip this section if you're already familiar with the Decorator pattern The Decorator Pattern allows you to add functionality to an implementation of an interface by wrapping it in another implementation. e.g. interface IService { string GetValue(); } class DbService { public string GetValue() = "value from DB"; } class LoggingService { private readonly IService _concreteService; private readonly ILogger _logger; public LoggingService(IService concreteService, ILogger logger) { _concreteService = concreteService; _logger = logger; } public string GetValue() { _logger.LogInformation("Getting value"); var value = _concreteService.GetValue(); _logger.LogInformation("Retrieved {0}", value); return value; } } We have a concrete implementation of an IService interface (DbService) that returns a string value. LoggingService then decorates that implementation by wrapping invocation of the concrete instance and logging entry and exit. This pattern is very useful when you want to augment either your own or framework types with some new behaviour such as logging or exception handling.


I guess you came to this post by searching similar kind of issues in any of the search engine and hope that this resolved your problem. If you find this tips useful, just drop a line below and share the link to others and who knows they might find it useful too.

Stay tuned to my blogtwitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.


This article is related to


.NET Core,c#,Development,decorator,design-patterns,software-development

No comments:

Post a Comment