• author: Nick Chapsas

Creating Clean Startup Code with SOLID and IConfiguration in .NET

In this video, Nick discusses how to have a clean startup code with SOLID OCS - a class used to register services or configure our pipeline in .NET code. The focus is on registering services in a clean way.

Creating an IInstaller Interface

To achieve cleaner startup code, Nick suggests creating an IInstaller interface with a void method called InstallServices. This method will have two parameters: IConfiguration and IServiceCollection.

Creating an Installers Folder

To begin, Nick creates a folder for the Installers. Inside this folder, he creates two installers - one for MVC and one for Data. The MVC installer will implement the IInstaller interface, and the Data installer will also implement the IInstaller interface.

Moving the Code

Next, Nick moves the MVC and data registration code to their respective installers. In the process, he removes the pre-existing code from the previous method.

Extracting the Listing of Lines

After creating separate classes for MVC and data installers, Nick suggests using the activator class to create an instance of all installers. With the use of Assembly and ExportedTypes, he creates instances of all classes implementing IInstaller. Then, each installer is installed (using the install method in the end) in a single line of code.

Installer Extensions

Finally, Nick creates an InstallerExtensions class, which is a static class. Along with an InstallServices method, InstallServicesInAssembly is also created. Nick shows that everything works by running the code.

Using Installer Extensions, all configuration's respective codes can go in their configuration installer, making startup code much cleaner.

Conclusion

Using IInstaller interface and installers can help achieve cleaner startup code. All the configuration services can be stored in their respective configuration installers, making it easy to understand.

Previous Post

Introducing the New C Sharp Experience in Visual Studio Code

Next Post

How to Properly Check for Null in C

About The auther

New Posts

Popular Post