OnInitialized is calling for every Entity

Posts   
 
    
AlexS
User
Posts: 14
Joined: 18-Apr-2016
# Posted on: 26-Jul-2018 12:57:43   

Hello,

i'm Using LLBLGEN Pro 5.4, my Project is self servicing two classes and there are hundreds of Entities in my MS SQL-Database. The project was created a long time ago with LLBLGEN 3 (?) and was updated to never version in the past years.

Now i was updating to 5.4 and i realised, that at first time using of any Entitie there is fired the "OnInitialized" Event of every other Entity.

I create a new Entite with new MyEntity(). And then every "OnInitialized" Event of all other entities are fired.

I extend some of my entities with partial classes, and there i overwrite the OnInitialized event. I use it to preset some values for new Entites. For example:

protected override void OnInitialized()
        {
            base.OnInitialized();
            if (this.IsNew) 
            {
                this.Datum = DateTime.Today;
                this.Status = StatusNeu;
            }

        }

This event is fired every time of first use of ANY other Entitie.

Is this "normal"? I dont know wheather this was bevor 5.4 or not... Maybe it's a bug of mine, but i can't find any. Before i continue to search my bug i should know wheather this behaviour is normal or not simple_smile

Can you help me?

Thanks,

Alex

PS: I use Relationships. But the event is also fired for Entites, they don't have a relationship to my first created entity...

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 26-Jul-2018 19:26:00   

That's not the expected behavior.

I don't see why this could fire events in other entities in memory, but could you please try it out without calling the base method?

        protected override void OnInitialized()
        {
            //base.OnInitialized();
            if (this.IsNew) 
            {
                this.Datum = DateTime.Today;
                this.Status = StatusNeu;
            }

        }
AlexS
User
Posts: 14
Joined: 18-Apr-2016
# Posted on: 27-Jul-2018 11:26:36   

Thank you for your answer Walaa!

It doesn't have an effect when i comment out the base methode.

When I'm creating an Entity, maybe UserEntity, with

var currentUser = new UserEntity();

it comes to "UserEntityBase.cs" , which is generated code from LLBLGEN.

             /// <summary>Initializes the class with empty data, as if it is a new Entity.</summary>
        /// <param name="validatorToUse">Validator to use.</param>
        private void InitClassEmpty(IValidator validatorToUse)
        {
            OnInitializing();
            this.Fields = CreateFields();
            this.Validator = validatorToUse;
            InitClassMembers();

            // __LLBLGENPRO_USER_CODE_REGION_START InitClassEmpty
            // __LLBLGENPRO_USER_CODE_REGION_END
            

            OnInitialized();
        }       

When the debugger comes to the line

this.Fields = CreateFields();

suddenly i come into "OnInitialized" of all other Entities.

I'm sure this is a own-made error. I extended a lot in a lot of Entity classes. But I don't find out what could cause this behaviour. My code is nearly 20 years old wink

Have you any idea what i could try to find my bug? I'm a little at a loss...

Thank you very much,

Alex

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 30-Jul-2018 07:51:51   

Hi Alex,

It's possible to you to isolate the error, or maybe replicate it in a dummy project? This way you could open a new HelpDesk thread (which is private) and attach a .zip file (with just the code) so we can reproduce your error and maybe find the problem.

David Elizondo | LLBLGen Support Team
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 30-Jul-2018 09:37:02   

Where did you add the OnInitialized method override? In a partial class of CommonEntityBase?

A small repro would be good as I don't see what you mean with ' fired the "OnInitialized" Event of every other Entity. ".

So if I do:

var c = new CustomerEntity(); var o = new OrderEntity(); // <<< this raises OnInitialized of 'c' again ? Or just of o ?

Frans Bouma | Lead developer LLBLGen Pro
AlexS
User
Posts: 14
Joined: 18-Apr-2016
# Posted on: 31-Jul-2018 09:16:20   

Thank you for your help!

Otis wrote:

Where did you add the OnInitialized method override? In a partial class of CommonEntityBase?

I do it in partial class of the Entity. For example:

namespace SUMS3.DAL.EntityClasses
{

    public partial class AufgabenEntity : AufgabenEntityBase
    {
        protected override void OnInitialized()
        {
            base.OnInitialized();
        }
    }
}

Otis wrote:

So if I do:

var c = new CustomerEntity(); var o = new OrderEntity(); // <<< this raises OnInitialized of 'c' again ? Or just of o ?

When Entity c is created, OnInitialized of all Entities are fired (also of Entity o). When Entity o is created, only OnInitialized of Entity o is fired. Not from c.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 01-Aug-2018 00:40:45   

Your last statement is so confusing. How on earth would creating c from customer would fire an event on o which doesn't exist (as it is created later). Please attach a repro.

AlexS
User
Posts: 14
Joined: 18-Apr-2016
# Posted on: 03-Aug-2018 12:25:56   

Thank you for your efforts!

I have now created a test project:

  1. a new database with only 2 tables. (SQL-Server) These tables have no relationships, no special features. Just tables with primary keys.
  2. created a new LLBLGEN 5.4.1 project, Database first.
  3. source code generated with TwoClasses SelfService.
  4. both entities extended with partial classes
  5. in this partial classes onInitialized overwritten and a console message written in. 6.. That's it.

