DEV Community

Zachary Hadjah
Zachary Hadjah

Posted on • Edited on

How to begin to set up Data Services file for a Web Application's Database

Between Identity Framework, authentication, authorization, and data services, the .net environment allows developers to build applications with security at the forefront. Seeding Data into a database is essential for developers to test programs. When in development, you can test this seed data without having to depend on your client’s information to test. You can also seed certain types of data into programs such as administrator accounts, moderator accounts, and accounts for other developers who will be working on the program. This sort of information you would want to isolate for two reasons. First, you wouldn’t want to seed data inside the Program.cs file because this would violate the single responsibility principle. The Program.cs should not have the responsibility of seeding data and calling the startup.cs file. The single responsibility principle is strictly enforced to make programs reusable and to simplify error handling. Second, If someone were to be able to access the program using nefarious techniques, sensitive information can be decrypted and stolen. In order to solve both problems, a developer will need to create an entirely separate file for Services. Next, the developer will need to pass an instance of IHost to the DataService.cs.
Alt Text
Alt Text

The Service file will feature a DataServices file whose single responsibility will be to seed data for the program. The instance of IHost will allow different objects of the program to obtain their specific services. The specific services are instantiated implicitly since it is easier for it to take on the form that it’ll be assigned to. Lastly, the variables with their specified services will be passed as parameters into the private asynchronous methods.
Alt Text

Alt Text

Alt Text

This method of data managment is known as a wrapper method, or in design terms, the adapter pattern. This pattern allows two interfaces, both with separate responsibilities, to interact with each other. The first interface (the client) will call another interface (the adaptee) in order to complete a service such as providing data, or instantiating a certain number of objects via its private static classes. This method of seeding an administrator and a moderator inside an adaptee is a top tier security technique. If need be, this design pattern can also be used for other services in order to instantiate more objects via multiple incompatible interfaces.

This is just the skeleton of the implementation. As the cohort goes along, our cohort will properly configure interfaces and properly implement dependency injection.

Top comments (0)