Dependency Injection Frameworks

Posts   
 
    
cerberis
User
Posts: 93
Joined: 20-May-2011
# Posted on: 12-May-2016 16:03:41   

Hello,

I have found an old post about DI/IOC framework usage together with LLBLGenPro. https://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=22397&HighLight=1

Its quite old post, so maybe something has been changed?

Do you consider enabling some kind of support for them? For example it would be nice to have ability to create Auditor or Authorizer instances through it. However I do not want to override half of the framework in order to do this. Also I do not want to create entities through it. I still like the way to use new Entity(); But I guess it should be a way where you do your custom dependency injection to replace with another object/class?

Also I do not want to use Ioc container directly from these classes as it violates layers and code responsibilities.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 17-May-2016 08:28:58   

You can use external frameworks, with little extensions / modifications, as pointed by the link you posted.

Why do you want to use external DI framework? and What framework do you want to use?

David Elizondo | LLBLGen Support Team
cerberis
User
Posts: 93
Joined: 20-May-2011
# Posted on: 17-May-2016 09:40:51   

Well... Basically it should not matter why and what... My all project uses DI everywhere. Its abstracted well enough to use any framework.

So for you I guess what is needed to change is in DependencyInjectionInfoStorage class, method:

private static object GetInstanceForInjection(InjectionInfo info)

I am pretty sure it is possible to change Activator with some kind of interface and use static property to access it (and to have possibility to replace it on application startup).

for example (code is simplified, not tested, written here, so might be some mistakes, maybe that's not the only place) (LLBLGenPro v4.2)


public interface IInstanceFactory 
{
     object Create(Type instanceType);
}

public class DefaultInstanceFactory : IInstanceFactory
{
    public object Create(Type instanceType)
    {
        return Activator.CreateInstance(instanceType);
    }
}

public static class DependencyInjectionInstances
{
    private static IInstanceFactory _factory = new DefaultInstanceFactory();
    public static IInstanceFactory Factory 
    {
        get { return _factory ?? new DefaultInstanceFactory(); }
        set { _factory = value; }
    }
}

// lets change your class
internal class DependencyInjectionInfoStorage 
{
     // ...... other methods here

        // lets modify this (see: now I can use any DI container here)
    private static object GetInstanceForInjection(InjectionInfo info)
    {
        object instance;
        if(info.ContextType == DependencyInjectionContextType.Singleton)
        {
            if(!_instanceCache.TryGetValue(info.InstanceType, out instance))
            {
                // not there
                instance = DependencyInjectionInstances.Factory.Create(info.InstanceType);
                _instanceCache.Add(info.InstanceType, instance);
            }
        }
        else
        {
            instance = DependencyInjectionInstances.Factory.Create(info.InstanceType);
        }
        return instance;
    }
}