Unity - Inject an object to Constructor by calling a Method of a Class

stack247

I have the following interface and its implementation

public class DummyProxy : IDummyProxy
{
    public string SessionID { get; set; }
    public DummyProxy(string sessionId)
    {
        SessionId = sessionId;
    }
}

public interface IDummyProxy
{
}

Then I have another class to get a session id

public class DummySession
{
    public string GetSessionId()
    {
        Random random = new Random();
        return random.Next(0, 100).ToString();
    }
}

Now, in my Unity container, I would like to inject 'session id' to DummyProxy every time the container is trying to resolve IDummyProxy. But this 'session id' must be generated from DummySession class.

container.RegisterType<IDummyProxy, DummyProxy>(
    new InjectionConstructor(new DummySession().GetSessionId()));

Is this even possible?

Lukazoid

Your best approach for this would be to make use of an InjectionFactory, i.e.

container.RegisterType<IDummyProxy, DummyProxy>(new InjectionFactory(c => 
{
    var session = c.Resolve<DummySession>() // Ideally this would be IDummySession
    var sessionId = session.GetSessionId();

    return new DummyProxy(sessionId);
}));

An InjectionFactory allows you to execute additional code when creating the instance.

The c is the IUnityContainer being used to perform the resolve, we use this to resolve the session and then obtain the session id, from that you can then create your DummyProxy instance.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Javascript

Calling a class method from the constructor

From Java

Calling method from constructor

From Java

Calling virtual method in base class constructor

From Java

Is method reference calling constructor of ArrayNode class under the hood?

From Dev

Calling Constructor with in constructor in same class

From Dev

Calling a class's method as default arg in constructor

From Dev

Calling ES6 class constructor from class static method

From Dev

Kotlin: calling outer class method in object expression

From Dev

find class object calling specific method

From Dev

attributeError when calling method of object class

From Dev

Calling a constructor from method within the same class

From Dev

Calling a void Method into a constructor

From Dev

Calling an object in constructor in other method/function

From Dev

Unity inject complex constructor object in app.config

From Dev

Calling a class method through an object instance

From Dev

Calling an object and its method from another object class method

From Dev

Calling a method on a constructor method

From Dev

Calling a constructor as a method

From Dev

Inject super class to raw object by generating constructor

From Dev

Inject constructor parameters using Unity

From Dev

Class constructor or static object-returning method?

From Dev

calling an Object constructor from my class

From Dev

Calling class constructor later in another class method in C++

From Dev

The difference between function, class, method, object and constructor?

From Dev

Calling an object of a method of a class within another method of the same class

From Dev

Javascript - Class - Method Returning Undefined When Calling Constructor Property

From Dev

Calling constructor of parent class in static method of child class

From Dev

What is the purpose of calling the constructor of the object class in Java?

From Dev

passing method reference in object and calling it raising the problem , the constructor inject dependency coming as undeifined

Related Related

  1. 1

    Calling a class method from the constructor

  2. 2

    Calling method from constructor

  3. 3

    Calling virtual method in base class constructor

  4. 4

    Is method reference calling constructor of ArrayNode class under the hood?

  5. 5

    Calling Constructor with in constructor in same class

  6. 6

    Calling a class's method as default arg in constructor

  7. 7

    Calling ES6 class constructor from class static method

  8. 8

    Kotlin: calling outer class method in object expression

  9. 9

    find class object calling specific method

  10. 10

    attributeError when calling method of object class

  11. 11

    Calling a constructor from method within the same class

  12. 12

    Calling a void Method into a constructor

  13. 13

    Calling an object in constructor in other method/function

  14. 14

    Unity inject complex constructor object in app.config

  15. 15

    Calling a class method through an object instance

  16. 16

    Calling an object and its method from another object class method

  17. 17

    Calling a method on a constructor method

  18. 18

    Calling a constructor as a method

  19. 19

    Inject super class to raw object by generating constructor

  20. 20

    Inject constructor parameters using Unity

  21. 21

    Class constructor or static object-returning method?

  22. 22

    calling an Object constructor from my class

  23. 23

    Calling class constructor later in another class method in C++

  24. 24

    The difference between function, class, method, object and constructor?

  25. 25

    Calling an object of a method of a class within another method of the same class

  26. 26

    Javascript - Class - Method Returning Undefined When Calling Constructor Property

  27. 27

    Calling constructor of parent class in static method of child class

  28. 28

    What is the purpose of calling the constructor of the object class in Java?

  29. 29

    passing method reference in object and calling it raising the problem , the constructor inject dependency coming as undeifined

HotTag

Archive