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);
}
}