My console program now creates an entity of Table1, but it calls the OnInitialized events of BOTH tables. After, i create although a table2-Entitiy and here it only calls OnInitialized from Table 2.

Console program:

static void Main(string[] args)
        {

            Console.WriteLine("Here we go: Now i will create Table1 Entity:");
            var tab1 = new Table1Entity();
            Console.WriteLine("I have just created Table1 Entity");
            Console.WriteLine("");
            Console.WriteLine("Now i will create Table 2 Entity:");
            var tab2 = new Table2Entity();
            Console.WriteLine("I have just created Table2 Entity");
            Console.WriteLine("Now i'm at the end - completely...");
            Console.ReadKey();

        }

My Partial-Class-Extensions: (the same for Table2)

 public partial class Table1Entity : Table1EntityBase
    {
        protected override void OnInitialized()
        {
            base.OnInitialized();
            Console.WriteLine("Here is OnInitialized from Table1");
        }
    }

Output:

Here we go: Now i will create Table1 Entity:
Here is OnInitialized from Table1
Here is OnInitialized from Table2
Here is OnInitialized from Table1
I have just created Table1 Entity

Now i will create Table 2 Entity:
Here is OnInitialized from Table2
I have just created Table2 Entity
Now i'm at the end - completely...

You see: OnInitialized is called for EVERY Entite, for Entity Table1 twice (don't know why). After that it's fired only for created Entities.

That's not normal, is it?

Thank you very much for your help. I add the project files as zip-File.

PS: this behaviour I don't have with LLBLGEN 4.2. I updated from 4.2. to 5.4, and then there was this new "feature" wink

Attachments
Filename File size Added on Approval
lgentest.zip 93,855 03-Aug-2018 12:56.17 Approved
daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 06-Aug-2018 07:53:05   

Indeed. I reproduced it apparently with your code... looking further.

David Elizondo | LLBLGen Support Team
daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 06-Aug-2018 09:03:00   

This is what I got so far. I testes with v5.3 and v5.4:

v5.3 - Adapter: Not Reproduced v5.3 - SelfServicing: Not Reproduced v5.4 - Adapter: Not Reproduced v5.4 - SelfServicing: Reproduced

RTL: 5.4.2.0

Steps to reproduced: 1. Create a Northwind project in LLBLGen v5.4. Set everything normal, and add two entities: Customer and Employee. 2. Generate code using SelfServicing template set. 3. Create a solution with the generated project and add a console application for testing. 4. Create a partial class for each entity to write something on the OnInitialized event override:

namespace NW.V54.EntityClasses
{
    public partial class CustomerEntity 
    {
        protected override void OnInitialized()
        {
            //base.OnInitialized();
            Console.WriteLine("Hello from CustomerEntity.OnInitialized!");
        }
    }
}
...
namespace NW.V54.EntityClasses
{
    public partial class EmployeeEntity
    {
        protected override void OnInitialized()
        {
            //base.OnInitialized();
            Console.WriteLine("Hello from EmployeeEntity.OnInitialized!");
        }
    }
}
  1. Write this on the Program.cs of the Console application:
Console.WriteLine("Start");
var customer = new CustomerEntity();
Console.WriteLine("End");

... the result is:

Start Hello from CustomerEntity.OnInitialized! Hello from EmployeeEntity.OnInitialized! Hello from CustomerEntity.OnInitialized! End

We are looking into this...

David Elizondo | LLBLGen Support Team
AlexS
User
Posts: 14
Joined: 18-Apr-2016
# Posted on: 06-Aug-2018 09:54:40   

Thank you David, for your help! I'm glad the behavior is reproducible. (Hooray, I'm not crazy! Not quite...). simple_smile

I'm looking forward to your solution.

Greetings to Guatemala,

Alex

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 06-Aug-2018 09:59:45   

Adapter also suffers from this issue, but less extreme: it still triggers an additional line to be emitted due to the factory cache initialization but that's about it.

In Selfservicing it indeed emits for all entities in the model a line due to the factory cache initialization, which needs the actual entity type with the factory and to get that type it creates a dummy instance at startup.

The fix will cause a behavior change (all initialized methods are currently called at startup, once, which is undocumented behavior but something some people might rely on), however it's so obscure that we think we can make the change, as it's a bug that needs correcting.

We'll correct it for both selfservicing and adapter (where it happens in a minor way).

In any case, it happens only once, at startup, but still can cause problems if you do a lot of things in OnInitialized...

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 06-Aug-2018 11:29:51   

The 5.4.3 hotfix build we just uploaded contains the fix for this issue. Be sure to re-generate the code and use the latest runtime build for 5.4.3.

Frans Bouma | Lead developer LLBLGen Pro
AlexS
User
Posts: 14
Joined: 18-Apr-2016
# Posted on: 06-Aug-2018 12:37:55   

Thank you very much for the very fast implementation! Now it works as expected.

Thank you,

Alex