General default filter for Collections (Self Servicing)

Posts   
 
    
Alexander
User
Posts: 8
Joined: 13-Jul-2007
# Posted on: 28-Mar-2008 09:48:00   

Hello everybody,

I'm using LLBLGen Pro 2.5 (Sept. 27, 2007) with MS SQL-Server and Self Servicing. And I have to say: I love LLBLGen :-)

But now I need a little bit help: Nearly all of my Tables have a Column "Deleted by", which contains a Username if the record is "deleted" or with value NULL, when the record is not deleted.

Now I would like to create a "default-build-in" filter into all of my Collections, which will filter out deleted records. And of course I only want to write this code once and not for every single Collection-Class.

How can I change my Collection (or the Collection.template?) to do that? I would like

  1. Check whether the field "Deleted by" is available
  2. if so, then set a default filter
  3. if there is still a filter, use it and expand it with my default filter

For Example: I have a (single) "ProjectEntity" which contains a lot of "Attachments". Via


projectEntity.Attachment

I get a Collection of Attachments. Of course I only want undeleted Attachment's.

Also when I directly use a "AttachmentCollection" with getMulti I want to get undeleted Attachments without setting an extra filter.

How can I do this directly in my Collection classes (or collection.template of LLBLGen)?

I hope you understand my poor english.....

Thank's a lot for your help,

Alexander

Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 28-Mar-2008 11:25:14   

The following is a relevant thread: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=11690

You just would need to use the appropriate events instead of these in the above thread. But it should put you in the right track.

Alexander
User
Posts: 8
Joined: 13-Jul-2007
# Posted on: 01-Apr-2008 11:09:41   

Hello Walaa,

and thank you very much for your precious hint! It leads me into the right direction. I manged it to override the OnEntityAdding Method like this:


protected override bool OnEntityAdding(AttachmentEntity entityToAdd)
        {
            bool result = base.OnEntityAdding(entityToAdd);

            
                EntityBase entity = entityToAdd as EntityBase;

                EntityField field = entity.Fields["DeletedBy"] as EntityField;
                if ((Object)field != null)
                {
                    if (field.DbValue != null)
                    {
                        result = false;
                    }
                } 
            return result;
        }

It works fine. But I think there has to be a better way which is more performant. On my way, the collection is fetchin ALL Data and THEN the collection will decide whether to add the Entity or not.

I'm sure there is a way to avoid fetchin the "deleted" records by changin the SQL-Statment - maybe by adding an default filter.

But I can't find a proper Method to override. Can you please give me another hint?

My collection should add a default filter BEFORE the End-SQL-Statement is sent to the SQL-Server. So, I would need a method like


OnBeforeFetchinData()
{ 
  //Adding a default filter here
 ....
}

Thank's a lot,

Alexander

Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 02-Apr-2008 16:53:30   

IMHO, you shouldn't hardcode this filter in the generated code to be your default fetching filter. Because you would never be able to fetch the deleted entities in case you wanted to for any reason.

Rather you can do the following: 1- In each class of the generated collection classes you may add a new method in the CustomEntityCollectionCode, say you call it MyGetMulti() which will wrap the default getMulti and add pass the needed filter.

2- you may automatically generate this method if you modify the entityCollection.template.

Alexander
User
Posts: 8
Joined: 13-Jul-2007
# Posted on: 04-Apr-2008 08:52:35   

Thank you Walla for your answer. Indeed I added a property to the collection "IgnoreDeletedRecords" which default-value is true. In this case the Collections will not Adding the "deleted" Entities. If I want to see the deleted ones I only have to set the property to false and refetch the data.

Your Idea with "MyGetMulti" sounds interesting. But what will be with Collections which are included in Entities? (For example One Entity "Project" with a Collection of "Attachments") Because I'm using SelfServicing the AttachmentCollection will use the default GetMulti and not my onw MyGetMulti?

A very tricky problem I think...