There might be times when you need to set an entity property value by the property name as a string at runtime. Sure, you could use reflection for this and use the PropertyInfo class, but there is a built-in way that is much easier to use. All entities implement the IDynamicEntity interface, which provides this functionality out of the box.
// IDynamicEntity is located in Sage.Platform.Orm.Interfaces.IDynamicEntity
When you use an entity object via it’s IDynamicEntity interface, it allows to to use dynamic properties instead of static.
Static/Normal Code Example:
var account = Sage.Platform.EntityFactory.Create<IAccount>(); account.AccountName = "Some Account"; account.Type = "Customer"; account.Save();
Dynamic Code Example using IDynamicEntity:
var account (Sage.Platform.Orm.Interfaces.IDynamicEntity)Sage.Platform.EntityFactory.Create<IAccount>(); account["AccountName"] = "Some Account"; account["Type"] = "Customer"; account.Save();
Here’s another example:
var phoneField = "MainPhone"; var account = this.BindingSource.Current as Sage.Platform.Orm.Interfaces.IDynamicEntity; account[phoneField] = "8005551212"; account["Type"] = "Customer"; // it works for relationship properties as well account["Owner"] = EntityFactory.GetById<IOwner>("SYST00000001"); account.Save();
Granted, there’s likely very limited scenarios where you’d need to access the entity properties by name in this way, but it does allow you to dynamically set entity properties without the use of (and overhead of) reflection.