简体   繁体   English

使用依赖注入的正确方法来连接控制器

[英]Correct way to connect controllers using Dependency Injection

If i have a controller that receives and processes the action selected by the user, and then want to use another controller to store all database related logic, what is the correct way to connect these controllers while allowing the 2nd controller to interact with the database context. 如果我有一个控制器可以接收和处理用户选择的操作,然后想使用另一个控制器来存储所有与数据库相关的逻辑,那么在允许第二个控制器与数据库上下文进行交互的同时,连接这些控制器的正确方法是什么? 。

At the moment I have it working with creating a database context in the first controller and then parsing that to the database controller when I connect the two using DI, but hopefully someone could show me the correct way to do this. 目前,我可以在第一个控制器中创建数据库上下文,然后在使用DI连接两者时将其解析到数据库控制器,但希望有人可以向我展示正确的方法。

  public class TestController : Controller
{
    private readonly DatabaseContext context;
    private Database.UserController userDatabaseController;

    public TestController(DatabaseContext db)
    {
        context = db;
        userDatabaseController = new Database.UserController(context);
    }

}

database controller 数据库控制器

public class UserController : Controller
{
    private readonly DatabaseContext context;

    public UserController(DatabaseContext ctx)
    {
        context = ctx;
    }
    public bool RegisterUser(Models.DatabaseModels.UserModel model)
    {
        try
        {
            context.Users.Add(model);
            context.SaveChanges();
            return true;
        }
        catch (Exception e)
        {
            return false;
        }

    }
}

startup.cs startup.cs

  services.AddDbContext<DatabaseContext>
            (options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

databasecontext 数据库上下文

 public class DatabaseContext : DbContext
{
    public DatabaseContext(DbContextOptions<DatabaseContext> options)
       : base(options)
    { }
    public DbSet<DatabaseModels.UserModel> Users { get; set; }
}

The "correct" way is: you don't. “正确”的方法是:您没有。 A controller should never directly call into another controller. 一个控制器绝对不能直接调用另一个控制器。 You can redirect to a new route that maps to a different controller, but that's it. 您可以重定向到映射到其他控制器的新路由,仅此而已。 If you have some common logic that needs to be shared, then that should be factored out into a completely different class that both controllers can utilize. 如果您有一些需要共享的通用逻辑,则应将其分解为两个控制器可以利用的完全不同的类。

If you're finding that you need to call Controller methods from another Controller, you probably need to refactor your code. 如果发现需要从另一个Controller调用Controller方法,则可能需要重构代码。 Controllers should have very little logic in them, which usually just involves calling a Service layer and then constructing a ViewModel from the data. 控制器中应该只有很少的逻辑,通常只涉及调用服务层,然后从数据构造ViewModel。

My advice would be to do some reading on the Service Layer pattern and the Repository pattern (sometimes called the Manager pattern). 我的建议是对服务层模式存储库模式 (有时称为管理器模式)进行一些阅读。

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

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