by Phil Adams
11. January 2010 23:26
Generics is a very nice feature of the C# and VB.NET language, and can be used to simplify the CRM Web Service method calls. One of my responsibilities is to develop a framework for CRM development, and one of the main classes in this framework is called CrmSystem, it wraps the CrmService methods among other things. Using generics one can then type,
C#
account acc = CrmSystem.Retrieve<account>(myAccountId);
VB
Dim acc As account = CrmSystem.Retrieve(Of account)(myAccountId)
also we use a special NameValue class CrmConditions to one of our Execute overloads, here is how it can look
C#
List<account> acc = CrmSystem.Execute<account>(new CrmCondition("emailaddress1", me@someone.com));
VB
Dim acc As List(Of account) = CrmSystem.Execute(Of account)(New CrmCondition("emailaddress1", "me@someone.com"))
This would retrieve all accounts that got the email "me@someone.com"
Here is one of the overloads for the Retrive Method that uses Generics, it calls an other overload that does the actual call to CRM Web Service using the EntityName string that we get thru typeof(T).Name
C#
public T Retrieve<T>(Guid id, params string[] columnSet) where T : BusinessEntity {
return (T)Retrieve(typeof(T).Name, id, new ColumnSet(columnSet));
}
VB
Public Function Retrieve(Of T As BusinessEntity)(ByVal id As Guid, ByVal ParamArray columnSet As String()) As T
Return DirectCast(Retrieve(GetType(T).Name, id, New ColumnSet(columnSet)), T)
End Function
Hope this gives some inspiration for your own CrmApi wrappers :)
Generics with CRM