locked
Learning Core, MVC and Data Access RRS feed

  • Question

  • User-795286723 posted

    Noob alert. 

    I'm slowing learning Net Core on the weekends as a hobby and had some (really fundamental) questions...

    Does the actual Model store data? (just the Model, no DB backend etc...) The 'concept' appears to just be a class of var/objects, so I want to say yes it does...

    If it does, how do I 'load' it?

    For instance, I've pieced together an async service to routinely consume a Json Web API. I've built a 'JsonModel' to represent my Json data and then in the service I have:

    Rootobject model = JsonSerializer.Deserialize<Rootobject>(MyJsonContent, options);

    (Rootobject being the main JsonModel class...)

    When I run this, I can now see my deserialized Json data, matching my JsonModel, in the 'model' var. Is this data now in the JsonModel or just in my var using the 'template' of the JsonModel and I need to do something else to actually set it into the JsonModel for later retrieval elsewhere?

    Or... am I just completely misunderstanding the concept of the 'Model'...

    Thank you in advance for the guidance, pointers, anything...

    CE

    Sunday, March 28, 2021 3:48 PM

Answers

  • User475983607 posted

    Or... am I just completely misunderstanding the concept of the 'Model'...

    I'm not sure if you understand or not.  A model, the way you are user a model, is just a data structure in memory; a C# class instance.  A model can be serialized which just means the data structure in memory is converted into a byte array.  Commonly serialized data is a string with a standard data format like JSON.  It can also be binary.  Typically data is serialized so it can be transmitted over the wire one byte at a time.    Deserializing is the opposite process.  A string is converted into a data structure and stored in memory.

    A model can also refer to types that map to persistent storage in the case if Entity Framework.  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 28, 2021 5:11 PM
  • User-1545767719 posted

    Does the actual Model store data? (just the Model, no DB backend etc...)

    The answer is no as it seems that the "store" means a persistent storage such as database or files available at the server side. Web application including ASP.NET is basically stateless.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 29, 2021 1:14 AM
  • User379720387 posted

    No.

    A model is a predetermined group of properties that you will use as a tool throughout your code to move data from one place to another, it is more of a type than a container.

    A model is a temporary subset of your data that you will use as a tool. I think roughly you can think of it as a View Model.

    Any page in your website will have a Model or Models associated with it.

    There are two types of models: Entity Models and View Models.

    Roughly speaking an Entity Model looks like a table in your db (provided it is normalized) with properties and information on how this data relates to other tables.

    A View Model takes a subset of data from an Entity Model or more than one Entity and groups it together.

    Table Customers:

    custId, firstName, lastName, address, city, state zip  == Entity Model  (think Entity Framework)

    That same data plus their Locations and the sales per Location == View Model

    Here is a good video that shows you how Models are used.

    https://www.youtube.com/watch?v=_qjR25bEsWs

    You might as well learn Blazor at this point

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 1, 2021 8:53 PM
  • User-821857111 posted

    MVC is a presentation pattern. It is one way of implementing some kind of separation of concerns in UI-based application development. The V is the View or UI, the C is the controller, and the M represents the rest of the application i.e. everything that isn't the View or the Controller. MVC doesn't concern itself with the actual composition of the Model. All it cares about is that it should be separated from the View, and the the controller is the primary separation mechanism.

    Basically, MVC says that you shouldn't have database access code in your views, or be serializing data into JSON in your views etc, which are things that people can do (and actually do) when they aren't implementing some kind of logical separation of concerns in their application. 

    There are countless other patterns that can be applied to the design, implementation and management of the model itself.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 2, 2021 5:51 AM

All replies

  • User475983607 posted

    Or... am I just completely misunderstanding the concept of the 'Model'...

    I'm not sure if you understand or not.  A model, the way you are user a model, is just a data structure in memory; a C# class instance.  A model can be serialized which just means the data structure in memory is converted into a byte array.  Commonly serialized data is a string with a standard data format like JSON.  It can also be binary.  Typically data is serialized so it can be transmitted over the wire one byte at a time.    Deserializing is the opposite process.  A string is converted into a data structure and stored in memory.

    A model can also refer to types that map to persistent storage in the case if Entity Framework.  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 28, 2021 5:11 PM
  • User-1545767719 posted

    Does the actual Model store data? (just the Model, no DB backend etc...)

    The answer is no as it seems that the "store" means a persistent storage such as database or files available at the server side. Web application including ASP.NET is basically stateless.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 29, 2021 1:14 AM
  • User379720387 posted

    No.

    A model is a predetermined group of properties that you will use as a tool throughout your code to move data from one place to another, it is more of a type than a container.

    A model is a temporary subset of your data that you will use as a tool. I think roughly you can think of it as a View Model.

    Any page in your website will have a Model or Models associated with it.

    There are two types of models: Entity Models and View Models.

    Roughly speaking an Entity Model looks like a table in your db (provided it is normalized) with properties and information on how this data relates to other tables.

    A View Model takes a subset of data from an Entity Model or more than one Entity and groups it together.

    Table Customers:

    custId, firstName, lastName, address, city, state zip  == Entity Model  (think Entity Framework)

    That same data plus their Locations and the sales per Location == View Model

    Here is a good video that shows you how Models are used.

    https://www.youtube.com/watch?v=_qjR25bEsWs

    You might as well learn Blazor at this point

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 1, 2021 8:53 PM
  • User-821857111 posted

    MVC is a presentation pattern. It is one way of implementing some kind of separation of concerns in UI-based application development. The V is the View or UI, the C is the controller, and the M represents the rest of the application i.e. everything that isn't the View or the Controller. MVC doesn't concern itself with the actual composition of the Model. All it cares about is that it should be separated from the View, and the the controller is the primary separation mechanism.

    Basically, MVC says that you shouldn't have database access code in your views, or be serializing data into JSON in your views etc, which are things that people can do (and actually do) when they aren't implementing some kind of logical separation of concerns in their application. 

    There are countless other patterns that can be applied to the design, implementation and management of the model itself.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 2, 2021 5:51 AM
  • User-795286723 posted

    This is great. Thank you everybody for taking the time to explain the details on the matter.

    Things make much more sense realizing the model is not meant to 'contain' but to shape the structure of the data being moved around, especially when you apply the idea at scale.

    CE

    Sunday, April 4, 2021 9:37 PM
  • User1535942433 posted

    Hi CodeExplorer,

    Do you have solved your problems? If you still have problems,you could post to us.If you have solved your problems,you could mark these answers which help you.It will make more people which have same problems with you to find these answers.

    Best regards,

    Yijing Sun

    Monday, April 5, 2021 7:08 AM