GithubHelp home page GithubHelp logo

Comments (4)

mlafleur avatar mlafleur commented on May 18, 2024

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.

lukaszciastko avatar lukaszciastko commented on May 18, 2024

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.

mlafleur avatar mlafleur commented on May 18, 2024

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.

JerryNixon avatar JerryNixon commented on May 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.