Early Bound vs Late Bound

Introduction

A common question in the interviews for MS Dynamics CRM Developers is the difference between Early Bound and Late Bound when creating data objects in a plugin or custom workflow action. In this article we’ll describe each of them, as well as showing a practical example to instantiate them in their own ways.

Late Bound

    • When to use?
      • When the Entity you’ll use is not finished or is very probable that in the future more attributes will be added. Late bound use root entity class called: Entity. Hence, it has a lot of flexibility with new fields and data types. Late Bound allows adding new attributes dynamically, just by sending the schema name as a parameter.
    • What are the advantages of using Late Bound?
      • Do not need Entities.cs
      • Do not need to re-generate Entities.cs each time there’s a change in the Entity(Fields added or deleted)
      • As per described by Microsoft: “use of the Entity class results in slightly better performance than the early bound entity types”
    • Practical example:
            // Create a new Late Bound object
            Entity Contact_LB = new Entity();

            // Set Target Entity
            Contact_LB.LogicalName = "contact";

            // Set Contact's First Name
            Contact_LB.Attributes["firstname"] = "Test";

            // Set Contact's Last Name
            Contact_LB.Attributes["lastname"] = "Contact";

            /*
             * As an example of dynamic fields, let's say that
             * a new field was added to Contact Entity, the field details are:
             *
             * Display Name: Hobby
             * Schema Name: ex_hobby
             * Field Type: String
             *
             * Also, let's say that we receive the schema name of that new
             * field in our input parameters as:
             *
             * string SchemaName_of_NewField;
             *
             * In order to add this new field to the late bound
             * object, simply call .Add() method and send the schema name
             * and value
             */
            Contact_LB.Attributes.Add(SchemaName_of_NewField, "Sports and running");

            // Create Contact in CRM
            service.Create(Contact_LB);

Early Bound

  • When to use?
    • This method is recommended when the Entity you’ll use has been already designed and implemented. Early bound creates an object with the attributes and data types of the specified entity. Hence, it has a strong data type security at compilation time.
    • What are the advantages of using Early Bound?
      • Strong data type security
      • Intellisense simplifies the task of writing the code as the classes and attributes are already contained in Entities.cs
      • Improves and simplifies reading of code
      • Less probable to make mistakes while setting values in the attributes of the data object, due to the strong type security
      • Errors related to data types are detected at compilation time
    • Practical example
            // Create a new Early Bound Contact object
            Contact Contact_EB = new Contact();

            // Set Contact's First Name
            Contact_EB.FirstName = "Test";

            // Set Contact's Last Name
            Contact_EB.LastName = "Contact";

            // Create Contact
            service.Create(Contact_EB);

Hope it helps!

 

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s