Data Source - Best way to organize methods/classes pertaining to the entire table?

Asked

Viewed 238 times

4

In my company we work with models based on the "Active Record" standard, that is, the model methods are always related to the operations of a unique database record, for example:

Class User {
    int id;
    string Name;

    public Find (id); // SQL / ORM to return the object based on the user's Id;
    public Save (); // SQL / ORM to save / update a user
    public Delete (); // SQL / ORM to delete a user.
}

// Create the user "John"
Set objUser = new User ();
objUser.Name = "John";
objUser.Save ();

My question is the following, in relation to the entity of the "User" database, we have methods that are at table level and not of record, as for example a method "getAllActiveUsers", which returns me a query object of all active users. This kind of situation today, for a lack of pattern ends up staying in the Activerecord model itself, which in my opinion does not make much sense.. Could you tell me what would be the most recommendable/elegant to address this kind of situation? I read something about Gateway and Repository Pattern that might be useful for this, but I was wondering if anyone had this same dilemma and how did to solve..

Thank you and sorry for the ignorance!

1 answer

1

In my opinion it does not make sense the "save" object itself in the database, regardless if you use pure ADO or some ORM, I believe that the Repository standard is more appropriate; but of course everything depends on the scenario.

I recommend you see this tutorial of Eduardo Pires, he does a step by step and explains some patterns and how to implement them, maybe it is not the solution of all your problems, but you will start to have a new vision and from there think about your own architecture:

http://eduardopires.net.br/2014/10/tutorial-asp-net-mvc-5-ddd-ef-automapper-ioc-dicas-e-truques/

I hope I’ve helped.

Browser other questions tagged

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