What is the best strategy for user input-dependent injection?

I have used quite a lot of dependency injection, but I want to get input on how to process information from the user at runtime.

I have A class that connects to the com port. I allow the user to choose the com port number. Now, I use the com port parameter as a constructor parameter. The reason is that the class cannot run without that information, and its implementation is specific (this The analog version of the class does not require a com port).

Another method is to use the “start” method to receive the com port, or have the property to set the com port. This makes it very compatible with the IoC container, but from It does not necessarily make sense from a class perspective.

It seems that logical routing conflicts with the dependency injection design, but this is because my UI is getting information about a specific type of class.

Other alternatives include using an IoC container, which allows me to pass in additional constructor parameters, or just build the classes I need at the top level without using dependency injection.

Is there a common problem for this type of problem? Accepted standard mode?

According to your needs, you can choose two routes.

< p>1. Connect the UI directly to your specific class

This is the simplest choice, but it is completely acceptable in many cases. Although you may have a domain model that contains a large number of interfaces and uses DI, the UI It forms the combined root of the object graph, where you can simply connect your specific classes, including the port number parameters you need.

The advantage is that this method is simple and easy to understand, easy to understand and implement.

The disadvantage is that your flexibility will be reduced. You will not be able to replace another implementation at will (however, you may not need this flexibility).

Even if you lock the UI to be specific Implementation, this does not mean that the domain model itself is not reusable in other applications.

2. Add an abstract factory

Another option is to add another layer of indirection. It can Use abstract factories to create instances instead of letting UI create classes directly.

The Create method of the factory can take port numbers as input, so this abstraction belongs to the best UI sublayer.

< p>

public abstract class MyFactory
{
public abstract IMyInterface Create(int portNumber);
}

Then you can add your The DI container is connected to the implementation of this factory, which uses the port number and passes it as a constructor parameter to your actual implementation. Other factory implementations may just ignore the parameter.

The advantage of this method is You will not pollute your API (or your specific implementation), and you can still program the interface flexibly.

The disadvantage is that it adds another layer of indirection.

I have used quite a lot of dependency injection, but I want to get input on how to process information from users at runtime.

I have a connection to com The class of the port. I allow the user to choose the com port number. Now, I use the com port parameter as a constructor parameter. The reason is that the class cannot run without that information, and its implementation is specific (an analog version of this class No need for com port).

Another method is to use the “start” method to receive the com port, or have the property to set the com port. This makes it very compatible with the IoC container, but from a class perspective It doesn’t necessarily make sense to see it.

It seems that logical routing and dependency injection The design conflicts, but this is because my UI is getting information about a specific type of class.

Other alternatives include using an IoC container, which allows me to pass in additional constructor parameters, or just build at the top level I need the class without dependency injection.

Is there a generally accepted standard pattern for this type of problem?

According to your needs, you can choose two routes.

1. Connect the UI directly to your specific Class

This is the simplest choice, but in many cases it is completely acceptable. Although you may have a domain model that contains a large number of interfaces and uses DI, the UI forms the composite root of the object graph. Simply connect your specific class, including the port number parameter you need.

The advantage is that this method is simple and easy to understand, easy to understand and implement.

The disadvantage is your flexibility The performance will be reduced. You will not be able to replace another implementation at will (however, you may not need this flexibility).

Even if the UI is locked to a specific implementation, this does not mean that the domain model itself is in another Not reusable in the application.

2. Add an abstract factory

Another option is to add another layer of indirection. It can use an abstract factory to create an instance instead of letting the UI directly Create a class.

The Create method of the factory can take the port number as input, so this abstraction belongs to the best in the UI sublayer.

public abstract class MyFactory
{
public abstract IMyInterface Create(int portNumber);
}

Then you can connect your DI container to the implementation of this factory, which uses Port number and pass it as a constructor parameter to your actual implementation. Other factory implementations may just ignore the parameter.

The advantage of this method is that you will not pollute your API (or your specific Implementation), and you can still program the interface flexibly.

The disadvantage is that it adds another layer of indirection.

Leave a Comment

Your email address will not be published.