在ASP.NET MVC中组织现实世界中的应用程序代码时有些失落 [英] A bit lost with organizing real world application code in asp.net mvc

查看:145
本文介绍了在ASP.NET MVC中组织现实世界中的应用程序代码时有些失落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下真实世界的场景,为使本示例稍为简化

I have the following real world scenario, somewhat simplified for the sake of this example

我有一个对象,我们称其为Movie,它将包含多个属性,例如

I have an object, let's call it Movie which will consist of several attributes, such as

  • 发布日期
  • 演员(数组)
  • 类型
  • 评分

我需要具有一个可以输入新电影的表单,该表单上包含以下元素:

I need to be able to have a form where a new movie can be entered, with the following elements on the form:

  • 日期日历
  • 与演员一起下拉列表
  • 从下拉列表中选择流派
  • 星际评价领域

使用asp.net mvc来组织我的代码的清晰便捷的方法是什么,请概述在何处

What would be a clear consice way to organise my code using asp.net mvc, please outline where

  • 数据访问逻辑不断发展
  • 业务逻辑(验证等)
  • 我想在这里使用ViewModel概念

到目前为止,我有

  • 电影模型
  • MovieViewModel视图模型
  • IMovieRepository接口

但是我不清楚参与者/体裁数组如何适合这个,以及我在哪里获取数据....它是否进入IMovieRepository接口?我是否要为其创建另一个接口,换句话说,是否要为ViewModel创建一个接口?我也可以创建一个接口来获取流派吗? 另一个问题: 如何使用ViewModels?我需要在应用程序设置中进行任何更改吗?

But I am unclear how does the actors/genres arrays fit into this and where do I fetch the data for it....does it go into IMovieRepository interface? Do I create another interface for it, in other words do I create an interface for a ViewModel? Do I create an interface for fetching genres too? Another question: How do I use ViewModels? Do I need to change anything in the application settings?

控制器动作在其主体中具有类似View()的东西....如何在其中传递ViewModel?我需要吗?

Controller action has something like View() in their body....how do I pass ViewModel there? Do I need to?

总而言之,我只想举一个简单的示例,说明如何实现上述方案.

All in all, I just want a simple example of how you would implement the scenario above.

我是MVC的新手,并试图确保我的代码井井有条.

I am new to MVC and trying to make sure my code is organized well.

推荐答案

我记得Mike Cohn关于敏捷的一个很好的说法,"不存在最佳实践"

I remember very good word from Mike Cohn about Agile,"Best Practice Is Not Exist"

因此,您不仅应始终针对代码,而且还应针对您的设计,体系结构,方法论等,不断进行改进和重构,为此,您将需要以下内容:

So you should always keep improving and refactoring not only for your code but also for your design, architecture, methodology, etc. and to do this You will need the following:

  • 要求所有工作都将可维护性作为无功能要求
  • BDD(行为驱动的开发)
  • TDD(测试驱动设计)
  • TDD(测试驱动开发)
  • 具有适当代码覆盖率的单元测试
  • 自动化构建部署和测试(对所有重复活动的完全自动化)

我知道它有点长,但是有必要了解我为什么我会建议您按照以下方法进行操作

I know it a bit long introduction, but it necessary to understand me why I will advise you doing my approach as the following

我在MVC项目中的默认方法如下

My default approach in MVC project as the following

  • 通过以下方式使用映射层进行映射的平面ViewModel: 映射库
  • 域模型考虑DDD指令
  • 将控制器作为服务使用的服务层 业务逻辑
  • 服务层和工作单元使用的存储库
  • Flatten ViewModel that mapped using mapping layer by using mapping library
  • Domain Model Consider DDD instructions
  • Service Layer that working with controllers as services business logic
  • Repositories that used by the service layer and unit of work

但是,正如我告诉您的那样,不存在最佳实践,因此我将使用BDD和TDD开始开发,并为实现这一点而创建并创建一个框架"

But as I told you, Best Practice is Not Exist so I will start my development using BDD and TDD, and to implement this I founder and create a framework "DevMagicFake" that published on CodePlex, this framework will enable me to finish and complete my view and make it real working without any design or code for the underline layers at all

该功能适用​​于涵盖大多数行为的所有单元测试之后,我将开始重构整个

After the feature is working with all unit tests that cover most of it's behaviors, I will start refactoring the whole

  • ViewModel
  • 映射
  • 服务
  • 存储库

对于每个重构,我都会运行所有必要的单元测试,以了解重构是否破坏了我的代码或破坏了应用程序的可接受行为以及已知行为,如果发生,我将修复任何破坏

and for each refactor I run all needed unit tests to find-out if my refactor break my code or breaking the accepted and knowing behaviors of the application, if it happen I will fix any break

例如,要保存一个客户并找回它,我将在每个操作方法中仅使用一行代码,如下所示:

So for example to save a Customer and retrieve it I will use only one line of code in each action method like the following

public ActionResult List(CustomerVeiwModel customerVeiwModel)
    {    
       var repository = new FakeRepository<CustomerVeiwModel >();
       repository.Save(customerVeiwModel);

要检索客户,我只需要编写以下代码:

And to retrieve the customer I just need to code the following:

var repository = new FakeRepository<CustomerVeiwModel>();
var  customer = repository.GetById(1);

因此,我总是在两点后决定ViewModel,存储库,体系结构等的决定

So I always take the decisions of the ViewModel, Repository, Architecture, etc, after 2 points

  • 功能已完成并担任客户或业务专家 预期
  • 我的单元测试涵盖了所有功能行为和响应
  • Feature completed and worked as the customer or the business expert expected
  • I have unit tests that cover all the feature behaviors and response

这将使我知道如何设计,开发架构,并使我确信我的代码确实可以按客户期望的那样高质量地工作.

This will make me aware of what to do about design, develop architecture and make me confidence that my code really work with high quality and as the customer expected

最后,只有一个字,我总是保持重构和重构,我身上发生的每一个新功能,修改,问题或问题都可能导致新的体系结构概念或设计决策,从而改变整个应用程序,而我随时为他们准备.

At the end, There is just one word, I always keep refactor and refactor, every new feature, modification, problem or issue happen to me it may lead to new architecture concept or design decisions that will change the whole application and I am always ready for them.

通过您可以在CodePlex上下载使用我的方法表格 DevMagicFake 的MVC3项目,您将找到项目称为" TryFakeMVC3 "

by the way you can download MVC3 project that use my approach form DevMagicFake on CodePlex, you will find project called "TryFakeMVC3"

这篇关于在ASP.NET MVC中组织现实世界中的应用程序代码时有些失落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