1

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?

1 Answer 1

1

"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.

Sign up to request clarification or add additional context in comments.

3 Comments

It only seems to auto-inject into the controller class for some reason not other classes. Any idea why?
I think it's more of a design decision. in some situations, injecting directly into action methods would be convenient but at the end of the day, it depends on the whole style and architecture of the application
but for methods like 'CheckAccessToServer' as you mentioned in your question, it seems like a cross-cutting concern and I suggest creating a custom action filter attribute for this kind of method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.