简体   繁体   English

Rest API C# (ASP.NET Core) 中的设计问题

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

So iv been put in charge of refactoring a really old code base.所以 iv 负责重构一个非常旧的代码库。 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).代码绝对是一团糟,所有功能代码都写在 controller class 中,没有服务类没有存储库类(Controller 是 6000 行代码)。

Now I have started by creating service classes that are Auto injected into the controller methods when they are called as follows:现在我已经开始创建服务类,当它们被调用时自动注入到 controller 方法中,如下所示:

[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.我习惯于使用 spring/JPA/Annotations 但在 C#(Net core)中我真的不知道如何以好的方式做到这一点。 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.这是服务 class(仅相关部分)的示例,它使用存储库 class,使用“新建”创建每个存储库 class,我认为它看起来很糟糕。

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?正如您所注意到的,自动注入到 controller 中的 DB object 被发送,并且每个 repo 都是使用前面提到的“New”语句制作的? 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?我可以通过自动注入在服务中创建回购类吗?如果可以,如何创建?

"can I have the DB auto injected into the service classes and how?" “我可以将数据库自动注入到服务类中吗?如何?”

Yes, if you have added DbContext in startup class like this:是的,如果您像这样在启动 class 中添加了 DbContext:

services.AddDbContext<YourDbContext>

then you can inject 'YourDbContext' in any service, repo, controller, etc.然后你可以在任何服务、回购、controller 等中注入“YourDbContext”。

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.一个建议是不使用“ApiMapper”,而是为每个回购类(ServiceLocationsRepository、StationsRepository 等)创建接口,然后在启动 class 中注册这些接口,然后将它们注入到您的服务类中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM