Does constructor size matter if you use control inversion?

So, I have maybe 10 objects, each of which has 1-3 dependencies (which, I think, well, as far as free communication is concerned), but also some parameters that can be used to determine the behavior (timeout, size windows, etc.).

Now, before I started using the Inversion of Control container, I would create a factory and, possibly, even a simple ObjectSettings object for each of the objects that require more than 1 installation in order to preserve the size of the constructor in the recommended "size less than 4". Now I use the inverse of the control container, and I just don't see anything in common with this. Of course, I can get a constructor with 7 parameters, but who needs it? All this is filled with IoC anyway.

Am I missing something or is it mostly correct?

+4
source share
5 answers

The relationship between the complexity of the classes and the size of the IoC constructor did not occur to me before reading this question, but my analysis below shows that having a large number of arguments in the IoC constructor is a smell of code that should be remembered when using IoC. With the goal of sticking to a short list of constructor arguments, you can simply save classes. Following the principles of single responsibility , you will lead you to this goal.

I am working on a system that currently has 122 classes that are created using the Spring.NET framework. All relationships between these classes are set in their constructors. Admittedly, the system has its share of less than perfect code, where I violated several rules. (But hey, our failure is an opportunity to learn!)

The constructors of these classes have a different number of arguments, which I show in the table below.

Number of constructor arguments Number of classes 0 57 1 19 2 25 3 9 4 3 5 1 6 3 7 2 8 2 

Classes with zero arguments are either specific strategic classes or classes that respond to events by sending data to external systems.

Those with 5 or 6 arguments are somewhat inelegant and may use some refactoring to simplify them.

Four classes with 7 or 8 arguments are great examples of God's objects . They must be broken, and each of them is already included in the list of problems in the system.

The rest of the classes (from 1 to 4 arguments) (mostly) are simply developed, understandable, and comply with the principle of single responsibility

+6
source

The need for many dependencies (possibly more than 8) may indicate a design flaw, but in general I think there is no problem if the design is cohesive.

Also, consider using a service locator or a static gateway for infrastructure issues such as logging and authorization, rather than cluttering constructor arguments.

EDIT: 8 is probably too much, but I thought there would be a strange case for him. Looking at Lee's post, I agree, 1-4 is usually good.

+2
source

G'day george

First, what are the dependencies between the objects?

A lot of "isa" relationships? A lot of "hasa" relationships?

A lot of fans? Or a fork?

George's response: "has basically been trying to follow the lineup over the inheritance advice ... why does it matter, though?"

Like basically "hasa", you should be fine.

It’s better to make sure that your design (and destruction) of the components is done correctly, although to prevent memory leaks.

And if it's in C ++, make sure you use virtual destructors?

+1
source

This is a difficult question, and why do I prefer a hybrid approach, when the corresponding properties are mutable, but only the immutable properties and the required dependencies without a useful default are part of the constructor. Some classes are built with the necessary elements, and then configured if necessary through setters.

+1
source

It all depends on which container you used to execute the IOC, and what goes with the container, whether it accepts annotations or a configuration file to saturate the object to be initialized. Also, if your constructor options are just primitive data types, then this is not very important; however, if you have non-primitive types, then, in my opinion, you can use DI based on properties, and not DI based on consultations.

0
source

Source: https://habr.com/ru/post/1277091/


All Articles