30 May 2012

crmsvcutil.exe generated classes differences

Prepared this post some time ago, but forgot to publish. In any case ...

CRM SDK allow you to use early binding. For this purpose you should use crmsvcutil.exe to generate classes which represent CRM entities. All the classes are derived from Microsoft.Xrm.Sdk.Entity class. I tried to realize what was changed in generated classes since beta version of SDK.



[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("accountid")]
public override System.Guid Id
{
  get
  {
    return base.Id;
  }
  set
  {
    this.AccountId = value;
  }
}


In the latest version of crmsvcutil.exe attribute  AttributeLogicalNameAttribute is applied to Id property of each entity. Using these attribute you are able to retrieve logical names of attributes which you can use e.g. in queries to service. Hot to do that you can read here. But I prefer to use generated class with all field name constants.

In beta version all OptionSet entity attributes looks like:


public System.Nullable<YourDataModel.AccountState> StateCode
{
  get
  {
     Microsoft.Xrm.Sdk.OptionSetValue optionSet = this.GetAttributeValue<Microsoft.Xrm.Sdk.OptionSetValue>("statecode");
     return ((YourDataModel.AccountState)(System.Enum.ToObject(typeof(YourDataModel.AccountState), optionSet.Value)));
  }
}

but now it looks like:


public System.Nullable<YourDataModel.AccountState> StateCode
{
  get
  {
    Microsoft.Xrm.Sdk.OptionSetValue optionSet = this.GetAttributeValue<Microsoft.Xrm.Sdk.OptionSetValue>("statecode");
    if ((optionSet != null))
    {
      return ((YourDataModel.AccountState)(System.Enum.ToObject(typeof(YourDataModel.AccountState), optionSet.Value)));
    }
    else
    {
      return null;
    }
  }
}

It's more safely. And the similar update related with collection fields like:


public System.Collections.Generic.IEnumerable<YourDataModel.ActivityParty> OptionalAttendees
{
  get
  {
    Microsoft.Xrm.Sdk.EntityCollection collection = this.GetAttributeValue<Microsoft.Xrm.Sdk.EntityCollection>("optionalattendees");
    if (((collection != null)
          && (collection.Entities != null)))
    {
      return System.Linq.Enumerable.Cast<YourDataModel.ActivityParty>(collection.Entities);
    }
    else
    {
      return null;
    }
  }
  set
  {
    this.OnPropertyChanging("OptionalAttendees");
    if ((value == null))
    {
      this.SetAttributeValue("optionalattendees", value);
    }
    else
    {
      this.SetAttributeValue("optionalattendees", new Microsoft.Xrm.Sdk.EntityCollection(new System.Collections.Generic.List<Microsoft.Xrm.Sdk.Entity>(value)));
    }
    this.OnPropertyChanged("OptionalAttendees");
  }
}

in previous version it looked like


public System.Collections.Generic.IEnumerable<YourDataModel.ActivityParty> OptionalAttendees
{
  get
  {
    return System.Linq.Enumerable.Cast<YourDataModel.ActivityParty>(this.GetAttributeValue<Microsoft.Xrm.Sdk.EntityCollection>("optionalattendees").Entities);
  }
  set
  {
    this.OnPropertyChanging("OptionalAttendees");
    this.SetAttributeValue("optionalattendees", new Microsoft.Xrm.Sdk.EntityCollection(new System.Collections.Generic.List<Microsoft.Xrm.Sdk.Entity>(value)));
    this.OnPropertyChanged("OptionalAttendees");
  }
}

And that's all. There are no more significant differences in generated with crmsvcutil.exe tool files. Only if you use context object there was added to each entity method like:


public void AddToAccountSet(YourDataModel.Account entity)
{
  this.AddObject(entity);
}

No comments:

Post a Comment