In our code we have some classes that represents business logic. Prior to usage, they are created and populated with two types of inputs:
- business data required for execution (e.g.
- infrastructure references to e.g. database, message bus, http client... etc, anything that is not related to business, but needed for communication with underlaying infrastructure.
We have the following options to separate inputs:
/A have everything in constructor:
public Foo(String name, DbRef db)
I would like to be able to separate somehow these two types of inputs. As number of arguments can be big, I do not want to overload constructor or to make it with huge number of arguments.
/B infra in constructor, data via setters
public Foo(DbRef db)... public Foo setName(String name)...
This way you can't create an object without passing the infra, but you need to add extra validation if some required argument is not missing (there is no compile-time check for that).
/C required data in constructor, infra and optional data via setters
public Foo(String name)... public Foo bind(DbRef dbref)... public Foo setMaxValue(int maxValue)...
This way you must not forget to call
bind, but at least it is always the same method - and this binding can be done automatically by e.g. proxy, probably, to reduce possible human errors.
I have started with B but now I am leaning towards the C.
How would you architecture this?