MVVM question: hierarchical model representation
-
Actually, my response was 100% correct given the information the poster gave. Whether he said it was an example or not is irrelevant. He did not indicate that code involved was not his or off limits. Had he responded to my response with that fact, I may have provided a different answer. For now, I will stand by my response that separating the data layer into a POCO and a M just for the sake of doing so (thinking it is a good separation of duty) is completely un-necessary and completely over-engineered and it would have completely solved his problem of converting between a Day and a DayModel. Just as I will stand by my other response (that you also down voted) that the use of partial classes is completely proper. If you hate partial classes, well, you better go and explain yourself to Bill Gates (er, Steve Balmer) because pretty much ALL his technologies use them. Winforms, WPF, WCF, ADO.NET, etc. EDIT: BTW, I gave your post a 3 (should have probably gone lower though) because it was an abuse of technology given the scenario. Use of factories and DI for such a simple problem? Really? Why not go even further and use a remote https server behind a firewall using 512 bit encryption and a separate validation server to ensure the object is valid? :laugh:
SledgeHammer01 wrote:
Actually, my response was 100% correct given the information the poster gave.
Your respnoce was simply a "You shouldn't do that." Nothing more nothing less. No explanation other than the claim of over engineering.
SledgeHammer01 wrote:
For now, I will stand by my response that separating the data layer into a POCO and a M just for the sake of doing so (thinking it is a good separation of duty) is completely un-necessary and completely over-engineered and it would have completely solved his problem of converting between a Day and a DayModel.
So, how are you going to handle a 3rd party controlled model? Or an object that you control that is used elsewhere and has embedded objects of which are not INOC? You make the assumption that the developer controls all aspects. In reality most patterns (Including MVVM) are created because we can not control all components. For the few, hey great. Code how you want. For the rest of the world we follow the patterns for easy transition, upgrades, extensions, testing etc. etc. etc
SledgeHammer01 wrote:
EDIT: BTW, I gave your post a 3 (should have probably gone lower though) because it was an abuse of technology given the scenario. Use of factories and DI for such a simple problem? Really? Why not go even further and use a remote https server behind a firewall using 512 bit encryption and a separate validation server to ensure the object is valid?
I doubt you have actually implimented a large scale MVVM project. The poster asked what is the common way to deal with that scenerio (you may want to go back and look... cause again you did not answer his question. You said he is over engineering). The scenerio is a very specific scenerio that large scale MVVM systems run into. And that is why we have other patterns. DI is NOT an abuse of technology and is actually quite simple. If however you system becomes large it gets complicated to maintain the intialization of yoru injected objects. In that case, you would be even more screwed with out it anyways and you need to simply start using some IoC. So explain to me how DI is too complicated for "such a simple problem"? You stating it is an abuse of technology is like stating OOP should not be used in simple apps. If you don't understand that, then it is likely you really don't understand what DI is.
-
This is where people go wrong with MVVM. Over thinking things. Over engineering things. Why do you need a Day AND a DayModel? A Day IS a DayModel. You should most certainly NOT have a DataProvider that returns a POCO object and then have your UI wrap it in a model type clas. Your DataProvider should return an object that implements INPC in some form. There is also no such rule that says you have to have 1:1 relationship between M and VM. A VM can most certainly use / expose 2 or 20 M's.
Thank you guys for all replies. No need to argue, really. To clarify a few points: the project which inspired the question is certainly more complex than counting days in a week (it is actually sports related but I came with the example to spare myself the need to explain the particular sport). It is a non commercial activity of mine and I though that one of the benefits would be to get comfortable with the celebrated MVVM. That is why I overthink things, because in this rare case I can afford it, and I can hardly get a thorough understanding by underthinking. SledgeHammer's reaction was my reasoning exactly - why complicate things with unneccessary model objects when viewmodel alone does all I need with no further complications. But most design patterns have use for model classes, so I thought there must be a "proper" (whatever it means) way to desing the application with model and viewmodel separated. I'll look into factory classes, thanks for the suggestion. I never said there is 1:1 correspondence between model and viewmodel classes, on the contrary. I remember now what made me think about the design the way I described. In one article on MVVM (don't know which one, maybe I read too many) someone said that model classes should contain only data that need to be stored in the data store. In particular, data which can be computed should not be present in the model classes but rather in the viewmodel. Back to my example - if the Day class contains a DateTime information about the time some employee came to work and another DateTime info when the employee left, it is all you need to store. Then my view wants to show how many hours the employee spend working, which can be computed from the data but it is job (I believe/ed) of the DayViewModel to provide this information. That is why I thought I needed both Day and DayViewModel. I got the impression that model classes store data, viewmodel contains all logic and view deals with appearance. I don't seek perfect code for the sake of it but I can hardly appreciate the benefits of MVVM for large scale applications if I can't successfully use it (with a bit of overkill) even for a small application. I'll be happy to hear any other suggestions and advice, H.
-
SledgeHammer01 wrote:
Actually, my response was 100% correct given the information the poster gave.
Your respnoce was simply a "You shouldn't do that." Nothing more nothing less. No explanation other than the claim of over engineering.
SledgeHammer01 wrote:
For now, I will stand by my response that separating the data layer into a POCO and a M just for the sake of doing so (thinking it is a good separation of duty) is completely un-necessary and completely over-engineered and it would have completely solved his problem of converting between a Day and a DayModel.
So, how are you going to handle a 3rd party controlled model? Or an object that you control that is used elsewhere and has embedded objects of which are not INOC? You make the assumption that the developer controls all aspects. In reality most patterns (Including MVVM) are created because we can not control all components. For the few, hey great. Code how you want. For the rest of the world we follow the patterns for easy transition, upgrades, extensions, testing etc. etc. etc
SledgeHammer01 wrote:
EDIT: BTW, I gave your post a 3 (should have probably gone lower though) because it was an abuse of technology given the scenario. Use of factories and DI for such a simple problem? Really? Why not go even further and use a remote https server behind a firewall using 512 bit encryption and a separate validation server to ensure the object is valid?
I doubt you have actually implimented a large scale MVVM project. The poster asked what is the common way to deal with that scenerio (you may want to go back and look... cause again you did not answer his question. You said he is over engineering). The scenerio is a very specific scenerio that large scale MVVM systems run into. And that is why we have other patterns. DI is NOT an abuse of technology and is actually quite simple. If however you system becomes large it gets complicated to maintain the intialization of yoru injected objects. In that case, you would be even more screwed with out it anyways and you need to simply start using some IoC. So explain to me how DI is too complicated for "such a simple problem"? You stating it is an abuse of technology is like stating OOP should not be used in simple apps. If you don't understand that, then it is likely you really don't understand what DI is.
Collin Jasnoch wrote:
Your respnoce was simply a "You shouldn't do that." Nothing more nothing less. No explanation other than the claim of over engineering.
Actually, my response was that they should be the same object and not two different ones which would solve the OPs problem. Given the info the OP posted, this was the correct answer.
Collin Jasnoch wrote:
So, how are you going to handle a 3rd party controlled model? Or an object that you control that is used elsewhere and has embedded objects of which are not INOC?
You make the assumption that the developer controls all aspects. In reality most patterns (Including MVVM) are created because we can not control all components. For the few, hey great. Code how you want. For the rest of the world we follow the patterns for easy transition, upgrades, extensions, testing etc. etc. etcWho asked about 3rd party components? Certainly not the OP. Quit making up imaginary scenarios to justify your answer. Until the OP says that he is using 3rd party components that are not designed for MVVM, your point is completely irrelevant to the OPs question. Where do you get off saying I don't follow design patterns? I certainly do. I don't spend a month implementing a design pattern when a 5 minute "shortcut" will work "for now". I certainly don't write all my code using "shortcuts" or I wouldn't even use MVVM.
Collin Jasnoch wrote:
I doubt you have actually implimented a large scale MVVM project. The poster asked what is the common way to deal with that scenerio (you may want to go back and look... cause again you did not answer his question. You said he is over engineering).
Where do you get off saying what I have implemented or not? I'm actually working on a large scale MVVM project right now and yes, I am using 100% proper MVVM with DI, messenger to communicate between views, etc. I use technologies where appropriate. Not to feed my ego like you obviously do.
Collin Jasnoch wrote:
You stating it is an abuse of technology is like stating OOP should not be used in simple apps. If you don't understand that, then it is likely you really don't understand what DI is.
Lol... you need help bro. Go see a therapist. I certainly know what DI is. In fact, I have personally implemented a DI conta
-
Thank you guys for all replies. No need to argue, really. To clarify a few points: the project which inspired the question is certainly more complex than counting days in a week (it is actually sports related but I came with the example to spare myself the need to explain the particular sport). It is a non commercial activity of mine and I though that one of the benefits would be to get comfortable with the celebrated MVVM. That is why I overthink things, because in this rare case I can afford it, and I can hardly get a thorough understanding by underthinking. SledgeHammer's reaction was my reasoning exactly - why complicate things with unneccessary model objects when viewmodel alone does all I need with no further complications. But most design patterns have use for model classes, so I thought there must be a "proper" (whatever it means) way to desing the application with model and viewmodel separated. I'll look into factory classes, thanks for the suggestion. I never said there is 1:1 correspondence between model and viewmodel classes, on the contrary. I remember now what made me think about the design the way I described. In one article on MVVM (don't know which one, maybe I read too many) someone said that model classes should contain only data that need to be stored in the data store. In particular, data which can be computed should not be present in the model classes but rather in the viewmodel. Back to my example - if the Day class contains a DateTime information about the time some employee came to work and another DateTime info when the employee left, it is all you need to store. Then my view wants to show how many hours the employee spend working, which can be computed from the data but it is job (I believe/ed) of the DayViewModel to provide this information. That is why I thought I needed both Day and DayViewModel. I got the impression that model classes store data, viewmodel contains all logic and view deals with appearance. I don't seek perfect code for the sake of it but I can hardly appreciate the benefits of MVVM for large scale applications if I can't successfully use it (with a bit of overkill) even for a small application. I'll be happy to hear any other suggestions and advice, H.
I never said you shouldn't use MVVM. On the contrary, I think its great even for small apps once you have a good MVVM framework established. I just said you shouldn't separate the data layer from the model. I'll even toss Colin a bone here and add "unless there is a very good reason to do so". It doesn't seem that you are using any 3rd party components and it seems like you are owning all the code, so there is no reason to do so. I also never said anything about combining the model and viewmodel. Those should definitely be separate. The model should implement INPC (or derive from a base class that does) and should handle dealing with the data. I.e. writing to the database, or XML, etc. The viewmodel should well, I hate to use the word "repackage" because that might give you the impression that you are supposed to repackage the data, which you aren't. Its just a "gateway" from the model to the view. It just presents the data in a way the view can use it. Often times, this is just exposing a collection as a property to get the data to the view. It really doesn't have to be all that complicated. With a few exceptions, most of the VMs I've seen in my life are very simple which is one of the joys of MVVM IMO. If the VM is doing a ton of work in manipulating the data, something is wrong somewhere. The VM also exposes commands that the View can use to manipulate the model, but its just a hook up. The real work of manipulating the models internals belong in the model.
-
SledgeHammer01 wrote:
Actually, my response was 100% correct given the information the poster gave.
Your respnoce was simply a "You shouldn't do that." Nothing more nothing less. No explanation other than the claim of over engineering.
SledgeHammer01 wrote:
For now, I will stand by my response that separating the data layer into a POCO and a M just for the sake of doing so (thinking it is a good separation of duty) is completely un-necessary and completely over-engineered and it would have completely solved his problem of converting between a Day and a DayModel.
So, how are you going to handle a 3rd party controlled model? Or an object that you control that is used elsewhere and has embedded objects of which are not INOC? You make the assumption that the developer controls all aspects. In reality most patterns (Including MVVM) are created because we can not control all components. For the few, hey great. Code how you want. For the rest of the world we follow the patterns for easy transition, upgrades, extensions, testing etc. etc. etc
SledgeHammer01 wrote:
EDIT: BTW, I gave your post a 3 (should have probably gone lower though) because it was an abuse of technology given the scenario. Use of factories and DI for such a simple problem? Really? Why not go even further and use a remote https server behind a firewall using 512 bit encryption and a separate validation server to ensure the object is valid?
I doubt you have actually implimented a large scale MVVM project. The poster asked what is the common way to deal with that scenerio (you may want to go back and look... cause again you did not answer his question. You said he is over engineering). The scenerio is a very specific scenerio that large scale MVVM systems run into. And that is why we have other patterns. DI is NOT an abuse of technology and is actually quite simple. If however you system becomes large it gets complicated to maintain the intialization of yoru injected objects. In that case, you would be even more screwed with out it anyways and you need to simply start using some IoC. So explain to me how DI is too complicated for "such a simple problem"? You stating it is an abuse of technology is like stating OOP should not be used in simple apps. If you don't understand that, then it is likely you really don't understand what DI is.
Collin Jasnoch wrote:
I did not vote on any of your 'Partial Class' posts. I will inform you I voted for you comment on IValueConvertor
SledgeHammer01 wrote:
In MVVM, IValueConverter and IMultiValueConverter
are very rarely used because the VM does that work.That is because that statement is completely false.
Maybe in your applications you have programmed this way, but a small search on google will show you they deffinately have their place.Do you not understand the difference between "rarely" and "never". You should actually read the latest response from the OP in which he clearly states that all your imaginary doom scenarios are not at play, so following your own "don't mislead the OP" rule, I have to down vote you.
-
Collin Jasnoch wrote:
Your respnoce was simply a "You shouldn't do that." Nothing more nothing less. No explanation other than the claim of over engineering.
Actually, my response was that they should be the same object and not two different ones which would solve the OPs problem. Given the info the OP posted, this was the correct answer.
Collin Jasnoch wrote:
So, how are you going to handle a 3rd party controlled model? Or an object that you control that is used elsewhere and has embedded objects of which are not INOC?
You make the assumption that the developer controls all aspects. In reality most patterns (Including MVVM) are created because we can not control all components. For the few, hey great. Code how you want. For the rest of the world we follow the patterns for easy transition, upgrades, extensions, testing etc. etc. etcWho asked about 3rd party components? Certainly not the OP. Quit making up imaginary scenarios to justify your answer. Until the OP says that he is using 3rd party components that are not designed for MVVM, your point is completely irrelevant to the OPs question. Where do you get off saying I don't follow design patterns? I certainly do. I don't spend a month implementing a design pattern when a 5 minute "shortcut" will work "for now". I certainly don't write all my code using "shortcuts" or I wouldn't even use MVVM.
Collin Jasnoch wrote:
I doubt you have actually implimented a large scale MVVM project. The poster asked what is the common way to deal with that scenerio (you may want to go back and look... cause again you did not answer his question. You said he is over engineering).
Where do you get off saying what I have implemented or not? I'm actually working on a large scale MVVM project right now and yes, I am using 100% proper MVVM with DI, messenger to communicate between views, etc. I use technologies where appropriate. Not to feed my ego like you obviously do.
Collin Jasnoch wrote:
You stating it is an abuse of technology is like stating OOP should not be used in simple apps. If you don't understand that, then it is likely you really don't understand what DI is.
Lol... you need help bro. Go see a therapist. I certainly know what DI is. In fact, I have personally implemented a DI conta
SledgeHammer01 wrote:
Actually, my response was that they should be the same object and not two different ones which would solve the OPs problem. Given the info the OP posted, this was the correct answer.
Exact Responce:
SledgeHammer01 wrote:
This is where people go wrong with MVVM. Over thinking things. Over engineering things. Why do you need a Day AND a DayModel? A Day IS a DayModel. You should most certainly NOT have a DataProvider that returns a POCO object and then have your UI wrap it in a model type clas. Your DataProvider should return an object that implements INPC in some form.
Sounds like you first told him he over engineered his "EXAMPLE". Then you went on to say he does not need a Day and DayModel (don't think the OP even mentioned DayModel... I will take this as a typo and assume ViewModel). Then you went on to talk about the data provider should return an INOC. Not clear. Not concise. Not a good answer. Keep griping. It will certainly get me to change my mind
SledgeHammer01 wrote:
Who asked about 3rd party components? Certainly not the OP. Quit making up imaginary scenarios to justify your answer. Until the OP says that he is using 3rd party components that are not designed for MVVM, your point is completely irrelevant to the OPs question.
Where do you get off saying I don't follow design patterns? I certainly do. I don't spend a month implementing a design pattern when a 5 minute "shortcut" will work "for now". I certainly don't write all my code using "shortcuts" or I wouldn't even use MVVM.The OP asked how this is handled. I explained. Asking why (which you have done under your gripe about being down voted for a poor answer) will get you the why... being the examples I gave.
SledgeHammer01 wrote:
Where do you get off saying what I have implemented or not?
If the shoe fits. You have stated the user should not use DI and should impliment INOC on the returning data access blindly ignoring that in the real world most objects being returned from the data layer are used by multiple consumers. Yes in a simple case who cares. Again, the user asked about what others do. I provided and you told him he over engineered an example code base.
SledgeHammer
-
Collin Jasnoch wrote:
I did not vote on any of your 'Partial Class' posts. I will inform you I voted for you comment on IValueConvertor
SledgeHammer01 wrote:
In MVVM, IValueConverter and IMultiValueConverter
are very rarely used because the VM does that work.That is because that statement is completely false.
Maybe in your applications you have programmed this way, but a small search on google will show you they deffinately have their place.Do you not understand the difference between "rarely" and "never". You should actually read the latest response from the OP in which he clearly states that all your imaginary doom scenarios are not at play, so following your own "don't mislead the OP" rule, I have to down vote you.
Yep, and to tell someone that something is 'Rarely' used when it certainly has its place is false. In other words inaccurate and deserves a low vote. You can find literally thousands of blogs talking about usage of IValueConverters. I will admit that IMultiValueConverters have had some debate, but I think it is more likely due to their mis-use as they are by definition more complicated. IValueConverters on the other hand are simple and keep with SOC. Embedding the logic in the VM is the reverse and is bad practice. [EDIT] Read his posts, and confused by your 'doom' scenerios comment. What doom scenerios. Again, it seems one should use best practices no matter the scale. Yes if it doesn't make sence you don't apply, because that is not best practice. For example, your exaggeration about encryption. If one does not care about encryption they obviously would not make a layer for it. However, washing away DI as you did saying it is over engineering is completely misleading. DI is quite simply and so are factories. It clearly solves the problem. I voted based on your content of your answer. I know and do actually agree with your point (people over engineer). And if you worded it better I would have gave you a 5. However, claiming my answer was over engineered is also poor taste. DI in its simplest wording is this:
interface
Saying that is overengineering is a ridiculous notion. Moreover, I introduced to the OP how these problems are handled. That was his question. Your answer was it is not a problem because his system is small. Again poor taste as he said it was an example. The poster has come back and said your point is what he was looking for, hopefully you didn't mislead him too much. As I said, I do agree with your point but you worded it poorly to mislead.Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.
-
I never said you shouldn't use MVVM. On the contrary, I think its great even for small apps once you have a good MVVM framework established. I just said you shouldn't separate the data layer from the model. I'll even toss Colin a bone here and add "unless there is a very good reason to do so". It doesn't seem that you are using any 3rd party components and it seems like you are owning all the code, so there is no reason to do so. I also never said anything about combining the model and viewmodel. Those should definitely be separate. The model should implement INPC (or derive from a base class that does) and should handle dealing with the data. I.e. writing to the database, or XML, etc. The viewmodel should well, I hate to use the word "repackage" because that might give you the impression that you are supposed to repackage the data, which you aren't. Its just a "gateway" from the model to the view. It just presents the data in a way the view can use it. Often times, this is just exposing a collection as a property to get the data to the view. It really doesn't have to be all that complicated. With a few exceptions, most of the VMs I've seen in my life are very simple which is one of the joys of MVVM IMO. If the VM is doing a ton of work in manipulating the data, something is wrong somewhere. The VM also exposes commands that the View can use to manipulate the model, but its just a hook up. The real work of manipulating the models internals belong in the model.
I am starting to get it now. As I explained, I lived under the impression that model classes should only store data and not contain any means to manipulate them (this belongs to viewmodel). I learned this in some MVVM article, possibly not very good one because you say the direct opposite. Still it is good to know there are ways to deal with data sources I do not have under my control, so I appreciate Colin's input. But suppose the UI allows for direct manipulation with the individual Day objects (which are low in the whatever-Week-Day hierarchy). I could implement this by exposing a Manipulate (or something) command in the Day class, but rather in the DayViewModel class, since command handling is the viewmodel's job, right? If so, I am back to square one since I need the Day model class which manipulates the data and the DayViewModel which exposes the manipulation commands. If this is correct that the question still stands, how can the Week model class contain a collection of Day objects if it actually needs DayViewModel objects and thus would have to be tied with one particular viewmodel? If you say to expose the commands directly in the Day class, then I really see no point of VM layer since it would do nothing of any value.
-
I am starting to get it now. As I explained, I lived under the impression that model classes should only store data and not contain any means to manipulate them (this belongs to viewmodel). I learned this in some MVVM article, possibly not very good one because you say the direct opposite. Still it is good to know there are ways to deal with data sources I do not have under my control, so I appreciate Colin's input. But suppose the UI allows for direct manipulation with the individual Day objects (which are low in the whatever-Week-Day hierarchy). I could implement this by exposing a Manipulate (or something) command in the Day class, but rather in the DayViewModel class, since command handling is the viewmodel's job, right? If so, I am back to square one since I need the Day model class which manipulates the data and the DayViewModel which exposes the manipulation commands. If this is correct that the question still stands, how can the Week model class contain a collection of Day objects if it actually needs DayViewModel objects and thus would have to be tied with one particular viewmodel? If you say to expose the commands directly in the Day class, then I really see no point of VM layer since it would do nothing of any value.
Member 1033907 wrote:
I lived under the impression that model classes should only store data and not contain any means to manipulate them (this belongs to viewmodel). I learned this in some MVVM article, possibly not very good one because you say the direct opposite.
Your model class should not have any logic. It some cases it doesn't exist in code. For example I have a DataBase with a DataTable of Customers. Through my services I can get the data but my client code still needs to build the object. This object can use the INotifyPropertyChange interface, which would then end up my View Model. The model 'Customers' exists inside my data base.
Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.
-
Actually, my response was 100% correct given the information the poster gave. Whether he said it was an example or not is irrelevant. He did not indicate that code involved was not his or off limits. Had he responded to my response with that fact, I may have provided a different answer. For now, I will stand by my response that separating the data layer into a POCO and a M just for the sake of doing so (thinking it is a good separation of duty) is completely un-necessary and completely over-engineered and it would have completely solved his problem of converting between a Day and a DayModel. Just as I will stand by my other response (that you also down voted) that the use of partial classes is completely proper. If you hate partial classes, well, you better go and explain yourself to Bill Gates (er, Steve Balmer) because pretty much ALL his technologies use them. Winforms, WPF, WCF, ADO.NET, etc. EDIT: BTW, I gave your post a 3 (should have probably gone lower though) because it was an abuse of technology given the scenario. Use of factories and DI for such a simple problem? Really? Why not go even further and use a remote https server behind a firewall using 512 bit encryption and a separate validation server to ensure the object is valid? :laugh:
Haha. Didn't like my responce so you 1 voted it? Good for you! Not gonna change the way I vote though. You can go ahead and one vote me all you want. It will prove to everyone you are just a troll.
Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.