base class or object type property | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

base class or object type property

I have designer dilemma. I make a program in C# (wpf) and want to use MVVM. I have a viewmodel that contains a model. This model say a vehicle, exists in different types which do the same thing in different ways (truck, jeep, tractor, car) I can make a base class viewmodel and derive all viewmodel from the baseclass (this is quitte a lot of work and requires 4 viewmodels and 4 views to be made, and of course the base class) VehicleViewModel // base class TruckViewModel : VehicleViewModel JeepViewModel : VehicleViewModel TractorViewModel : VehicleViewModel CarViewModel : VehicleViewModel or I can use just one viewmodel and define the type of model that is used as a constructor parameter. This is less work. VehicleViewModel(VehicleType MyVehicle) Which way do you prefer ? Why should I choosse the first or the second option

28th Feb 2018, 9:12 PM
sneeze
sneeze - avatar
3 Answers
0
Edited after reading the question wrong at first. I would go with option one because I prefer the types of my viewmodels to be as specific as possible to what they are. If I want to create a Tractor I prefer to create a Tractor, not a Vehicle. Same way when sending, receiving data and so on. I see no problem with either though. Would say it mostly comes down to personal preference. Take the approach you like the most, they both work.
1st Mar 2018, 12:15 AM
Brandlarm
Brandlarm - avatar
+ 1
@Brandlarm You are correct. This is about personal preference and that is making the decision even harder. On a theoretical view I prefer option one. It looks more sustainable and solid to me. I do not have much experience with MVVM and like to stay on the save side. Also all views will propably be the same. So option two is a also a good option. May be not one which studybooks prefer but will work prefectly fine. @Catalin I use c# and WPF together with MVVM. The model are the description of the data The viewmodels are the code that enable the views to display data Yes also restricted access can be in there The views display data but do not know about business logic and rules. In option one I want all four ViewModels to inherited from one VehicleViewModel. (notice it is a ViewModel not a Model) In the View I can create a listbox and bind this listbox to a observablecollection. ObservableCollection<VehicleViewModel> Vehicles = new ObservableCollection<VehicleViewModel>(); Now all fourViewModels can be added to Vehicles and wil use there correspoding View to display the data. I find it really hard to explain, I hope I explained myself a bit more
1st Mar 2018, 8:52 PM
sneeze
sneeze - avatar
0
i supose you speak about asp.net application. first you need models. the viewmodels are used in current programming trending for displaing and restricting acces, setting imput limits, displaying the name of an property ( asume that your prop is named ConfirmPassword and you need the name to be "Confirm password") etc. all these can be done using viewmodels and helps for security makeing the app harder to crack. but if you named the base models with truckViewModel...thats a nother thing because base models are used for scaffolding with the database using code first. cheers
1st Mar 2018, 6:54 PM
Catalin Popinciuc
Catalin Popinciuc - avatar