This is a proof of concept for a structured content solution for DNN Platform (formerly known as DotNetNuke). This should not be used in a production environment. Anything can change at this stage of development.
Other
13
stars
5
forks
source link
Refactored SQLHelper to use Dependency Injection #59
We rely on an interface instead of a concrete piece of code. We know what the class does without caring about how it does it. This brings flexibility in refactoring that class if need be by editing a single file or to offer multiple ways to do things or to completely replace it say one day SQL server is no longer what we want to use.
Performance and scalability: before this change, instances of this class could have been created in 14 places, with 1000 users within a few minutes that could well be 14000 instances that need to be created and then garbage collected which would consume much memory and CPU cycles. Since this class is almost stateless (the only state it carries is the connection string) I made it a singleton, so only one instance will ever exist. Should the connection string change, it causes a DNN to restart anyway and a new instance will replace it with the new connection string.
Loose coupling: 14 other classes relied on this class, now only the startup class relies on it, the others only know about the interface.
Testing: Should we add unit tests, we can mock this interface with our own implementation built just for testing. This allows us to test the classes that formerly relied on it to be tested in isolation without calling the real implementation that obviously needs a database connection to be available and would fail during tests.
Good example: This is how we want new DNN development to be done and this module could be a great example to show how to do it right.
This has several benefits: