Blazor Server: Dependency Injection into LLBLGEN generated Class Library

Posts   
 
    
Blex
User
Posts: 3
Joined: 15-Dec-2023
# Posted on: 15-Nov-2024 09:32:28   

Hello LLBLGEN Team,

I have been using LLBLGEN for 20 years, and my project has grown quite large (WinForm). Now, I’m migrating it to Blazor Server – the LLBLGEN class library will be maintained as much as possible to continue using my custom code.

So, my Blazor Server project uses the class library that is generated with LLBLGEN 5.11. LLBLGEN Pro Runtime Framework, Self Service, Two Classes, Netstandard 2. Library name: DAL (By the way, maybe I should upgrade it to .NET 8?…)

In the DAL class library, I’ve added a custom class called “SessionService.” It handles login and stores some session data. This class is registered in:

Program.cs as a Scoped Service:

builder.Services.AddScoped<SessionService>();

Within the Blazor app, Dependency Injection works fine.

However, I also need this Scoped Service in my entity classes. How can I achieve Dependency Injection into the LLBLGEN class library (DAL) from the Blazor level?

I tried adding a static variable to the CommonEntityBase. This actually works, but in a multi-user environment, a static solution is not ideal...

Why do I need this? In CommonEntityBase, I’ve extended the “OnSave” event to log some data, including the username. This username is stored in the SessionService, and I would like to access it within the event. However, I cannot extend the constructor or event with parameters (Dependency Injection).

Do you have any tips for me? Thank you very much!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39760
Joined: 17-Aug-2003
# Posted on: 17-Nov-2024 09:14:50   

We don't have any experience with Blazor server, so not sure if this helps, but e.g. in the documentation there's an example of an Authorizer: https://www.llblgen.com/Documentation/5.11/LLBLGen%20Pro%20RTF/Using%20the%20generated%20code/gencode_authorization.htm#example-1-employeeauthorizer which uses the Thread.CurrentPrincipal, which is set at authentication and available everywhere. Tho not sure this works in a web based scenario where threads are recycled.

With extending the OnSave event with parameters you mean, injecting the service into the method like asp.net does with dependency injection ?

Frans Bouma | Lead developer LLBLGen Pro
Blex
User
Posts: 3
Joined: 15-Dec-2023
# Posted on: 18-Nov-2024 10:48:31   

Hello Otis,

Thank you for your response! Yes, I mean dependency injection by method. Of course, using the constructor would be better (constructor injection). I could change the entity template in LLBLGen to achieve that — but I’m still hesitant to do so simple_smile

At the moment, I've solved it like this: I’ve added a property in CommonEntityBase:

public SessionService sessionService { get; set; }

This way, each entity has this property and can access it. I just have to remember to set it after initialization:

LogEntity log = new LogEntity();
log.sessionService = sessionService;

I would like to solve this more elegantly, so I don't have to add this line for every new entity. And if I create a new entity in my class library, I have to remember to pass the SessionService through the parameters. Not very elegant...

Perhaps, the LLBLGen Pro Runtime Framework with SelfService two classes isn’t optimal for my approach.

I will take a closer look at your suggestion with the thread example. However, Singleton and Static are not suitable in a Blazor Server app with a multi-user environment since each user would have access and could potentially overwrite these values.

Thank you for your efforts, Alex

Walaa avatar
Walaa
Support Team
Posts: 14986
Joined: 21-Aug-2005
# Posted on: 19-Nov-2024 01:38:25