CRM 4 – CrmService and ICrmService problems

I have recently been writing a CRM 4 plugin.  Usually when I code a plugin, I write the some unit tests and use the CrmService to create entities, retrieve entities etc.

I usually structure the code so the plugin code calls the same function in a seperate class.  This means I can test the code without having to deploy the plugin,

This works ok in CRM 2011 but in CRM 4 plugins use an interface ICrmService.

this forum posts discusses the problem

The problem is you can cast a ICrmService to a CrmService, which basically meant I had to duplicate the methods I had written, one to use CrmService and another to use iCrmService.

To get round the problem I have written a wrapper class which implements the ICrmService interface and basically wraps the CrmService.  I found the answer for this solution on this post although it was my plan to do this after I had quickly got the code working with duplicating the methods.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;

namespace Metaphorix.Crm.Plugin.OrderValidation
{
    class CrmServiceWrapper : ICrmService
    {
        private bool _disposed;
        private readonly CrmService _service;

        public CrmServiceWrapper(CrmService service)
        {
            _service = service;
        }
        public Guid Create(BusinessEntity entity)
        {
            return _service.Create(entity);
        }

        public void Delete(string entityName, Guid id)
        {
            _service.Delete(entityName, id);
        }

        public object Execute(object request)
        {
            return _service.Execute((Request)request);
        }

        public string Fetch(string fetchXml)
        {
            return _service.Fetch(fetchXml);
        }

        public BusinessEntity Retrieve(string entityName, Guid id, global::Microsoft.Crm.Sdk.Query.ColumnSetBase columnSet)
        {
            return _service.Retrieve(entityName, id, columnSet);
        }

        public BusinessEntityCollection RetrieveMultiple(global::Microsoft.Crm.Sdk.Query.QueryBase query)
        {
            return _service.RetrieveMultiple(query);
        }

        public void Update(BusinessEntity entity)
        {
            _service.Update(entity);
        }

        public void Dispose()
        {
            _service.Dispose();
        }
    }
}
Advertisements

4 thoughts on “CRM 4 – CrmService and ICrmService problems

  1. Mitch Milam November 29, 2011 / 8:01 pm

    This was great Ben. I also created a MetadataService version:

    class MetadataServiceWrapper : IMetadataService
    {
    private bool _disposed;
    private readonly MetadataService _service;

    public MetadataServiceWrapper(MetadataService service)
    {
    _service = service;
    }

    public object Execute(object request)
    {
    return _service.Execute((MetadataServiceRequest)request);
    }

    public void Dispose()
    {
    _service.Dispose();
    }
    }

    Like

    • Hosk November 30, 2011 / 12:09 am

      Fantastic. It will make CRM 4 coding a bit easier and it needs all the help it can get compared to CRM 2011 coding.

      Like

  2. Teja September 18, 2014 / 7:54 am

    what about updateAsync

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s