A bit lost with organizing real world application code in asp.net mvc

妖精的绣舞 提交于 2019-12-23 17:52:30

问题


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

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

  • release date
  • actors (array)
  • genre
  • rating

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

  • date calendar
  • drop down list with actors
  • grop down list with genres
  • rating field with stars

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

  • data access logic goes
  • business logic goes (validation etc)
  • I would like to use ViewModel concept here

So far I have

  • Movie model
  • MovieViewModel view model
  • IMovieRepository interface

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?

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.

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


回答1:


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:

  • Put the maintainability as a none function requirement at the begging of all your work
  • BDD (Behavior Driven Development)
  • TDD (Test Driven Design)
  • TDD (Test Driven Development)
  • Unit Test with appropriate code coverage percentage
  • Automate your build deploy and test (Full automation of all repeated activities)

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

My default approach in MVC project as the following

  • 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

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
  • Mapping
  • Service
  • Repository
  • etc.

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);

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.

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



来源:https://stackoverflow.com/questions/8127144/a-bit-lost-with-organizing-real-world-application-code-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!