How can i register an open generic type with a complex type constraint in Simple Injector?

oguzh4n

I have an open generic type AccessMessageHandler<TProcess> which I want to resolve every time that an IProcessHandler<AccessMessage<TProcess>> is resolved. How can I do this?

This is my code:

public interface IProcess {}

public interface IProcessHandler<in TProcess> where TProcess : IProcess {
    void Handle(TProcess message);
}

public class AccessMessage<TProcess> : IProcess where TProcess : IProcess {
    public virtual string Username {get;protected set;}
    public virtual TProcess InnerProcess { get; protected set; }
}

public class AccessMessageHandler<TProcess> : IProcessHandler<AccessMessage<TProcess>> 
    where TProcess : IProcess {
    public AccessMessageHandler(IProcessHandler<TProcess> innerHandler){}
    public void Handle(AccessMessage<TProcess> message){
      // access control
      _innerHandler.Handle(message.InnerProcess)
    }
}

public class JustDoIt : IProcess {
    public virtual string What {get;set;}
}

public class JustDoItHandler : IProcessHandler<JustDoIt> {
    public void Handle(JustDoIt message) {
      // handle
    }
}

How can i register IProcessHandler, AccessMessageHandler for Simple Injector resolve like below:

var accessMessageProcess = new AccessMessage<JustDoIt>()
{ 
    Username = "user", 
    InnerProcess = new JustDoIt() { What="xxx" }
};

var handler  = GetHandlerFor(accessMessageProcess); 

// must return AccessMessageHandler<JustDoIt>(JustDoItHandler)
handler.Handle(accessMessageProcess);
Steven

You can do the following registration:

container.RegisterManyForOpenGeneric(
    typeof(IProcessHandler<>), 
    typeof(JustDoItHandler).Assembly);

container.RegisterOpenGeneric(
    typeof(IProcessHandler<>), 
    typeof(AccessMessageHandler<>));

The call to RegisterManyForOpenGeneric will search the assembly of the JustDoItHandler and looks for all public concrete (non-generic) implementations of IProcessHandler<TProcess>. In the end this is just the same as doing a bunch of manual calls to container.Register<IProcessHandler<SomeProcess>, SomeProcessHandler>().

The call to RegisterOpenGeneric maps an open generic abstraction to an open generic type. It uses unregistered type resolution on the background, so every time a IProcessHandler<TProcess> is requested that is not registered explicitly (using RegisterManyForOpenGeneric for instance), an AccessMessageHandler<TProcess> is resolved (if the generic type constraints match).

The following code can be used to resolve the object graph and execute the handler:

var handler = container.GetInstance<IProcessHandler<AccessMessage<JustDoIt>>>();

handler.Handle(accessMessageProcess);

This should resolve the following graph:

IProcessHandler<AccessMessage<JustDoIt>> handler = 
    new AccessMessageHandler<JustDoIt>(
        new JustDoItHandler());

Do note though that the AccessMessageHandler<TProcess> is not a decorator. A decorator wraps the same type as it implements, but your AccessMessageHandler<TProcess> implements IProcessHandler<AccessMessage<TProcess>> but wraps IProcessHandler<TProcess>. I think the right name for this pattern is a proxy.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Register open generic type as fallback with Simple Injector

From Dev

Register open generic type as fallback with Simple Injector

From Dev

Register instance creator for generic type with Simple Injector

From Dev

Binding an open generic type to a method in Simple Injector

From Dev

Binding an open generic type to a method in Simple Injector

From Dev

How can i register a type with its interfaces as singleton using simple injector?

From Dev

Simple Injector: Registering open generic type with constructor parameter

From Dev

How to register generic with two types in Simple Injector?

From Dev

How can I refactor a simple class member to a more complex type?

From Dev

Simple Injector - Register foreach type with parameter

From Dev

How do I register a generic type in MVVMCross

From Dev

Why can't I use a Guid as a generic type constraint?

From Dev

Can Simple Injector register multiple instances of same type using different constructor parameters?

From Dev

Generic type in generic constraint

From Dev

Generic type in generic constraint

From Dev

Simple Injector register multiple type of same interface with metadata

From Dev

Register decorator conditional based on consumer service type in Simple Injector

From Dev

How can i define in xsd that a simple/complex type contain another XML?

From Dev

How can I get a generic type annotation

From Dev

Auto-Register Generic Repositories with Simple Injector

From Dev

How can I check for array type (not generic type) in Kotlin

From Dev

Generic type constraint and variance

From Dev

Generic type constraint not applying

From Dev

Generic constraint of the same type

From Dev

How to constraint a generic type to have new()?

From Dev

How to register a generic type for all of its interfaces

From Dev

Can't use function type as generic type constraint in Swift

From Dev

How can I pass generic type to generic methode?

From Dev

injector.getInstance of a generic type

Related Related

  1. 1

    Register open generic type as fallback with Simple Injector

  2. 2

    Register open generic type as fallback with Simple Injector

  3. 3

    Register instance creator for generic type with Simple Injector

  4. 4

    Binding an open generic type to a method in Simple Injector

  5. 5

    Binding an open generic type to a method in Simple Injector

  6. 6

    How can i register a type with its interfaces as singleton using simple injector?

  7. 7

    Simple Injector: Registering open generic type with constructor parameter

  8. 8

    How to register generic with two types in Simple Injector?

  9. 9

    How can I refactor a simple class member to a more complex type?

  10. 10

    Simple Injector - Register foreach type with parameter

  11. 11

    How do I register a generic type in MVVMCross

  12. 12

    Why can't I use a Guid as a generic type constraint?

  13. 13

    Can Simple Injector register multiple instances of same type using different constructor parameters?

  14. 14

    Generic type in generic constraint

  15. 15

    Generic type in generic constraint

  16. 16

    Simple Injector register multiple type of same interface with metadata

  17. 17

    Register decorator conditional based on consumer service type in Simple Injector

  18. 18

    How can i define in xsd that a simple/complex type contain another XML?

  19. 19

    How can I get a generic type annotation

  20. 20

    Auto-Register Generic Repositories with Simple Injector

  21. 21

    How can I check for array type (not generic type) in Kotlin

  22. 22

    Generic type constraint and variance

  23. 23

    Generic type constraint not applying

  24. 24

    Generic constraint of the same type

  25. 25

    How to constraint a generic type to have new()?

  26. 26

    How to register a generic type for all of its interfaces

  27. 27

    Can't use function type as generic type constraint in Swift

  28. 28

    How can I pass generic type to generic methode?

  29. 29

    injector.getInstance of a generic type

HotTag

Archive