|Number of votes:||2|
When working with Relate (EPiServer Community) sites you sometimes come across entity attributes idea – which is quite powerful. If you are using some of the built-in entities like IUser, Entry (blog post) or for instance VideoGallery you are using object that are derived from FrameworkEntityBase which implements IAttributeExtendableEntity interface. This is the interface which gives you possibility to add some user-defined attributes to the entity. Attribute could be of some predefined scalar value like bool, int, string, etc. or some complex type for instance Club or EntryComment.
This approach gives you pretty high level of flexibility to extend built-in entities with your own required attributes or link together your own entities.
Working with attributes is pretty simple:
And setting the value:
How ever this approach involves some drawbacks:
So I wanted to introduce attribute synchronizer that will sync defined attributes for entities and somehow provide strongly-typed access to the attributes.
Hereby I introduced EntityAttributeBuilder that takes care of some of the tasks which you have to face when working with attribute extendable entities.
So here comes some insight description of the library:
So entity attribute definition may look something like this:
Effect of this definition will be that IUser entity will receive 2 additional attributes (“OrgCode” with type string and “PrimaryClub” with type Club) after initialization module will execute its sync process.
Then comes next part. Next step is to provide strongly-typed access for those attributes. Strongly-typed access to attributes wouldn’t be possible without lambda expressions. So we will borrow some approaches from this post to extract member access expressions.
To support fully strongly-typed approach I made few extension methods for IAttributeExtendableEntity interface.
Using these extension methods it’s now possible to set attribute value using following code snippet:
and you can get attribute value using following snippet:
Anyway this is not perfect solution when reviewing multiple assignments in the row:
Lots of code repetitions and code tries to be non-readable.
So therefore I introduced one more useful extension method that will convert entity into attribute metadata definition class. Code snippet for this:
This gives you opportunity to talk to your entity in interest via metadata definition class.
Behind the scene AsAttributeExtendable() method actually creates proxy and intercepts all the incoming requests for property setters or getters and calls original IAttributeExtendableEntity methods. Therefore it’s possible to talk to entity via metadata definition class and set or get attribute values.
Entity meta data definition looks like this:
Attribute definition supports:
All of the definition values are optional. This means that library will extract all required information from the property information itself which is decorated by the CommunityEntityMetadata attribute. Library is also capable to handle partial entity attribute definitions (for example part of the attributes are defined in one assembly rest are defined in another one). But be careful with duplications – as this will cause exception of course.
What is does not support?
Library could be found at EPiServer NuGet feed called “Geta.Community.EntityAttributeBuilder”.
Library required following assemblies from the EPiServer platform that are not delivered via some of the NuGet packages:
So assumption is that there will be those missing libs.
Hope this helps!