EntityMapper.Dynamics.CRM 1.0.0

There is a newer version of this package available.
See the version list below for details.
Install-Package EntityMapper.Dynamics.CRM -Version 1.0.0
dotnet add package EntityMapper.Dynamics.CRM --version 1.0.0
<PackageReference Include="EntityMapper.Dynamics.CRM" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EntityMapper.Dynamics.CRM --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: EntityMapper.Dynamics.CRM, 1.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install EntityMapper.Dynamics.CRM as a Cake Addin
#addin nuget:?package=EntityMapper.Dynamics.CRM&version=1.0.0

// Install EntityMapper.Dynamics.CRM as a Cake Tool
#tool nuget:?package=EntityMapper.Dynamics.CRM&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Entity Mapper

Entity mapper is an attribute based mapper for Microsoft Dynamics CRM SDK.

Existing methods

There are two programming models that you can choose from Early bound and Late bound.

Late bound method

Use the code generation tool (CrmSvcUtil) to create early-bound entity classes, derived from the Entity class, which you can use to access business data in Dynamics 365 Customer Engagement. These classes include one class for each entity in your installation, including custom entities. More information: Use the early bound entity classes in code

Early bound method

The Entity class contains the logical name of an entity and a property-bag array of the entity's attributes. This lets you use late binding so that you can work with types such as custom entities and custom attributes that were not present when your application was compiled. More information: Use the late bound entity class in code

Entity Mapper method

Lets you enjoy the stability of a strong typed model and the agility of a property-bag array.

Usage

The "CrmEntityBase" Class

This basic class holds only two attributes

LogicalName (the schema name for the sorce entity) Id (as you know every entity in dynamics crm has one)

Every model you build has to inherit from this class.

public abstract class CrmEntityBase
{
    public abstract string LogicalName { get; }
    public Guid Id { get; set; }
}

A Basic Model

public class ContactModel : CrmEntityBase
{
    public override string LogicalName => "contact";

    [CRMField("fullname")]
    public string FullName { get; set; }

    [CRMLookup("parentaccountid", "account")]
    public Guid? ParentAccountId { get; set; }
}

Mapping From Entity

var contactEntity = new Entity("contact")
{
    ["fullname"] = "John Doe",
    ["parentaccountid"] = new EntityReference("account", Guid.Empty)
};

var contactModel = Mapper.ToModel<ContactModel>(contactEntity);

Console.WriteLine(contactModel.FullName);
Console.WriteLine(contactModel.ParentAccountId.ToString());

// The example displays the following output:
//  John Doe
//  00000000-0000-0000-0000-000000000000

Mapping To Entity

var contactModel = new ContactModel
{
    FullName = "Bar Refaeli",
    ParentAccountId = Guid.Empty,
};

var contactEntity = Mapper.ToEntity(contactModel);
var fullname = contactEntity.GetAttributeValue<string>("fullname");
var parentAccountRef = contactEntity.GetAttributeValue<EntityReference>("parentaccountid");

Console.WriteLine(fullname);
Console.WriteLine(parentAccountRef.LogicalName);
Console.WriteLine(parentAccountRef.Id);

// The example displays the following output:
//  Bar Refaeli
//  account
//  00000000-0000-0000-0000-000000000000

Getting The Required ColumnSet

var contactColumnSet = Mapper.GetColumnSet<ContactModel>();

Console.WriteLine(string.Join(", ",contactColumnSet.Columns));

// The example displays the following output:
//  fullname, parentaccountid

CRMFieldType

This is the type of the field that we are declaring. It has the following values:

  1. Basic - A general map string > string, int > int, etc..
  2. OptionSet - int get / set the value of OptionSet
  3. Lookup - Guid get / set the id of the referenced target
  4. Money - decimal get / set the value of the Money object
  5. FormattedValue - string get (only) the formatted value
  6. Enum - Enum get / set the value of OptionSet (using its numeric value)
  7. LookupName - string get the reference name.

Attributes Examples

Lookup

public class ContactModel : CrmEntityBase
{
    ...
    [CRMLookup("parentaccountid", "account")]
    public Guid? ParentAccountId { get; set; }
    ...
}
var contactModel = new ContactModel
{
    ParentAccountId = Guid.Empty,
};

var contactEntity = Mapper.ToEntity(contactModel);
var parentAccountRef = contactEntity.GetAttributeValue<EntityReference>("parentaccountid");
var contactModelAgain = Mapper.ToModel<ContactModel>(contactEntity);

Console.WriteLine(parentAccountRef.LogicalName);
Console.WriteLine(parentAccountRef.Id);
Console.WriteLine(contactModelAgain.ParentAccountId);

// The example displays the following output:
//  account
//  00000000-0000-0000-0000-000000000000
//  00000000-0000-0000-0000-000000000000

FormattedValue

public class ContactModel : CrmEntityBase
{
    ...
    [CRMField("parentaccountid", CRMFieldType.FormattedValue)]
    public string ParentAccountIdName { get; set; }
    ...
}
var contactEntity = new Entity("contact", Guid.NewGuid())
{
    ["parentaccountid"] = new EntityReference("account", Guid.Empty)
};

// faking formatted value
contactEntity.FormattedValues["parentaccountid"] = "Mama Theresa";
var contactModel = Mapper.ToModel<ContactModel>(contactEntity);

Console.WriteLine(contactModel.ParentAccountIdName);

// The example displays the following output:
//  Mama Theresa

Enum

public enum ContactMood
{
    Sad = 0,
    Tired = 1,
    Happy = 2,
    Coding = 14,
}

public class ContactModel : CrmEntityBase
{
    ...
    [CRMEnum("new_mood", typeof(ContactMood?))]
    public ContactMood? Mood { get; set; }
    ...
}
var contactModel = new ContactModel
{
    FullName = "Bar Refaeli",
    ParentAccountId = Guid.Empty,
    Mood = ContactMood.Coding
};

var contactEntity = Mapper.ToEntity(contactModel);
var contactState = contactEntity.GetAttributeValue<OptionSetValue>("new_mood");
var contactModelAgain = Mapper.ToModel<ContactModel>(contactEntity);

Console.WriteLine(contactState.Value);
Console.WriteLine(contactModelAgain.Mood);
Console.WriteLine(contactModelAgain.Mood.GetType().Name);

// The example displays the following output:
//  14
//  Coding
//  ContactMood

LookupName

public class ContactModel : CrmEntityBase
{
    ...
    [CRMField("parentaccountid", CRMFieldType.LookupName)]
    public string ParentAccountIdName { get; set; }
    ...
}
var accountRef = new EntityReference("account", Guid.Empty)
{
    Name = "Mama Theresa"
};

var contactEntity = new Entity("contact", Guid.Empty)
{
    ["parentaccountid"] = accountRef,
};

var contactModel = Mapper.ToModel<ContactModel>(contactEntity);

Console.WriteLine(contactModel.ParentAccountIdName);

// The example displays the following output:
//  Mama Theresa

Working With Aliased Values

In development

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.7 89 8/15/2021
1.0.6 135 6/18/2021
1.0.5 125 6/18/2021
1.0.4 124 6/18/2021
1.0.3 130 6/18/2021
1.0.2 134 6/18/2021
1.0.1 122 3/13/2021
1.0.0 91 3/12/2021

Initial Release