In MVC, if I have utility classes, will they be part of the models?

Asked

Viewed 192 times

6

For example, let’s assume I have a string manipulation class called strman which I will use to store operations to work with strings, already in the models folder I own usuario and usuarioDAO, in the case of the class strman, it can stay together with the classes I have in the models?

And if it’s really a model and it can stay in this folder, how can I arrange so it doesn’t mix with the logic of the application? (I say the main logic that are classes that contain business rules, not utilities like string handlers)

2 answers

3

Is not cool these utility classes.

In your case, create a folder called Extensions and then create a class StringExtensions and within it put all methods that are to treat strings.

Example, if you put a method to count words:

public static class StringExtensions
{
    public static int WordCount(this String string)
    {
        return string.Split(new char[] { ' ', '.', '?' }, 
                         StringSplitOptions.RemoveEmptyEntries).Length;
    }
}   

And then to consume:

var nome = "Renan Cavalieri";
var totalPalavras = nome.WordCount();
  • 2

    I agree with the view that it is not legal. Being specific about naming namespaces and classes is always a good thing. Creating a class and making a Batman utility belt is a bad idea. I’ve heard a buzzing comment that if you have a namespace called Util, then you say all the rest of the project is useless :D

  • It was a bad expression of mine, I didn’t know how to say, in my project I was using with the namespace app components strings and the class with the name strman, in case, would component be a correct term?

  • 1

    @Renancavalieri, component has more complex metadata. Vc just want to group methods to handle string, so Extensions is more suitable.

  • @Thiagolunardi, that I did not know, I found the subject interesting, I will ask a question about the differences of component and extensions.

3


If these are really utilities, you should organize under a structure of this category, usually a directory/namespace called Helpers, Utils, Services, depends on what nomenclature your environment or framework uses.

They should not be part of your model because your operations are more generic and mainly because they are not directly linked to the flow of operations of your business rules. Like you said, they’re just utilities.

A practical example:
A particular application when registering a blog post needs to generate one Slug for that record. Although you could generate this Slug in the function itself or method that persists the record in the database, it would be much better if you extract this operation to a more appropriate application category, such as a service or helper, or even stringutils. Now you can use this Slug generator not only in the method where blog post persistence occurs, but also anywhere that needs to create a string Slug.

  • Your edition killed the riddle of my question, but I have a second doubt that perhaps even deserves a question, I’m afraid to create dependency, both of using this class strman with static methods or having an instance injected into the model. It would be very harmful to use these functions within the model and create a dependency?

  • @Renancavalieri If your class depends on another, well, fine. The problem is how this dependency is linked to your main object. Use Dependency Injection both to make object dependencies explicit and to make it easier to switch to another class that implements the same interface as Srtman implements (Strmaninterface). As for static methods, it is another question. There is no problem if there is a motivation for them to be static, but issues about it in stackoverflow.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.