Implicit Dynamics CRM data-type conversion

by Daniel Halan 24. June 2010 00:29

When working with Microsoft CRM data-types using web services there is no constructor parameters due to limitations of the WSDL code generation. This leads to few lines of code each time one want to create a variable of any CRM type.

This can be optimized using a Factory class where you encapsle the creation of the desired class. But one even cleaner way is to create a partial class and define "implicit" operators between CRM data-type and .NET data-type.

Here is an example on how it works.

Standard way of assigning a .NET DateTime to CRM DateTime,

CrmDateTime cdt = new CrmDateTime();
cdt.Value = DateTime.Today.ToString("s");

Using implicit declaration,

CrmDateTime cdt = DateTime.Now;

and back,

DateTime dt = cdt;

The code behind this,

public partial class CrmDateTime {

  public CrmDateTime(DateTime dt) {
    this.Value = dt.ToString("s");
  }
  public static implicit operator CrmDateTime(DateTime dt) {
     return new CrmDateTime (dt);
  }
  public static implicit operator DateTime(CrmDateTime dt) {
     return DateTime.Parse(dt.Value);
  }
}

Tags: ,

.NET | CRM

blog comments powered by Disqus

Blog3ngine dot NET 1.6.1.2
Theme by Daniel Halan

About the author

Daniel Halan Daniel Halan, M.Sc. systems architect in Sweden.

Working primary with Microsoft .NET, Dynamics CRM, XRM & Web Read more...

The content of this site are my own personal opinions and do not represent my employer's view in anyway.


Follow Me
@derekmorrison Thanks :) 26 Feb 2011

Microsoft Dynamics Salary Survey 2012

Nigel Frank International would like to invite you to complete our annual survey of global Microsoft Dynamics salaries. The survey will only take a couple of minutes to complete and your response and any personal details will be kept strictly confidential.

Prizes,
1st  iPad2 16gb Wi-Fi + 3G 
2nd Xbox 360 + Kinect 
3rd Kindle Keyboard w/ Free 3G + Wi-Fi 
4th MS LifeCam Studio Webcam 
5th MS Arc Touch Mouse 

You will also receive a FREE copy of the Salary Survey report once it has been compiled. 

Take the Salary Survey 
The survey is available in 12 languages for your convenience.