locked
Structure of folder in MVC RRS feed

  • Question

  • User-1439110336 posted

    Hi there,

    I have one questions about structure of folderin ASP.NET MVC. Let's assume that I'm writing an application and I have a few methods that perform some fnctions like getting data, edit or other things. 

    Should I create this methods in inside appropriate controler or create new folder and throw it in there ? 

    I understand that in Model folder i have only class with properties describing the columns in the database.

    Thanks for the answer.

    Wednesday, July 17, 2019 7:50 PM

Answers

  • User-821857111 posted

    Some people view the Model as being anything that isn't the controller or view. So they put their data access code in there too. There's no reason why you can;t do that too, but as your application grows, you will feel like separating code out into more logical units. In larger applications, you might even separate your data access etc into separate projects.

    There's no right or wrong answer, just what works for you. You will probably try several different approaches to code organisation until you find one that makes sense to you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 17, 2019 8:29 PM
  • User1120430333 posted

    Hi there,

    I have one questions about structure of folderin ASP.NET MVC. Let's assume that I'm writing an application and I have a few methods that perform some fnctions like getting data, edit or other things. 

    Should I create this methods in inside appropriate controler or create new folder and throw it in there ? 

    I understand that in Model folder i have only class with properties describing the columns in the database.

    Thanks for the answer.

    IMHO, you should consider Seperation of Concerns.

    https://en.wikipedia.org/wiki/Separation_of_concerns

    https://www.c-sharpcorner.com/UploadFile/56fb14/understanding-separation-of-concern-and-Asp-Net-mvc/

    A layered or n-tier style implements SoC.

    https://docs.microsoft.com/en-us/previous-versions/msp-n-p/ee658117(v=pandp.10)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 17, 2019 9:29 PM
  • User-474980206 posted

    folders are cheap, and are the main organization of most IDE's. so common folders most of my projects end up with

    accessors - mappers be datastraore and models
    helpers - utility and extension methods
    models - data models
    viewModels - MVC view models

    once a projects has various concerns, I create a folder for each, and add the above to each concern as needed. never be afraid to add a folder and move code to that folder. except for view razor files, MVC doesn't care what scheme you use. Any scheme you use should make it easier to find your code. 

    I also feel free to create projects as soon as possible. obviously data models belong in their own project, so that can be shared. data accessors also belong in their own project, so they can be shared, but not in the model project, as all projects may not use the same accessors.

    for security, you generally need to do n-tier, so the UI project, calls an eternal webapi/microservice to access data-stores and  corporate resources. I usually start the main business logic webapi server as soon as I create the UI projects.

    if I need a httphandler, or .net core middleware, I often create a separate project for the code when I need to reuse.

    again be agile, create organization as your code grows. as sizable chunks of code develop, think about moving to there own library. This helps with code isolation.  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 17, 2019 10:34 PM

All replies

  • User-821857111 posted

    The convention is to create a separate folder for data access code such as you describe and put your classes there. 

    Wednesday, July 17, 2019 7:57 PM
  • User-1439110336 posted

    The convention is to create a separate folder for data access code such as you describe and put your classes there. 

    OK. So in Model folder just poco classes with properties, and in other new folder I will write my class with methods, which that are called from controller ?  

    Wednesday, July 17, 2019 8:09 PM
  • User-821857111 posted

    Some people view the Model as being anything that isn't the controller or view. So they put their data access code in there too. There's no reason why you can;t do that too, but as your application grows, you will feel like separating code out into more logical units. In larger applications, you might even separate your data access etc into separate projects.

    There's no right or wrong answer, just what works for you. You will probably try several different approaches to code organisation until you find one that makes sense to you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 17, 2019 8:29 PM
  • User1120430333 posted

    Hi there,

    I have one questions about structure of folderin ASP.NET MVC. Let's assume that I'm writing an application and I have a few methods that perform some fnctions like getting data, edit or other things. 

    Should I create this methods in inside appropriate controler or create new folder and throw it in there ? 

    I understand that in Model folder i have only class with properties describing the columns in the database.

    Thanks for the answer.

    IMHO, you should consider Seperation of Concerns.

    https://en.wikipedia.org/wiki/Separation_of_concerns

    https://www.c-sharpcorner.com/UploadFile/56fb14/understanding-separation-of-concern-and-Asp-Net-mvc/

    A layered or n-tier style implements SoC.

    https://docs.microsoft.com/en-us/previous-versions/msp-n-p/ee658117(v=pandp.10)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 17, 2019 9:29 PM
  • User-474980206 posted

    folders are cheap, and are the main organization of most IDE's. so common folders most of my projects end up with

    accessors - mappers be datastraore and models
    helpers - utility and extension methods
    models - data models
    viewModels - MVC view models

    once a projects has various concerns, I create a folder for each, and add the above to each concern as needed. never be afraid to add a folder and move code to that folder. except for view razor files, MVC doesn't care what scheme you use. Any scheme you use should make it easier to find your code. 

    I also feel free to create projects as soon as possible. obviously data models belong in their own project, so that can be shared. data accessors also belong in their own project, so they can be shared, but not in the model project, as all projects may not use the same accessors.

    for security, you generally need to do n-tier, so the UI project, calls an eternal webapi/microservice to access data-stores and  corporate resources. I usually start the main business logic webapi server as soon as I create the UI projects.

    if I need a httphandler, or .net core middleware, I often create a separate project for the code when I need to reuse.

    again be agile, create organization as your code grows. as sizable chunks of code develop, think about moving to there own library. This helps with code isolation.  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 17, 2019 10:34 PM