Walaa wrote:
Do you want to extend the entityFields or maybe all you need is some custom properties?
If you want to extend the entityFields, would you please explain why?
There are some field I want to have on the Entity that can't easily be setup in the Designer. I am testing doing this with both custom entities and typedlists - both have their pros and cons.
Here is some sample code for one of the custom entities and its factory...
public partial class ShipDFNEntity
{
public const int FollowUpMessagesFieldIndex = (int) ShipDFNFieldIndex.AmountOfFields;
public const int HasPriorityMessagesFieldIndex = FollowUpMessagesFieldIndex + 1;
public virtual int FollowUpMessagesCount
{
get { return (int) GetValue(FollowUpMessagesFieldIndex, true); }
set { SetValue(FollowUpMessagesFieldIndex, value); }
}
public virtual bool HasPriorityMessages
{
get { return (int) GetValue(HasPriorityMessagesFieldIndex, true) > 0; }
set { SetValue(HasPriorityMessagesFieldIndex, value); }
}
protected override void OnInitClassMembersComplete()
{
ShipDFNEntityFactory.ExtendFields(Fields);
}
protected override string GetFormatString(char pattern)
{
switch(pattern)
{
case 'n':
//TODO: Remove this hack!!!
return StringHelper.ToTitleCase(Name);
case 'g':
case 'G':
return Name;
default:
return base.GetFormatString(pattern);
}
}
}
using System;
using BPOSS.Entities.EntityClasses;
using BPOSS.Entities.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
namespace BPOSS.Entities.FactoryClasses
{
public partial class ShipDFNEntityFactory
{
static ShipDFNEntityFactory()
{
docRelations.Add(DocumentTransactionEntity.Relations.DocumentEntityUsingDocID, JoinHint.Left);
}
static readonly RelationCollection docRelations = new RelationCollection();
static readonly Predicate documentFilter = ShipDFNFields.ID == DocumentTransactionFields.ShipID &
DocumentTransactionFields.VoyageID == DBNull.Value &
DocumentTransactionFields.AllocatedFlag == true;
static readonly ScalarQueryExpression followUpMessagesQuery =
new ScalarQueryExpression(
DocumentFields.ID.SetAggregateFunction(AggregateFunction.Count),
documentFilter & DocumentFields.FollowUpFlag == true, docRelations);
static readonly ScalarQueryExpression hasPriorityMessagesQuery =
new ScalarQueryExpression(
DocumentFields.ID.SetAggregateFunction(AggregateFunction.Count),
documentFilter & DocumentFields.PriorityFlag == true, docRelations);
public override IEntityFields2 CreateFields()
{
return ExtendFields(base.CreateFields());
}
internal static IEntityFields2 ExtendFields(IEntityFields2 fields)
{
if (fields.Count == (int)ShipDFNFieldIndex.AmountOfFields)
{
fields.Expand(2);
fields.DefineField(new EntityField2("FollowUpMessagesCount", followUpMessagesQuery), ShipDFNEntity.FollowUpMessagesFieldIndex);
fields.DefineField(new EntityField2("HasPriorityMessages", hasPriorityMessagesQuery), ShipDFNEntity.HasPriorityMessagesFieldIndex);
}
return fields;
}
}
}
I am using the same field expanding code (in ExtendFields(IEntitiyFields)) and calling it from two places: when the entity is created and when CreateFields on the factory is called.
This code seems to work though but I just wanted to confirm this is the necessary way to do it as I would have thought there would be a single place where the Fields for an entity are created and only one override was required.
Cheers
Simon