Dynamic CRM Decorator Pattern
Have you ever curious why sometimes your plugins run very slow? Then you wonder where are the critical operation that makes all of operation slower? Of course you can investigate this in many ways: from database server perspective, network, etc. Today I want to share how to easily leveraging Dynamic CRM Functionality to make a simple log with a Decorator Pattern.
Decorator Pattern is a design pattern to add a functionality of a class, without changing the original behavior of that class. In this case I will show you guys how to add new functionality in real OrganizationService class. Without further ado, let see the example of implementation:
public class LogOrganizationService : IOrganizationService
{
private readonly IOrganizationService _service;
public LogOrganizationService(IOrganizationService service)
{
//Get the real class using Dependency Injection
_service = service;
}
public Guid Create(Entity entity)
{
var now = DateTime.Now;
var result = _service.Create(entity);
var end = DateTime.Now;
//Your logic to add the log
return result;
}
}
The LogOrganizationService constructor retrieve the real implementation of IOrganizationService. Then the LogOrganizationService class just work as a wrapper of that class. For example in these, we wrap the Create operation to do logger.
Then for using this one, we only to do like this:
public class PostIncidentCreate : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
var service = (IOrganizationService)serviceProvider.GetService(typeof(IOrganizationService));
var decorateService = new LogOrganizationService(service);
//Do your business logic using decorateService
}
}
Viola, you already learn your decorator pattern!
This was originally posted here.

Like
Report
*This post is locked for comments