IoC and .NET framework

I want to know what is the best practice with the IoC pattern when working with .NET.

For example, should I create SqlConnection / OracleConnection or any other provider through an IoC container or with a simple new keyword?

Does my class share any value with specific provider types (including when I want to use only one provider type)?

+4
source share
5 answers

It can make a difference for unit testing if you use IDbConnection and all other classes instead of specific classes.

Besides IOC and the like, I actually used DbFactoryProvider ( additional information ) several times to create my connections and other database related objects and read the provider through ConnectionString.

The main problem with the database material is that usually you cannot use only ANSI-SQL with the database, therefore, while you are separated from specific classes, your sql will not be migrated. (i / e limit in MySql or Over and Partition on Sql Server).

About DI / IOC with other things that were not related to the database, perfectly separate your classes and remove dependencies, and also help with unit testing, say, when you have a service that you work with. This is useful, even if not in the testing unit, when you are working against the service, and the other team is still developing the service, you can create a fake service that basically solves your problems (not all) so that you can work against anything before a real service is available.

There are many more examples, but working with services (database repository / web / authorization / whatever) is the first simplest added value.

0
source

There is more to code maintenance than simply replacing one provider with another. For me, dependency injection is more about logical code separation and thus more support than future code verification on the day the vendor changes.

DI also makes it easier to use code from one project to another, as it makes the dependency between different parts more explicit.

However, I never used the IOC container, and I never saw the need for it, so I can not comment on this aspect of the question.

But you should definitely remove the implicit dependencies from your code as much as possible, just so that the code is reused, maintained and correct.

+2
source

Enabling a SqlConnection or IDbConnection will be useless because DbConnection is a fuzzy abstraction. You can only use it to call stored procedures or simple queries like SELECT * FROM VIEW . Any more complex queries will be different for the SQL dialect.

You will have a better change after merging the connection itself behind a higher level abstraction, for example, IUserRepository or some kind of. By default, the implementation of this interface is likely to be SqlUserRepository , which will communicate with MS SQL Server or OracleUserRepository , which communicates with Oracle.

Even better would be to move away from the low-level ADO.NET DbConnection API and go to O / RM, such as LINQ to SQL or Entity Framework. Then you will usually have LinqToSqlUserRepository .

Please note that in this case I'm still talking about XXXUserRepository . The interface is still unchanged. In other words: IUserRepository not a leaky abstraction. You can replace this interface for unit testing, although this is hardly possible with SqlConnection .

+1
source

IOC exists, so although you want to use one type of provider in fact, someday something will change, and you may need an additional adapter, so it is better to use IoC instead of creating an object using a new keyword. IoC exists for the flexibility to change things over time (with changing requirements).

0
source

It would be nice to use IoC even for .NET classes. This will allow your code to be bound only to the interface or base class, if available. Not only that, but you can use your IoC framework to specify constructor arguments. This may be useful for connection strings in SqlConnection.

It should also be mentioned that using IoC and writing code just for the interface will simplify unit testing. This is especially true when working with DB connections.

0
source

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


All Articles