Comments (4)
In most cases logic like that belongs in the ViewModel. If you think about the Model as a schema, your ViewModel would be a Stored Procedure for working with the data.
from template10.
In case of applications that just read and write data, like, for example, a simple task manager, thinking about the Model as a schema is probably quite a good idea. After all, the app only needs to add new tasks to a table (e.g. in a database or a CSV file) and display them to the user. View Models can handle that. But what if an app needs to do some more advanced calculations? Let's say, we have an app that needs to calculate distance between two points (each point being a model class). In such a case, each point object can have a method called CalculateDistanceFromOtherPoint(Point point) or we could use a domain service (which would still be a part of the model). However, View Models wouldn't be a good place to put such logic, and if you discourage users from putting methods into model classes (which the template documentation does), this might actually lead to creating anemic Domain Models and overcowrded View Models that don't ahdere to Single Responsibility Principle and behave like a Swiss Army Knife.
from template10.
Using your example of calculating distance, that strikes me as something that belongs out of band from the Model /and/ View Model. A better option would be to house that logic in a separate class entirely. The selection of MVVM or MVC does not remove the need for "utility functions" as it were.
What would make sense is adding a method to your Model that returns Model data in a format that the calculator can use. I've run into this exact scenario a few times and typically include an .AsPoint() or .AsLocation() method that returns the data in a format my calculation class can work with.
from template10.
This is a general question and not a Template 10 question. I will give you my personal response, but then I will close the issue. Next time you can ask these types of questions on StackOverflow and get lots of great feedback that way.
Anyway, the only two reasons to abstract logic out of a class into a separate class is either for reuse or for servicing. That is to say, you would take your logic out of your ViewModel if you cannot test it without abstracting it out, need it in more than one ViewModel, or because your ViewModel will never change but your logic is subject to frequently changing and you can service your project in some simpler way by having it removed. If, however, your logic is unique to that ViewModel and unlikely to change, it would be a long row to hoe to convince me you need yet another class. If it is because your class file is too large, then use a partial. Otherwise, I am a fan of putting logic where it is used and abstracting as little as possible for the ultimate sake of cost of maintenance. Some developers feel differently and create abstraction levels more frequently and they are not wrong in their own right. However, it's not what I prefer. Simple is almost always the best architectural pattern.
from template10.
Related Issues (20)
- Discussion: How to handle Background Tasks. HOT 1
- Unable load T10 project form GitHub HOT 13
- Add ToUri() to the PathBuilder HOT 4
- Navigation Services Not working HOT 1
- Reconsider if ViewModelBase should implement NavigationService HOT 1
- Why haven't we implemented auto-discover for Nav/Pages? HOT 1
- Confirm revert projects back to UWP Class Library
- Misleading information about Prism HOT 1
- Misleading exception while failing to deserialize an argument is passed in Navigation Service HOT 1
- Xaml exception (suddenly, without history) HOT 2
- AttachGestures should have an override without Window
- Add Nav extensions off Page types HOT 1
- GestureService shouldn't handle the BackRequested from SystemNavigationManager
- Exception handling in SettingsService HOT 3
- How can I use Navigation Service without Template 10 Bootstrapper? HOT 2
- Any news about future? HOT 1
- I need access to old Nuget packages HOT 1
- Update to reference latest version of Newtonsoft.Json library HOT 2
- Windows 10
- Template 10 ellipsis collapsed error
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from template10.