Dependency Injection With ICollectionEntityCore Interface On LLBLGen v5.9

Posts   
 
    
bimar
User
Posts: 12
Joined: 17-Oct-2017
# Posted on: 23-May-2022 23:24:58   

Dear LLBLGen Team,

We would like to consult you about a issue that we cannot to implement with LLBLGen v5.9.1.

Development environment like this;

Oracle DB, generate source code target framework is .NET Standard, project IDE for source code is Visual Studio 2022 and LLBLGen version 5.9.1

We aim to use DLL's created by LLBLGen in projects with different frameworks(like .NET Framework 4.8 and .NET 6). So we needed a dependency injection mechanism in LLBLGen. Our main goal was to read the information in the HttpContext object from 2 separate projects using common LLBLGen DLLs. You can see how we progressed below and we used the following document which does the following.(https://www.llblgen.com/Documentation/5.3/LLBLGen%20Pro%20RTF/using%20the%20generated%20code/gencode_usingdi.htm#instance-type-example)

We added property named as "RotaHttpContextToUse" in IEntity interface (It's inside one interface that created by us with seperated project IRotaHttpContext)

public interface IEntityCore : IEditableObject, IActiveContextParticipant, ITransactionalElement
{
    IAuditor AuditorToUse { get; set; }
    
    // We added this property 
    IRotaHttpContext RotaHttpContextToUse { get; set; }

   // other LLBLGen codes

And we implemented the property here in the following class EntityCore;

public abstract partial class EntityCore < TFields >: IEntityCore, INotifyPropertyChanged, IEntityCoreInternal, IXmlSerializable, IDataErrorInfo, ISerializable, IDeserializationCallback
where TFields: IEntityFieldsCore {
    // ...
    // some codes

    private ITypeDefaultValue _typeDefaultValueProvider;
    private IAuthorizer _authorizerToUse;
    private IAuditor _auditorToUse;
    private IRotaHttpContext _rotaHttpContextToUse; // we added this line

    [Browsable(false)]
    public IRotaHttpContext RotaHttpContextToUse {
      get {
        return _rotaHttpContextToUse;
      }
      set {
        _rotaHttpContextToUse = value;
      }
    }

    //some codes

We added the following to the global.asax file of our project ( WebForm created with .net fw 4.8 ) in Application_Start method for register our class;

RuntimeConfiguration.SetDependencyInjectionInfo(new List<Assembly>() { typeof(RotaHttpContext).Assembly },new List<string>() { "Interface48Imp" });

And RotaHttpContext class looks below (this class generated by target framework .net framework 4.8 with WebForm);

[DependencyInjectionInfo(typeof (IEntityCore), "RotaHttpContextToUse")]
public class RotaHttpContext: IRotaHttpContext 
{
  //some codes
}

With this implementation we were able to read property from RotaHttpContext in runtime. As you can see below we need this class on CommonEntityBase.cs and with above DI mechanism we can access GetUserId property in RotaHttpContext class.

RotaHttpContextToUse.GetUserId()

Everything was as we wanted so far. This implemantation worked for Entity classes. Now we want to use the GetUserId() property (in RotaHttpContext) for collection objects as well. For this purpose we added below code inside to IEntityCollectionCore

IRotaHttpContext RotaHttpContextToUse { get; set; }

And changed the part below to;

 [DependencyInjectionInfo(typeof(IEntityCollectionCore), "RotaHttpContextToUse")]
 public class RotaHttpContext : IRotaHttpContext 
{
  //some codes
}

With this change, we thought that we could use this property in collection classes as well. But it didn't turn out as we expected. We cannot access to GetUserId property because RotaHttpContextToUse always comes up NULL. I think for collection classes we didn't implement DI mechanism but I don't undertand why.

Below is the part we are trying to access from the collection class.

public void SetTransferObjects(Rota.DataTransfer.DokKonsKonteynerYukuTransferObjectList transferObjectList)
{
        var rotaHttpContextToUse = RotaHttpContextToUse.GetUserId();
       //some codes
}

We couldn't understand why we couldn't use this method over Collection classes as it is in Entity classes, we request your help on this matter.

My CustomerId: 19501

Best regards.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39006
Joined: 17-Aug-2003
# Posted on: 24-May-2022 08:45:28   

First of all, it's really not recommended to alter framework interfaces. If you need to use the DI system, please add your code to a partial class of CommonEntityBase instead, as you can then use future runtime versions without the necessity of porting code over. The DI system will check the exposed properties on the full type, not IEntityCore, for injection

Our DI system is invoked when entity classes are instantiated, not when collections are instantiated, so the property you added to the collection isn't set by the DI system

What I wonder is why don't you use a singleton for the httpcontext? ASP.NET's HttpContext is also a singleton after all. It also frees you from having references to a singleton ASP.NET object at the entity level (which is really not where it belongs)

Frans Bouma | Lead developer LLBLGen Pro
bimar
User
Posts: 12
Joined: 17-Oct-2017
# Posted on: 26-May-2022 14:05:03   

The criteria and directions you specify are so important to us. Now we understand why we can't DI in the section in collection and after your mail we tried a number of different solutions.

We decided to manage the HttpContext information in the runtime by creating a project that supports multi-target framework (.net framework 4.8 and .net 6) and referencing it to the related projects (WebForm and .NetCore WebApi). We proved it with a little POC study.

Thank you for your valuable support Otis