C # dependency injection

I'm trying to figure out if I understand the injection of depression.

I have a project that is used as a parser. It can parse delimited text, key value and also regular expression.

The first way this was done is with a single function with a switch. The next way was to put it in separate functions and call it based on the switch

The next way I thought was to create an interface and implement a class for each type. Does it work a lot? Will this question be able to function or will it demonstrate benefits that I don’t see yet.

I believe that my problems are related to the fact that I was originally going to implement the interface, and than every time I need another parsing, a new class. But it still requires me to input and add this to some type of logical stream, since I don’t see how to do this with the implementation framework.

So, let's say I add another analysis method based on tags or xml. Create a class that implements the interfaces, and I will need to add them to the flow logic in order to create them as this interface if the user decides to parse this type of text. Any clear way to do this?

+4
source share
4 answers

I think you really need a Factory. A Factory is a class that knows, given some information for work, how to create the necessary objects of the required type. In your case, you must create the analyzer interface, and then separate classes that implement various parsers. Finally, create a Factory parser that, given some ability to determine which type of parser to create, creates and returns the desired look. This is where your logic will be. Factory provides a way to localize the creation logic for created items.

public interface IParser<T> { T Parse<T>( string item ); } public class KeyValueParser : IParser<KeyValue> { KeyValuePair Parse<KeyValue>( string item ); } ... public class ParserFactory { public IParser<T> CreateParser<T>() { var type = typeof(T); if (type == typeof(KeyValuePair)) { return new KeyValueParser(); } ... throw new InvalidOperationException( "No matching parser type." ); } } 

Some others have suggested a plug-in model and, if necessary, Factory can be adapted to read the plug-in configuration, load the appropriate plug-ins, and create instance types as needed. In this case, it would be more appropriate to think of Factory as a “manager," because it does not just create instances.

+2
source

I would say that you are on the right track, but you are a little mistaken. You should go towards the Factory method and the visitor to first solve the parsing problem.

  • Visitors who handle various types of input.
  • The visitors created the mental method Factory.
  • Factory is introduced as a constructor that implements a dependency injection pattern.

This work may be interesting for you - http://www.exciton.cs.rice.edu/research/sigcse05/dp4rdp.pdf

+2
source

What you are describing is only remotely related to Injection Dependency. This, however, is a poster example of the Replace with Polymorphism Refactoring (p. 255) replacement, so this is definitely a good idea.

It becomes only DI if you decide to completely abandon the conditional and instead introduce the Strategy into the user .

+2
source

I am using a file analyzer using the Factory abstract template. And my Factory will just ask for the file extension, and based on this usage strategy (or switch case) I decide which parser should instantiate.

0
source

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


All Articles