CRM Helper

Many times we need to retrieve, insert or update data from dot net application, in the application we create CRM object many times, even last created CRM object is not yet destroyed. But because of it we consume more recourses, result of it performance get impacted. Thus, we have use previously created  object if it exist. Also we should be create CRM object  each time we need it, instead check for existing object.

Here is the example of it, in the following code I used singleton pattern to create CRM service object, when we call it will return me previously created object  and if not found then new object will be created.

Here is the CRM helper class with singleton pattern

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;

namespace DTool.CrmService
{
///
/// Singleton class
///

public class CRMConnection
{
///
/// providing Crm Connection – Create one time instance
///

private static CRMConnection _serviceInstance;
///
/// CRM Service Interface
///

public static IOrganizationService _organizationService;
///
/// Control Id – CRM Sdk
///

private Guid _controlID;
///
/// Not Yet use in Application – For Future Scope while we would like to work or in a case of multithreading(Sigleton is no more available).
///

private static object _mutexLock = new object();

///
/// OrganizationService
///

public IOrganizationService OrganizationService
{
get { return _organizationService; }
}

///
/// ControlID
///

public Guid ControlID
{
get { return _controlID; }
}

///
/// CRMConnection – Private connection for Singleton
/// works for early and late bound
///

///private CRMConnection(Guid controlID)
{
IServiceConfiguration serviceConfig = null;
OrganizationServiceProxy service = null;
ClientCredentials userCredentials = null;

serviceConfig = ServiceConfigurationFactory.CreateConfiguration(
new Uri(ConfigurationManager.AppSettings[“CrmUrl”] + “/XRMServices/2011/Organization.svc”));

userCredentials = new ClientCredentials();
userCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;

using (service = new OrganizationServiceProxy(serviceConfig, userCredentials))
{
service.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
}

_organizationService = (IOrganizationService)service;
_controlID = controlID;

}

///
/// Return Crm Connection Object
///

///
public static CRMConnection GetConnection()
{
if (_serviceInstance == null)
{
lock (_mutexLock) //in case of multi threading
_serviceInstance = new CRMConnection(new Guid());//guid in case of creation of uniq instance.

}
return _serviceInstance;
}
}
}

Advertisements

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