Skip to content

Defining Factory Functions for Objects with Multiple Constructors #152

@IrisPeter

Description

@IrisPeter

When I originally needed to create a DI Object which would be passed some arguments I was guided by the "Scaling Doubles Injection Test", and looked at ScalerFactory, however my object didn't have any additional dependencies, so I had no substitute for the Multiplier*, and so I just removed it.

In the old Business Tax Objects system, some of the objects have multiple ::Create methods, so far I've been translating objects that had only one Create Method and therefore also only one private Constructor and so this hasn't been a limitation, when working with fruit.

The object I'm trying to create receives a Factory, as far as I can see, just as you have a Multiplier* parameter in your constructor, I can substitute it for a SomeObjectFactory parameter

using SomeObject = std::unique_ptr<ISomeObject>;

using SomeObjectFactory = std::function<SomeObject(int)>;

However I am unsure on what step to do next to support multiple constructors.

So far I see that there is a 1 to 1 relation ship between constructors and getXXXComponent functions

Does this mean I will need two sets of getXXXComponent functions?

using SomeObjectFactory = std::function<SomeObject(int)>;
using SomeObjectFactoryVer2 = std::function<SomeObject(std::string, int, double)>;

fruit::Component<SomeObjectFactory> getSomeObjectOneParam();
fruit::Component<SomeObjectFactoryVer2> getSomeObjectThreeParams();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions