开发者

What's the convention to name the 3 different types of model that exist in ASP.NET MVC? (controller input, controller output and persistence)

开发者 https://www.devze.com 2023-03-26 04:10 出处:网络
What\'s the convention to name the 3 different types of mode开发者_如何转开发l that exist in ASP.NET MVC? (controller input, controller output and persistence)

What's the convention to name the 3 different types of mode开发者_如何转开发l that exist in ASP.NET MVC? (controller input, controller output and persistence)

Visual Studio, while MVC 3 scaffolding, puts a "Model" suffix to every model class but I'm not sure this is a good approach not to differentiate the type of model.

A secundary question I'd like to ask is how to name these 3 different types of model. I'm not the model that enters the view is called view-model. The persistent can be called persistence model. But what about the controller input model?


I usually use *ViewModel for the Controller output model and *Form for the Controller input model.

For persistence (domain models/entities) I don't use any special naming convention.


I rather like the way it's given in here. It distinguishes between Controller input models which query and those which call service layers (repositories etc). I'm starting to adopt the following:

  • Use *ViewModel for Controller output models.
  • Use *Query for Controller input models where you are just en-capsulizing parameters used in a query.
  • Use *Command for Controller input models which call Service layers.

A couple of examples:

// Example of Controller query input model
public ActionResult Search(SearchProductsQuery query)
{
    ProductSearchViewModel searchView = query.ExecuteWith(productsRepository);
    return View(searchView);
}

// Exmaple of Controller command input model
public ActionResult Create(CreateProductCommand command)
{
    if (command.Validate(this.ModelState))
    {
        var newProduct = command.CreateProduct();
        productRepository.Add(newProduct);
    }
    return View();
}


I have a User domain entity, which has a ChangePassword method that accepts a ChangePasswordInput class and returns a ChangePasswordResult class. ChangePasswordInput is also used as controller input and view model of the ChangePassword view (form), and ChangePasswordResult is used as view model of the ChangePasswordSuccess view.

0

精彩评论

暂无评论...
验证码 换一张
取 消