Yeap
it may be little be aside from the subject
I am building mechanism that will allow the users of the system to create filters on different entities. In the database I wanted to store the TargetEntity /collection of which entity is to be fetched after GetMulti() call/, then PredicateExpression in separate binary field.
I have done several tasks to make it posible.
First found a way to get the related entites of the target Entity. Reflection and Recursion through the Relations of each Entity down to desired level, in my case up to 3. This is the entities on which the given Filter can have conditions(predicates) on.
Second you must be able to iterate throught the Predicates in a PredicateExpression and extract the Entities taking part in a particular PredicateExpression. After that, having the Entities in the PredicateExpression, you iterate through this list and add just the needed Relations in a RelationCollection, that need to be supplied to GetMulti().
And finally we have the PredicateExpression and the RelationCollection we only need to call GetMulti() and this is where the TargetEntity steps in. As i mentioned before I decided to save the type name of the collection in the database like "PersonCollection" and not entity name as "PersonEntity", cause i did not find anywhere in PersonEntitiy mentioned what collection it builds while there is field in PersonCollection what entities it contains. And one more GetMulti is method of "PersonCollection".
public static EntityCollectionBase GetResultRecords(FilterEntity filter)
{
IRelationCollection relationBucket = new RelationCollection();
List<string> EntityNames = new List<string>();
//Need to add the target Entity first.
string fullEntityName = filter.TargetEntity; //This must be TargetCollection
if (!fullEntityName.Contains(","))
{
fullEntityName = String.Format("{0},{1}", fullEntityName,"Omegasoft.Omeks.BusinessObjects");
}
Type t = Type.GetType(fullEntityName);
EntityCollectionBase entityColl = (EntityCollectionBase)Activator.CreateInstance(t);
EntityNames.Add(entityColl.EntityFactoryToUse.ForEntityName); //This is where EntityName is stored in EntityCollection
BuildEntitiesList(ref EntityNames, (PredicateExpression)filter.FilterExpression);
EntityNames.AddRange(EntityNames);
relationBucket.ObeyWeakRelations = true;
relationBucket.AddRange(GetRelations(EntityNames, JoinHint.Left));
bool res = BusinessEntityAdapter.GetMulti(entityColl, filter.FilterExpression,0,relationBucket);
return entityColl;
}
BusinessEntityAdapter is our class that is intended to add additional Predicates to restrict the returned entities according to the rights of the user in the system.