Mr.Gomer
Mr.Gomer

Reputation: 649

Rest API design issues in C# (ASP.NET Core)

So iv been put in charge of refactoring a really old code base. The code is a absolute mess where all the functional code is written in the controller class, no service classes no repository classes (Controller is 6000 lines of code).

Now I have started by creating service classes that are Auto injected into the controller methods when they are called as follows:

[HttpGet]
[Route("")]
public IActionResult CheckAccessToServer([FromServices] CheckServerAccessService _checkServerAccessService)
        {
            return _checkServerAccessService.CheckAccessToServer(_env);
        }

But when it comes to the repository classes I cant find any conventional/good way of doing it. I'm used to working with spring/JPA/Annotations but in C# (Net core) I really don't know how to do this in a good way. This is a example of a service class (only relevant part) that uses a repository class using "New" to create each repo class and I think it looks terrible.

public dynamic ApiMapper(string method, dynamic paramsObject, DbContext db)
        {
            HelpMethods.GetNetworkTimeStamp("Start", 2);
            dynamic returnData = null;
            string error = null;

            switch (method)
            {
                case "getservicelocations":
                    returnData = new ServiceLocationsRepository(db).GetServicelocations();
                    break;
                case "getstationsinservicelocation":
                    returnData = new StationsRepository(db).GetStationsInServiceLocation(paramsObject);
                    break;
                case "addstation":
                    returnData = new StationsRepository(db).AddStation(paramsObject);
                    break;
                case "updatestation":
                    returnData = new StationsRepository(db).UpdateStation(paramsObject);
                    break;
                default:
                    error = "Unknown method";
                    break;
            }

As you can notice the DB object that is Auto injected into the controller is sent along and each repo is made with the "New" statement as mentioned before? Any way this could be cleaned up? can I have the DB auto injected into the service classes and how? can I create the repo-classes in the services by auto-injection and if so how?

Upvotes: 1

Views: 234

Answers (1)

Meysam
Meysam

Reputation: 580

"can I have the DB auto injected into the service classes and how?"

Yes, if you have added DbContext in startup class like this :

services.AddDbContext<YourDbContext>

then you can inject 'YourDbContext' in any service, repo, controller, etc.

One suggestion would be instead of using 'ApiMapper', create interfaces for each of your repo classes (ServiceLocationsRepository, StationsRepository, ...) and then register those interfaces inside the startup class then inject them inside your service classes.

Upvotes: 1

Related Questions