如何使用ASP.NET MVC通用控制器来填充正确的模型

26 浏览
0 Comments

如何使用ASP.NET MVC通用控制器来填充正确的模型

我提了一个关于ASP.NET MVC通用控制器的问题,这个答案展示了一个类似这样的控制器:

public abstract class GenericController 
    where T : class
{
    public virtual ActionResult Details(int id)
    {
        var model = _repository.Set().Find(id);
        return View(model);
    }
}

这可以这样实现。

public class FooController : GenericController
{
}

现在当有人请求/Foo/Details/42时,实体会从_repository的Set()中获取,而无需在FooController中编写任何内容。

他的解释很好,但是我想为产品和客户开发一个通用控制器,我不想使用EF加载产品和客户模型,而是使用MS数据访问应用程序块。

public abstract class GenericController 
        where T : class
    {
        public virtual ActionResult Details(int id)
        {
            //var model = _repository.Set().Find(id);
            var model =customer.load(id);
            或者
            var model =product.load(id);
            return View(model);
        }
    }

所以当请求像/Customer/Details/42或/Product/Details/11这样的时候,通用控制器的details方法将被调用,但是我们如何检测请求来自哪个控制器,并相应地实例化正确的类来加载正确的模型。

如果请求是针对客户的,则需要从通用控制器的details动作方法中加载客户详细信息,如果请求是针对产品的,则需要从通用控制器的details动作方法中加载产品详细信息。

如何使用泛型来获取类型为T的数据集(Entity Framework数据块)?

0
0 Comments

使用ASP.NET MVC Generic Controller填充正确的模型的问题出现的原因是:

1. 控制器直接与数据访问层耦合,使用了具体的数据访问上下文类YourEFContext。

2. 控制器直接返回数据库实体,而不是使用视图模型进行返回。

3. 控制器直接进行数据访问,没有将数据访问层抽象为存储库模式。

4. 控制器实例化了数据访问层,而不是通过依赖注入来获取数据访问层的实例。

解决这些问题的方法是:

1. 使用存储库模式将数据访问层抽象出来,使控制器不直接与具体的数据访问层耦合。

2. 使用依赖注入来获取数据访问层的实例,而不是在控制器内部实例化。

3. 使用视图模型来封装数据库实体,控制器返回视图模型而不是数据库实体。

4. 将数据访问操作移动到服务层,服务层同时包含业务逻辑,并通过存储库模式来抽象数据访问。

最终目标是将MVC模式与服务层和存储库模式结合起来,实现以下架构:

[ MVC ] <=> [ Service ] <=> [ Repository ]

View ViewModel Controller BusinessModel BusinessLogic DataModel Database

在这个架构中,控制器只与服务层进行交互,通过服务层来进行创建/读取/更新/删除业务模型,并在控制器中进行视图模型和业务模型之间的映射。服务层包含业务逻辑和业务模型,并与数据模型进行映射,并通过存储库来持久化模型。

使用Generic Controller的主要目标是实现代码的重用性,并减少重复的代码编写。通过使用泛型控制器,可以减少针对不同实体的控制器的编写,提高开发效率。同时,通过使用存储库模式和视图模型,可以实现控制器的解耦和数据的封装,提高代码的可维护性和可测试性。

0
0 Comments

ASP.NET MVC Generic Controller is a convenient way to handle CRUD operations for multiple models in a consistent manner. However, there are concerns about placing data-access and business logic directly in controllers as the application size and complexity grows. This article explores the reasons behind this concern and suggests a solution.

The main reason for not placing data-access and business logic in controllers is that it violates the separation of concerns principle. As the application grows, controllers become bloated and difficult to maintain. To address this, it is recommended to create repositories that handle data-access and abstract the technology away from the consumers. This separation allows for better maintainability and testability of the application.

To further separate the business logic from controllers, a service layer can be introduced. The service layer acts as a bridge between repositories and controllers, delegating data-access to repositories and containing the business logic. By using services, controllers can remain thin and focused on handling user interactions.

To glue the layers together and achieve loose coupling, an Inversion of Control container is recommended. This container manages the dependencies between layers and allows for easy swapping of implementations if needed.

To implement this approach, a search for "c# mvc repository and service pattern" will yield many examples and resources. It is important to note that in the example mentioned, the author returns view models from services instead of domain models. This can be adjusted to fit the specific needs of the application.

In summary, the use of ASP.NET MVC Generic Controller is not recommended as the application grows in size and complexity. Instead, repositories and a service layer should be used to separate data-access and business logic from controllers. This approach improves maintainability and testability of the application.

0
0 Comments

ASP.NET MVC Generic Controller的主要目标是为了使控制器的代码更加可重用和通用化。通过使用泛型控制器,我们可以避免在每个实体的控制器中重复编写相似的代码,从而提高开发效率。

使用ASP.NET MVC泛型控制器的主要优点有:

1. 减少代码重复:通过定义一个通用的控制器类,我们可以在不同的实体控制器中重用相同的代码,从而减少代码的重复性,提高代码的可维护性。

2. 更好的代码组织:通过将通用的逻辑放在基类控制器中,我们可以更好地组织代码的结构,使代码更易于阅读和理解。

3. 简化控制器的开发:使用泛型控制器可以简化控制器的开发过程,减少开发时间和工作量。

4. 提高代码的可测试性:通过将通用逻辑放在基类控制器中,我们可以更容易地编写单元测试代码,提高代码的可测试性。

通过上述内容可以得出ASP.NET MVC Generic Controller的主要目标是为了使控制器的代码更加可重用和通用化。通过使用泛型控制器,我们可以避免在每个实体的控制器中重复编写相似的代码,从而提高开发效率。

0