Home>

"Your MVC is wrong (https://www.slideshare.net/MugeSo/mvc-14469802)"
I saw the article, but I can't chew it, so I applied it to the tool I'm making for studying now.
I would like you to explain. . .
* I think that there are many explanation shortages, so I would like to add it if you can point out.

[Tools under construction]
○ Function
→ Download the uploaded file as a new file by replacing the file contents based on the contents registered in the DB

○ Configuration
ASP.NET MVC5
Visual Studio 2017 community
Use entity fremwork to create a DB with code first
sql server 2016

○ Current coding
・ Model
→ DB model created with code first (has replacement ID and replacement contents in properties) and dbContext

・ View
→ Display model replacement contents, upload file with input tag file

・ Controller
→ Get uploaded file from view with argument,
Receive replacement ID and contents from DB (model), replace file contents and download

For now, most of the work is done on the controller and the model is in the data storage state.
How should we allocate work?

Thanks for your advice m (_ _) m

  • Answer # 1

    Make everything a model can do.
    However, the model must not reference a view. Don't let it do work that you can't do without knowing about the appearance of the view.
    Inevitably such work will be that of the controller.

    The bloated controller means that the appearance and business logic depend on each other, and the separation is not progressing.

    MVC's role is to separate the appearance and business logic as much as possible, so controller bloat is a testament to MVC not working.

    Don't ask people how to allocate specifically.
    Instead of allocating, separate the view and the model.
    The remaining part that cannot be separated is the controller.

  • Answer # 2

    I felt like I was worried about how to design the Model.

    The explanation I often use is that Model is a "modeling artifact" when it is designed object-oriented.
    Start by defining domains (actual tasks) with objects without thinking about systematization.

    And then you can see that "Replace the file contents based on the contents registered in the DB" is a model candidate.
    If it is a Converter object,

    output = Converter.doConvert (input)

    You can write like

    .

    View is the part that displays output, and Controller is the part that receives the file from View and calls the above method. Model is the Converter object itself.

    Although it is a bit old, please refer to my article below.
    [Miscellaneous] I'd like to say something about the MVC model soon. , You should work inside that object.

    It is better not to perform processing that involves logic such as processing on external objects.

    So in this case, the model will process the data.

    As for the reason that the object should process the attribute of the object, if the same processing is required in other places, it leads to the result of describing the same processing, and pays a huge amount of cost for maintenance such as correction Because it results.

    In this case, if you need to create the same download file at another location (controller), do you write the same process? You will be forced to choose.
    There are cases where it is possible to cut out at this timing, but if development is at the end, it may be difficult for a project to do so.

    By the way, here is a supplement.

    The display control itself by conditional branching on the view side is also performed on the model side to determine the condition.

    Although it will be out of MVC, if you try to develop with only 3 types of MVC objects, you will end up with a fat object somewhere. Often, the controller or model tends to become fat.

    So, if you think that processing with a certain amount of logic is necessary, do not stick to MVC, think in the original object orientation, use layered architecture, domain drive, MVC etc. appropriately and develop I think that is good.

  • Answer # 3

    Thank you for "MVC memorable MVC".

    First, what is a "design pattern" in programming and what purpose does it introduce?
    If you impose "constraints" when creating something, something with a similar purpose will be made into a similar shape.
    At the programming language level, it is encapsulation, object orientation, or procedural.

    The design pattern of MVC was conceived for Alt, a window system,
    A model for applications with a user interface.

    I think it's unreasonable to apply this to a website.
    The role of the controller is unclear.

    "It's MVC's role to separate appearance and business logic as much as possible"
    I think you're writing such a mess,
    The original idea of ​​MVC is a GUI application design policy that tends to be complicated. Oh.

    Of course, there are other design patterns besides MVC.
    For web programming, you can easily specify the upload file name,
    If you ’re submitting to the next page, upload the next page,
    Write to replace DB contents and send download file,
    If the file is uploaded, replace it with the DB contents and create a file.
    If the file has been created, download it "or
    "When the user presses the submit button, a series of processing is executed" or
    It is only necessary that some pattern is consistent in the program code. I think.

    So those who care about MVC can't judge good design patterns by themselves,
    Even if you can't explain why, if you follow the authoritative MVC, there is no doubt.
    I think it ’s just blindly and irresponsibly blaming the wrong programming pattern.
    Is there anyone who became happy with MVC in the web system?

    MVC has been divided into three parts, but if you are on the web, recently asynchronous communication with JavaScript is not applicable,
    If the UI is based on screen transitions, how about user input, its check, action to be executed, backend processing backend, output to the user, error processing error? Yeah.