C
C#9mo ago
fateos

❔ Understanding the model-view-controller

if there is a good example with code I would be thankful if you linked it here. So from what I understand correct me if I am wrong: I have these classes: Model: Dog.cs,Person.cs .. View: form.cs Controller: logic.cs right now if I do a winforms app lets say my view is the events (code in form.cs) then i do separate class for the model (for example person class dog class and so on) and the controller class that manages the logic Do I put the proper method call in the click events in my form.cs? and the method calls all come from my controller class right? so in my form.cs I have a click event in that click event I say for example "controller.CreatePerson(name,age..);" and this controller class has access to the Dog,Person class through composition There could be also some ifs in my form.cs depending on if I want to do certain logic only if that checkbox is checked for example and so on. But then the logic is not all in my logic.cs did I get this concept right? Looking at this Picture on this link: https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller the Model updates the view but how would that look in this code example? I thought the models are just the "object classes"
Model–view–controller
Model–view–controller (MVC) is a software design pattern commonly used for developing user interfaces that divides the related program logic into three interconnected elements. This is done to separate internal representations of information from the ways information is presented to and accepted from the user.Traditionally used for desktop graph...
47 Replies
ekhidna
ekhidna9mo ago
MVC is more web targeted. I guess you call also use it in Windows Forms, not sure. Visual Studio 20xx you have several templates to start Web Apps or Web API in MVC with no code or minimal code
cap5lut
cap5lut9mo ago
you are a bit off. ur Form would be the View, its there for the representation of the data. ur Dog, Person, etc are the Models, which contain the respective data and the business logic. the event handlers of ur Form would be the controller, thats the piece that wires the model and the view together.
Pobiega
Pobiega9mo ago
Winforms isn't really MVC oriented, and you don't really get a clean separation between view and controller when implementing it as such Sniped by cap5 😄
cap5lut
cap5lut9mo ago
finally got back at ya for once! xD
ekhidna
ekhidna9mo ago
Model is usual composed of classes that abstract the DB, the DAL, the classes are the same as in DB. In the Controller you perform actions typically CRUD, to the DB and present them in the View usual by DTO's. DTO's are simillar to DAL classes usually with some more or less properties and closer to the View https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/getting-started https://www.youtube.com/watch?v=E7Voso411Vs
Pobiega
Pobiega9mo ago
true in an ASP app, but for a desktop app there is no guarantee that your models are backed by a database or similar at all
Pobiega
Pobiega9mo ago
Until @zezima11 confirms that they are making a web app, there is no reason to post ASP.NET content
ekhidna
ekhidna9mo ago
True. Also for windows Forms I am not sure if MVC is the right pattern. Maybe MVVM would make more sense imo.
Pobiega
Pobiega9mo ago
yes 🙂 ultimately, we need the thread creator to say something :p
ekhidna
ekhidna9mo ago
I can delete my messages, if it's not a web app or API and is polluting the answers just say it @zezima11
cap5lut
cap5lut9mo ago
lets go with a small example. u have a ur form to create person, containing 2 text boxes for first and last name and 2 buttons to save the person data and to simply clear the form again the form itself is ur view, the button event handlers reflect ur controller, and some Person class that holds the data and has a save method to store it somewhere - this is ur model. the model itself doesnt know anything about the controller or the view, it just has to know how to deal with itself (and other models). what wires everything together is the controller/ur event handlers. eg, clicking on the clear button, the controler doesnt have to touch the model at all, there is no business logic going on as it just has to update the view (emptying the text boxes) the more important part is the create button. here the controller gets the data from the text boxes, creates a new person instance, sets its properties and cales its save method. it doesnt have to know how the person is saved, it just needs to trigger that. now 2 things could have happened, either the saving was a success, or there was an error (123 wouldnt be a valid firstname, right?) so based on the result, it triggers an update to the view. if it was success, it might clear the form and show an message box telling the user that everything went fine. it might have failed and marks the erroneous text box somehow. basically the controller just grabs the data from the view, constructing the respective model and triggers its business logic. based on that result, the controller decides which view to show next and provides relevant data.
fateos
fateos9mo ago
its winforms. I thought MVVM is the same just a another term?
cap5lut
cap5lut9mo ago
no, MVC and MVVM are different design patterns, MVVM is more fitting for GUI applications tho
fateos
fateos9mo ago
okey what would differ then? I thought the whole concept was so I dont have to touch my program logic and the model and swap out winforms for lets say WPF or console app btw I am reading all your comments one by one I would also need to look into ASP.NET is that with POST,GET,PUT and so on?
ekhidna
ekhidna9mo ago
You've got here a nice explanation of the MVC, MVVM and MVP Architecture Patterns. In the Bottom of the page. In the beginning is about software patterns https://www.netsolutions.com/insights/software-design-pattern/
Pobiega
Pobiega9mo ago
ASP.NET is the "web development" portion of the C# base library its for making websites, web apis, web apps etc.
ekhidna
ekhidna9mo ago
I found this repository in GitHub https://github.com/KhalilKhalaf/MvvmTemplate Maybe it will help you Search in google for github projects with examples of MVVM
fateos
fateos9mo ago
"the model itself doesnt know anything about the controller or the view, it just has to know how to deal with itself (and other models)." so the controller is in my view then. My view being form.cs and my form.cs also including all the event handlers so in the click event I would just say clear textboxes and in the case of an update I would call the model.update(); method in the click event handler in my form.cs right? and in the create button example: I would need to write an if(success)... else ....in my click event handler of the create button. so I need to know what the return of that method to interpret that unless its a bool method obviously. so the confusion part for me was or is that the controller and view is in the same class (form.cs) so if I wanna renew my app to a wpf app I would need to write everything new except for the models that text there didnt help me understand it :/
Pobiega
Pobiega9mo ago
correct. MVC is not a good fit here. MVVM would be better, then you only need to rewrite your view your viewmodel could be kept, assuming you write it properly with reactivity in mind (NotifyPropertyChanged etc) but lets be real, if you're thinking about possibly moving to WPF/Avalonia later... just do it now
fateos
fateos9mo ago
no I am not thinking its just to understand the concept
Pobiega
Pobiega9mo ago
there are no good reasons for using winforms for a new project okay
fateos
fateos9mo ago
I still dont understand the differences between MVC and MVVM is the actual logic in my model classes or in a "logic" class? when I program I just put each logic in their own class
Pobiega
Pobiega9mo ago
logic related to the model itself can be in the model
cap5lut
cap5lut9mo ago
yeah thats a bit of the weird thing. theoretically u could also make the controller an extra class, but then u would have methods in the controller for each event handler, basically a 1:1 mapping of that, making it just more complex and verbose for the sake of the design pattern. but that would be just bad
fateos
fateos9mo ago
I am at the very beginning still. Only created desktopapps and only small ones
ekhidna
ekhidna9mo ago
MVC as said earlier is designed for WebApps. MVVM is mainly for GUI, windows forms You also have the Model and the View. The ViewModel acts kind of a glue between them, specially with the bindings
fateos
fateos9mo ago
ye I didnt learn with the bindings yet so in that github example public string FirstName { get { return _firstName; } set { _firstName = value; NotifyOfPropertyChange(() => FirstName); // this notifies the change to whoever binded to FirstName property NotifyOfPropertyChange(() => FullName); // this notifies the change to whoever binded to FullName property } } I wouldnt know how this line works NotifyOfPropertyChange(() => FirstName is this only a wpf thing?
ekhidna
ekhidna9mo ago
I actually didn't see the code, but I imagine in the View when the user changes the first name, there is an event in ViewModel and Model that will be updated or do some action. I am not sure though. I never worked with MVVM, only with MVC, so my experience with it is scarce. Sorry
Pobiega
Pobiega9mo ago
Not exclusively, but afaik winforms doesn't care about it. Most xaml frameworks do
fateos
fateos9mo ago
offtopic question: Is there some sort of a guide after the basic oop stuff that I can go and learn with tutorials? like one step above polymorphism and so on. I feel like internet is flooded with tutorials up to that point but I am looking for things to learn after that so far I have done winforms and console apps
Pobiega
Pobiega9mo ago
tutorials are easy to make for the basic stuff, since everyone goes through the same thing but once you reach higher up the field expands greatly, like the canopy of a tree short answer: no but ask specific questions here, and we give specific answers This discord is an insane resource, we have hundreds of very very experienced developers, some microsoft employees (including at least 2 that work on C# itself) and just a huge trove of accumulated knowledge
ekhidna
ekhidna9mo ago
It's hard to know your level of knowledge as I don't know your expertise or how many years of work you have. But there's a lot more to learn in C# besides it's oop characteristics: classes, objects, encapsulation, inheritance, abstraction, and polymorphism. You can learn software design patterns. The link I sent before talks about them and there are free resources on internet to learn about it https://www.netsolutions.com/insights/software-design-pattern/ You can also have 1 month free trial in Azure and program C# with Azure Program C# with different architectures. eg Microservices, containerize Microservices and use an orchestrator like Kubernetes. Event Driven, CQRS, Message/Publisher with RabbitMQ, or Azure Service Bus, Unit Tests, Delegates, Asynchronous Programming Async/Await, Threads, Dependency Injection, good software practices like SOLID and many more .... Sorry if you already know most of this. I'm just assuming.
fateos
fateos9mo ago
I program event driven (winforms) noone writes unit tests also isnt fun the few times I had to do it. What do you mean with program c# with azure? I know azure has a version control is that what you meant? Hard to keep design patterns in mind if you dont use it in practise enviroment.. Never done anything with different architectures. I looked into delegates and its also very specific knowledge I prob wont need in a long while. SOLID I heard a lot I will look deeper in to that. Whats with stuff like ASP.NET ? is it the winforms for web?
Pobiega
Pobiega9mo ago
ASP.NET is the "framework" for any web app in C# its nothing like winforms thou
fateos
fateos9mo ago
Is it worth getting in to? I keep hearing winforms is outdated and so on
Pobiega
Pobiega9mo ago
its what most people use C# for these days
ekhidna
ekhidna9mo ago
Yes it is worth for sure. I don't know what you use C# for. If it's merely a hobby. But if it is for work programming C# for web is fundamental
Pobiega
Pobiega9mo ago
winforms is fucking dead, so avoid it like the plague. If you wanna do desktop stuff, Avalonia is the current recommendation. ASP.NET is extremely good for web development (backends primarily) its a very battletested and well designed system
ekhidna
ekhidna9mo ago
At work most of companies use C# for backend development for Web based applications. Being Web Apps with MVC , or Web APIs. So imo knowing how to program C# in Web environment is essenctial for a job "Unit Test isn't fun" ok I can even agree with you at some point. But is essential to make them if you want to work in IT
Pobiega
Pobiega9mo ago
agree that its hard to write proper tests for most winform apps thou because of how spaghetti they tend to become
ekhidna
ekhidna9mo ago
I think the main question here is: Why do you want to program in C#? Is it merely a hobby or do you want to become an IT Professional (or already are) Depending on the answer the approach can be different "What do you mean with program C# with azure? I know azure has a version control is that what you meant? " I think you're a bit confused here. For version control you use Git. You can use older version control systems like SVN or even Microsoft's I think it's TFS, not sure. But Git is the standard in the Industry. Azure is Microsoft's cloud. You can use Azure DevOps to work in an Agile framework like Scrum. Create pipelines etc. Azure also has a ton of services in there. It's basically program C# for cloud. Where your services and infrastructure are all in the cloud. But I would leave that for later
fateos
fateos8mo ago
we use winforms at work and xamarin for mobile app (but I dont program that for now) and then we have webbased aswell but its not asp.net its gizmox webgui I dont know if it says something for you is it really tho? even we have QA testers and I thought maybe in other IT companies there are also people who specialize on unittesting and testing in general
fateos
fateos8mo ago
so basically this what we have at work
No description
fateos
fateos8mo ago
but I didnt set this up so I wouldnt know how it was configured and everything I think it was called tfs and now its called Azure DevOps Server which we use i think almost all companies work still with 4.8 Frameowrk atleast I didnt find one with a newer technology being used
fateos
fateos8mo ago
https://www.youtube.com/watch?v=E7Voso411Vs is this outdated? Otherwise I would like to do it in my freetime to get into that topic
Programming with Mosh
YouTube
Step-by-step ASP.NET MVC Tutorial for Beginners | Mosh
🔥Get the complete ASP.NET MVC course: http://bit.ly/2OBKf0w Want to learn ASP.NET MVC 5 from scratch in a fun, step-by-step and pragmatic way? Watch this tutorial and get started. Table of Content: 00:00 Introduction 02:48 ASP.NET MVC Architecture 05:31 Setting Up the Development Environment 07:38 Your First ASP.NET MVC 5 App 14:34 MVC in Ac...
Accord
Accord8mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts