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 3.0.0.0
Theme by Daniel Halan

About the author

Daniel Halan Daniel Halan, M.Sc., Microsoft Windows Azure Advisor Software Architect and Lead Developer from Sweden, currently in Bangkok.

Working primary with Microsoft .NET, Windows Azure, DDD, CQRS and some Dynamics CRM  Read more...

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

Month List