#CompositeDesignPattern
Dynamics CRM development can be much easier and efficient using "Object Oriented Design" techniques. Let me explain.
Suppose the case study of dynamics CRM is such that xyz business is having customers as companies each company has sub branches which is again customers. Customers have people representing contacts for the business.
If I say that customers are accounts then sub branches means self relationship with accounts so there is 1-n self relation between accounts. Now for representatives of company, we have contacts entity in CRM. so there is 1-n relation between accounts and contacts.
Means
1 - N relation : Accounts -> Accounts
1 - N relation : Accounts -> Contacts
Now suppose I want to get all such information, logic and relationships in terms of object oriented design, what I should do is. Firstly I will identify the objects in this case study.
I have two objects Accounts and Contacts.
So now I will surely and definitely define classes for these objects. i.e. the pillar of object oriented design.
I have Account class and Contact class. Should I start writing these classes and adding properties for the respective objects? But what if I have properties which are same in both objects. Or I will prefer to make and identify properties similar for both classes and all other classes which can be added later for CRM case studies.
So for the purpose of adding similar properties I will like to define an Abstract class. This class will hold the parent class properties and act as base class to share the properties and methods for all child classes (entities of CRM).
Model: (Objects)
Dynamics CRM development can be much easier and efficient using "Object Oriented Design" techniques. Let me explain.
Suppose the case study of dynamics CRM is such that xyz business is having customers as companies each company has sub branches which is again customers. Customers have people representing contacts for the business.
If I say that customers are accounts then sub branches means self relationship with accounts so there is 1-n self relation between accounts. Now for representatives of company, we have contacts entity in CRM. so there is 1-n relation between accounts and contacts.
Means
1 - N relation : Accounts -> Accounts
1 - N relation : Accounts -> Contacts
Now suppose I want to get all such information, logic and relationships in terms of object oriented design, what I should do is. Firstly I will identify the objects in this case study.
I have two objects Accounts and Contacts.
So now I will surely and definitely define classes for these objects. i.e. the pillar of object oriented design.
I have Account class and Contact class. Should I start writing these classes and adding properties for the respective objects? But what if I have properties which are same in both objects. Or I will prefer to make and identify properties similar for both classes and all other classes which can be added later for CRM case studies.
So for the purpose of adding similar properties I will like to define an Abstract class. This class will hold the parent class properties and act as base class to share the properties and methods for all child classes (entities of CRM).
Model: (Objects)
using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Account
{
class Model
{
}
/// <summary>
/// Abstract Entity Class
/// </summary>
public abstract class CRMEntity
{
public Entity Entity;
public EntityReference EntityReference;
public string EntityDisplayName;
public string Href;
public string CurrentAttributeName;
//Other Common Properties
public CRMEntity(Entity entity)
{
this.Entity = entity;
this.EntityReference = new EntityReference(entity.LogicalName, entity.Id);
this.Href = "https://datainvent-sandbox.crm3.dynamics.com/main.aspx?etn=" + Entity.LogicalName + "&id=" + Entity.Id.ToString() + "&newWindow=true&pagetype=entityrecord";
// https://datainvent-sandbox.crm3.dynamics.com/main.aspx?etn=insr_inspectionelement&id=60a3e6da-5584-e711-8125-480fcff44541&newWindow=true&pagetype=entityrecord
}
public CRMEntity(Entity entity, string EntityDisplayName) : this(entity)
{
SetName(EntityDisplayName);
}
public void SetName(string EntityDisplayName)
{
this.EntityDisplayName = EntityDisplayName;
this.Entity.Attributes.Add("EntityName", this.EntityDisplayName);
}
public bool IsValidAttribute(string name)
{
if (this.Entity.Attributes.Contains(name))
return true;
return false;
}
}
public class Account : CRMEntity
{
public Account ParentAccount { get; set; }
public List<Account> SubAccounts = new List<Account>();
public List<Contact> Contacts = new List<Contact>();
public Account() : base(new Entity(EntityNames.Account)) { }
public Account(Entity entity) : base(entity) { }
public Account(Entity entity, Account ParentAccount) : base(entity) {
this.ParentAccount = ParentAccount;
}
public string Name
{
get
{
CurrentAttributeName = "name";
if (this.IsValidAttribute(CurrentAttributeName))
{
return this.Entity.GetAttributeValue<string>(CurrentAttributeName);
}
return null;
}
set
{
CurrentAttributeName = "name";
this.Entity[CurrentAttributeName] = value;
}
}
public Guid AccountId
{
get
{
CurrentAttributeName = "accountid";
if (this.IsValidAttribute(CurrentAttributeName))
{
return this.Entity.GetAttributeValue<Guid>(CurrentAttributeName);
}
return Guid.Empty;
}
set
{
CurrentAttributeName = "accountid";
this.Entity[CurrentAttributeName] = value;
}
}
public string AccountNo
{
get
{
CurrentAttributeName = "accountno";
if (this.IsValidAttribute(CurrentAttributeName))
{
return this.Entity.GetAttributeValue<string>(CurrentAttributeName);
}
return null;
}
set
{
CurrentAttributeName = "accountno";
this.Entity[CurrentAttributeName] = value;
}
}
}
public class Contact : CRMEntity
{
public Account ParentAccount { get; set; }
public Contact() : base(new Entity(EntityNames.Account)) { }
public Contact(Entity entity) : base(entity) { }
public string FirstName
{
get
{
CurrentAttributeName = "firstname";
if (this.IsValidAttribute(CurrentAttributeName))
{
return this.Entity.GetAttributeValue<string>(CurrentAttributeName);
}
return null;
}
set
{
CurrentAttributeName = "firstname";
this.Entity[CurrentAttributeName] = value;
}
}
public string LastName
{
get
{
CurrentAttributeName = "lastname";
if (this.IsValidAttribute(CurrentAttributeName))
{
return this.Entity.GetAttributeValue<string>(CurrentAttributeName);
}
return null;
}
set
{
CurrentAttributeName = "lastname";
this.Entity[CurrentAttributeName] = value;
}
}
public Guid ContactId
{
get
{
CurrentAttributeName = "contactid";
if (this.IsValidAttribute(CurrentAttributeName))
{
return this.Entity.GetAttributeValue<Guid>(CurrentAttributeName);
}
return Guid.Empty;
}
set
{
CurrentAttributeName = "contactid";
this.Entity[CurrentAttributeName] = value;
}
}
public EntityReference ParentAccountId
{
get
{
CurrentAttributeName = "parentcustomerid";
if (this.IsValidAttribute(CurrentAttributeName))
{
return this.Entity.GetAttributeValue<EntityReference>(CurrentAttributeName);
}
return null;
}
set
{
CurrentAttributeName = "parentcustomerid";
this.Entity[CurrentAttributeName] = value;
}
}
}
public class AccountHierarchy
{
public List<Account> Accounts { get; set; }
public List<Contact> Contacts { get; set; }
public AccountHierarchy() {
Accounts = new List<Account>();
Contacts = new List<Contact>();
}
}
public class EntityNames
{
public static string Account = "account";
public static string Contact = "contact";
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Account
{
class Model
{
}
/// <summary>
/// Abstract Entity Class
/// </summary>
public abstract class CRMEntity
{
public Entity Entity;
public EntityReference EntityReference;
public string EntityDisplayName;
public string Href;
public string CurrentAttributeName;
//Other Common Properties
public CRMEntity(Entity entity)
{
this.Entity = entity;
this.EntityReference = new EntityReference(entity.LogicalName, entity.Id);
this.Href = "https://datainvent-sandbox.crm3.dynamics.com/main.aspx?etn=" + Entity.LogicalName + "&id=" + Entity.Id.ToString() + "&newWindow=true&pagetype=entityrecord";
// https://datainvent-sandbox.crm3.dynamics.com/main.aspx?etn=insr_inspectionelement&id=60a3e6da-5584-e711-8125-480fcff44541&newWindow=true&pagetype=entityrecord
}
public CRMEntity(Entity entity, string EntityDisplayName) : this(entity)
{
SetName(EntityDisplayName);
}
public void SetName(string EntityDisplayName)
{
this.EntityDisplayName = EntityDisplayName;
this.Entity.Attributes.Add("EntityName", this.EntityDisplayName);
}
public bool IsValidAttribute(string name)
{
if (this.Entity.Attributes.Contains(name))
return true;
return false;
}
}
public class Account : CRMEntity
{
public Account ParentAccount { get; set; }
public List<Account> SubAccounts = new List<Account>();
public List<Contact> Contacts = new List<Contact>();
public Account() : base(new Entity(EntityNames.Account)) { }
public Account(Entity entity) : base(entity) { }
public Account(Entity entity, Account ParentAccount) : base(entity) {
this.ParentAccount = ParentAccount;
}
public string Name
{
get
{
CurrentAttributeName = "name";
if (this.IsValidAttribute(CurrentAttributeName))
{
return this.Entity.GetAttributeValue<string>(CurrentAttributeName);
}
return null;
}
set
{
CurrentAttributeName = "name";
this.Entity[CurrentAttributeName] = value;
}
}
public Guid AccountId
{
get
{
CurrentAttributeName = "accountid";
if (this.IsValidAttribute(CurrentAttributeName))
{
return this.Entity.GetAttributeValue<Guid>(CurrentAttributeName);
}
return Guid.Empty;
}
set
{
CurrentAttributeName = "accountid";
this.Entity[CurrentAttributeName] = value;
}
}
public string AccountNo
{
get
{
CurrentAttributeName = "accountno";
if (this.IsValidAttribute(CurrentAttributeName))
{
return this.Entity.GetAttributeValue<string>(CurrentAttributeName);
}
return null;
}
set
{
CurrentAttributeName = "accountno";
this.Entity[CurrentAttributeName] = value;
}
}
}
public class Contact : CRMEntity
{
public Account ParentAccount { get; set; }
public Contact() : base(new Entity(EntityNames.Account)) { }
public Contact(Entity entity) : base(entity) { }
public string FirstName
{
get
{
CurrentAttributeName = "firstname";
if (this.IsValidAttribute(CurrentAttributeName))
{
return this.Entity.GetAttributeValue<string>(CurrentAttributeName);
}
return null;
}
set
{
CurrentAttributeName = "firstname";
this.Entity[CurrentAttributeName] = value;
}
}
public string LastName
{
get
{
CurrentAttributeName = "lastname";
if (this.IsValidAttribute(CurrentAttributeName))
{
return this.Entity.GetAttributeValue<string>(CurrentAttributeName);
}
return null;
}
set
{
CurrentAttributeName = "lastname";
this.Entity[CurrentAttributeName] = value;
}
}
public Guid ContactId
{
get
{
CurrentAttributeName = "contactid";
if (this.IsValidAttribute(CurrentAttributeName))
{
return this.Entity.GetAttributeValue<Guid>(CurrentAttributeName);
}
return Guid.Empty;
}
set
{
CurrentAttributeName = "contactid";
this.Entity[CurrentAttributeName] = value;
}
}
public EntityReference ParentAccountId
{
get
{
CurrentAttributeName = "parentcustomerid";
if (this.IsValidAttribute(CurrentAttributeName))
{
return this.Entity.GetAttributeValue<EntityReference>(CurrentAttributeName);
}
return null;
}
set
{
CurrentAttributeName = "parentcustomerid";
this.Entity[CurrentAttributeName] = value;
}
}
}
public class AccountHierarchy
{
public List<Account> Accounts { get; set; }
public List<Contact> Contacts { get; set; }
public AccountHierarchy() {
Accounts = new List<Account>();
Contacts = new List<Contact>();
}
}
public class EntityNames
{
public static string Account = "account";
public static string Contact = "contact";
}
}