Forum:  Custom Templates

Thread:  RemovedEntitiesTracker initialization


gilles.marceau (User)   Posted on: 14-Feb-2017 11:49:06.
Hi,

I'm actually developping a WPF application in which I use the RemovedEntitiesTracker property of EntityCollection to manage the deletion of the entities.

I would like to simplify the use of it by initialize this property automatically to be not have to check if it's null before using it.
Is there a way to do it or must I change the LLBLGEN template to generate the needed code ?
If changing the template is the only way, can you give me the place where I must put it (because I tried to put it on the constructors of the EntityCollection but it seems to not be the right way to do it).

Thanks in advance
Otis (LLBLGen Pro Team)   Posted on: 14-Feb-2017 15:23:55.
The best way to deal with this I think is to simply let the framework deal with it by using DataScopes http://www.llblgen.com/Documentation/5.1/LLBLGen%20Pro%20RTF/Using%20the%20generated%20code/gencode_datascopes.htm

Have you looked at these? There's an example as well on github: https://github.com/SolutionsDesign/LLBLGenProExamples_5.x/tree/master/Example_DataScope


gilles.marceau (User)   Posted on: 15-Feb-2017 14:18:21.
A lot of work has already been done at this time so it's not a possibility for me to use datascope.

Actually, the UI call "service" methods (not real services, just the name) which are intercept using AOP to create DataAccessAdapter, manage transactions, error log, performance log...

Because I fetch a graph of objects (say Order with Order details) and that Order Details can be removed, I need to be able to trace the removal to delete it on the "service" side.

In previous projects, I used to initialize RemoveedEntitiesTracker but it was not really a good stuff so I would like to improve it.
gilles.marceau (User)   Posted on: 15-Feb-2017 17:03:11.
Finally I modifiy the "commonEntityBaseAdapter.template" template to initialize the RemovedEntitiesTracker properrty like this

Code:

        protected EntityCollection<T> GetOrCreateEntityCollection<T, TFactory>(string navigatorName, bool setContainingEntityInfo, bool forMN, ref EntityCollection<T> destination)
            where T:EntityBase2 , IEntity2
        {
            if(destination==null)
            {
                destination = new EntityCollection<T>(EntityFactoryCache2.GetEntityFactory(typeof(TFactory)));
                destination.RemovedEntitiesTracker = new EntityCollection<T>(EntityFactoryCache2.GetEntityFactory(typeof(TFactory)));


and it seems to run correctly (when entity is created manually or prefteched). Is is the right place for you?


Walaa (Support Team)   Posted on: 15-Feb-2017 20:16:21.
Quote:
Is is the right place for you?
I believe so
Otis (LLBLGen Pro Team)   Posted on: 16-Feb-2017 10:09:22.
If the generated method is the same for all entities, you can simply extend commonEntityBase with a partial class and add the method there, no need for extra templates

gilles.marceau (User)   Posted on: 16-Feb-2017 10:35:00.
Don't understand.
Because I want this behavior to be by default, and that GetOrCreateEntityCollection is the method called by the framework, I must find a way to change this method.

And I can't redefine this method in the partial class, can I ?
Otis (LLBLGen Pro Team)   Posted on: 16-Feb-2017 10:57:32.
oh sorry, my bad! Embarrassed I hadn't looked into the code and was assuming that was your method. I don't remember every method in the framework Wink

I do recall there's another way though. I'll look into it.


Otis (LLBLGen Pro Team)   Posted on: 16-Feb-2017 11:08:05.
Yes there's another way which doesn't require template editing Regular Smiley

Create a partial class of HelperClasses\EntityCollection.cs.

In there, override the following method like this:
Code:

protected override void PlaceInRemovedEntitiesTracker(TEntity item)
{
    if(this.RemovedEntitiesTracker == null)
    {
        this.RemovedEntitiesTracker = new EntityCollection<TEntity>();
    }
    base.PlaceInRemovedEntitiesTracker(item);
}



This will make sure that when the removedentitiestracker is needed, there's one, and if it's not needed, none is created. The base class' method will check if there's a tracker and return if there's none. So this is IMHO the best way: when you need it, you'll get a call to that method, you create the tracker if it's not there, and then proceed as normal.
gilles.marceau (User)   Posted on: 16-Feb-2017 16:37:15.
OK I understand.
Unfortunately, I prefer change the template because I would have to write code like this to check if RemovedEntitiesTracker is null before iterate it.

Code:
if (order.OrderDetails.RemovedEntitiesTracker != null)
{
     foreach (OrderDetailEntity orderDetail in order.OrderDetails.RemovedEntitiesTracker)
     {
     }
}


Thanks