How to inject constructor/properties properly in A

2020-03-27 03:14发布

问题:

I have a class called UsersOnlineModule that is being created from an IHttpModul. Within this class I would like to have two properties injected, I'm using Simple Injector for this.

public class UsersOnlineModule
{
    public ITenantStore tenantStore;
    public ICacheManager cm;

I'm calling this class from an IHttpModule:

Modules.UsersOnline.UsersOnlineModule usersOnlineModule =
    new Modules.UsersOnline.UsersOnlineModule();
usersOnlineModule.TrackUser(app.Context);

However my IHttpModule does not know about the cache manager or tenantStore. I can solve this by getting the objects from the container, however I would prefer not to create a reference to Simple Injector. Is there any other nice option to resolve these two properties without creating references to my container?

-- Update

I modified the example as follows:

class ImportAttributePropertySelectionBehavior : IPropertySelectionBehavior
{
    public bool SelectProperty(Type serviceType, PropertyInfo propertyInfo)
    {
        return typeof(IHttpModule).IsAssignableFrom(serviceType) &&
            propertyInfo.GetCustomAttributes<ImportAttribute>().Any();
    }
}

private static void RegisterHttpModules(Container container)
{
    var httpModules =
        from assembly in BuildManager.GetReferencedAssemblies().Cast<Assembly>()
        where !assembly.IsDynamic
        where !assembly.GlobalAssemblyCache
        from type in assembly.GetExportedTypes()
        where type.IsSubclassOf(typeof(IHttpModule))
        select type;

    httpModules.ToList().ForEach(container.Register);
}

but it's not returning any of my httpmodules.

回答1:

The integration guide shows how to use a HttpModule to initialize HttpHandlers. A HttpModule however doesn't get created per request; it is created once for the application and will register to the HttpApplication's events when its initialize method is called.

Since a module is a singleton, you should not inject dependencies into your module. You should resolve dependencies per request, and since the HttpModule can't be configured and is not a dependency of its own, you will have to call back into the container from within your HttpModule. There's really no other way, but the trick here is to minimize the amount of code needed. Example:

public class UsersOnlineModule : IHttpModule
{
    public void Init(HttpApplication context) {
        context.PreRequestHandlerExecute += (s, e) => {
            var handler = Global.GetInstance<UsersOnlineHandler>();
            handler.Handle();
        };
    }
}

In this example, the UsersOnlineModule does do nothing more than resolving one single service and call its method on it. This service (UsersOnlineHandler in this case) should capture all logic and dependencies needed. So in other words, your HttpModule becomes a Humble Object and all logic is extracted into the UsersOnlineHandler:

public class UsersOnlineHandler
{
    private readonly ITenantStore tenantStore;
    private readonly ICacheManager cm;

    public UsersOnlineHandler(ITenantStore tenantStore, ICacheManager cm) {
        this.tenantStore = tenantStore;
        this.cm = cm;
    }

    public void Handle() {
       // logic here
    }
}

WARNING: Be sure not to store any dependencies into class or instance fields or properties of your HttpModule, because that might cause a dependency to become a Captive Dependency. Instead, as described above, just resolve, use, and forget about that dependency, all inside that method call. Do not store it.