Random Musings

July 6, 2009

Create simple domain generator by reading from CRM Metadata

Filed under: c#,Dynamics CRM — haditeo @ 9:30 pm
Tags: ,

Objective
I would like to create my own domain (class) from the custom CRM entity. The advantage of this is to simplify domain persistence and retrieval between application layer and database layer (CRM Web Service)

How to achieve this ?

  • First of all we utilize RetrieveEntityRequest message to retrieve all attributes of the specific entity.
                    RetrieveEntityRequest request = new RetrieveEntityRequest();
                    request.LogicalName = entityName;
                    request.EntityItems = EntityItems.IncludeAttributes;
    
                    RetrieveEntityResponse response = (RetrieveEntityResponse)metadataService.Execute(request);
    
                    EntityMetadata entityMetadata = response.EntityMetadata;
    
                    foreach (AttributeMetadata attributeMetadata in entityMetadata.Attributes)
                    {
                        //loop through all attributes here.
                    }
    
  • There are some interesting attributes of the AttributeMetadata. Such as LogicalName attribute, DisplayName attribute and AttributeType attribute
  • By looping through each of the attributes, i read the relevant template file consisting of the code templates and replace the necessary placeholder with the actual values

    private LookupProperty _#internalVariableName#;
    [CrmAttributeMapping("#crmAttributeMapping#")]
    public LookupProperty #publicPropertyName#
    {
        get { return _#internalVariableName#; }
        set { _#internalVariableName# = value; }
    }
    
    public void Set#publicPropertyName#(Guid value)
    {
        _#internalVariableName# = new LookupProperty("#crmAttributeMapping#", new Lookup("#lookupAttributeMapping#", value));
    }
    
    public bool Is#publicPropertyName#Null()
    {
        if (_#internalVariableName# == null)
            return true;
    
        if (_#internalVariableName#.Value == null)
            return true;
    
        if (_#internalVariableName#.Value.Value == Guid.Empty)
            return true;
            
        return false;
    }
    

    Here are the result of the placeholder value replacement

    private LookupProperty _owninguser;
    [CrmAttributeMapping("owninguser")]
    public LookupProperty owninguser
    {
        get { return _owninguser; }
        set { _owninguser = value; }
    }
    
    public void Setowninguser(Guid value)
    {
        _owninguser = new LookupProperty("owninguser", new Lookup("#lookupAttributeMapping#", value));
    }
    
    public bool IsowninguserNull()
    {
        if (_owninguser == null)
            return true;
    
        if (_owninguser.Value == null)
            return true;
    
        if (_owninguser.Value.Value == Guid.Empty)
            return true;
            
        return false;
    }
    
  • Some clean up needs to be performed to tidy up the public Property name, but this code generator has helped me tremendeously to eliminate boring and repititive tasks of creating custom domain objects by hand.
Advertisements

1 Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: