0

I use the abstract factory pattern to add runtime dependencies to my object graph (that was created at program startup through a DI framework). Currently my factory stores all dependencies that I do have at compile-time. No I want to add some dependencies through a init() method at runtime. In my DI container I could spread the factory around my program and get the object by calling Factory#create(). Well, I could pass the runtime parameter at runtime (e.g. like Factory#create(String someString) to avoid a leaky abstraction), however I only want to use the runtime parameter to finish the "set up" of the factory, so that I can get the object without providing any runtime parameters again?

Here a simple example for illustration:

public class CarFactory {

    //can be injected at compile-time through DI container
    Wheel wheel;
    Light light;

    public Car create(String carName) {
        return new Car(carName, this.wheel, this.light);
    }
}

At some point (and only there!) in the code [CodePosition1] I will get the runtime parameter, which I can use to finally build the car:

public class SomeClass {
    CarFactory cf;
    ...
    //thanks to the factory I can combine injectables and runtime params
    Car car = cf.create("bmw");
    ...
}

However I want to use that newly created car also at other places, e.g. at a totally differnt [CodePosition2]:

public class CarConsumer() {
    CarFactory carFactory;

    ...
    //I want to get the car that same car I build at [CodePosition1] with 
    //something like the following as I don't have the runtime parameter here
    Car car = carFactory.getCar();
    ...
}

The question is, where is the right place to "store" the new car from [CodePosition1] to use it at other places in the code, e.g. at [CodePosition2] or simply how to provide a dependency that was created at runtime still globally through the DI container? The factory only allows me to create an object where I can supply the runtime information.

The only solution I came up so far is to inject some sort of a context object, and fill this context object with runtime data. But this also leads to temporal coupling leaves the the object inside the context at null during compile time.

muley
  • 41
  • 5
  • Your question is a bit abstract, making it unclear what you're asking. Could you provide examples of what it is you're doing and that demonstrate what you're trying to achieve? – Steven Sep 26 '18 at 07:56
  • @Steven I added an example that focuses on the described problem. I hope you can finally help me out – muley Sep 27 '18 at 00:57
  • Does your application actually handle cars? Please use the actual domain and explain what kind of runtime data you are injection and where it is coming from? Is it request data? – Steven Sep 27 '18 at 05:41
  • You guessed correctly. The car is actually a socket. The problem doesn't change though. I do have the **runtime information** only at one place, but want to make the created object available everywhere else. I described my problem with the actual domain objects here [here](https://stackoverflow.com/questions/52507754/is-it-a-bad-practice-to-set-dependencies-to-null-in-a-ioc-container-and-supply-t/52512677?noredirect=1#comment91999463_52512677). I thought to illustrate my problem from a different perspective, bringing it more to the point with this question. Sorry, if that confused you! – muley Sep 27 '18 at 07:34

0 Answers0