Strongly-typed entities
Classes can inherit from FuseIT.S4D.DynamicsConnector.Entities.EntityBase to define strongly-typed entity objects.
using FuseIT.S4D.DynamicsConnector.Metadata;
// The Entity attribute defines the Dynamics 365 entity name.
[Entity("contact")]
class Contact : EntityBase<Contact>
{
// Makes the emailaddress1 field available as an object property, with a type.
public string email
{
get => (string)this["emailaddress1"];
set { this["emailaddress1"] = value; }
}
public string firstname
{
get => (string)this["firstname"];
set { this["firstname"] = value; }
}
public string lastname
{
get => (string)this["lastname"];
set { this["lastname"] = value; }
}
// Any fields not explicitly defined are still accessible via the Dictionary syntax.
// e.g. this["contactid"]
}
This also allows use of alternate query expression syntax.
var session = serviceFactory.GetSessionFromConnectionString("MyDynamicsInstance");
// Find a contact by email address.
var contact = await Contact.LoadSingleAsync(session,
(query, exp) =>
{
ODataExpression filterExpression = (exp.email == "example@example.com");
ODataExpression[] selectExpression = new ODataExpression[] { exp.firstname, exp.lastname };
query.Filter(filterExpression).Select(selectExpression);
}
);
// Returned objects have strongly-type fields where defined.
Console.WriteLine($"First name: {contact.firstname}, Last name: {contact.lastname}");