user7560542
user7560542

Reputation: 547

Where do you store "generic" class methods?

So let's say I have an Person entity:

PERSON  
ID  
FirstName  
LastName  
SSN   
LastModifiedDate

After this table exists in the database, and is part of my .edmx file, I usually create a partial class for the entity to use to perform some object-specific work. For example, with the Person entity I might have an UpdateLastModifiedDate() method which would simply set the value for the LastModifiedDate field to the present date and time for the selected object.

My question, however has to do with more "generic" methods, that don't relate to any specific object. For example, let's say I wanted to create a GetPersonBySSN() method which would accept a SSN and return the Person object with that SSN (if it exists). Where would I store this method?

I couldn't store it in the partial class (could I?) because I would have to do something odd like this:

Person myPerson = db.Person.where(u => u.ID = 12345);
Person myPerson2 = myPerson.GetPersonBySSN(123456789);

It just doens't make sense to do it this way.

Also, I am reluctant to create a second static class to store these "generic" methods (like maybe a PersonGenericMethods class) because it feels like I should be able to do this with only the main class, Person.

Where would you store these "generic" methods?


Edit: I am using ASP.NET MVC and would like recommendations on how to best build using this.

Upvotes: 0

Views: 109

Answers (2)

Roelant M
Roelant M

Reputation: 1716

The repository-pattern is perfect for this. Do keep in mind that you are programming OO. It is strange to get the person from 'itself'.

A repository will get your data and return your desired result.

on a side-note:

Person myPerson = db.Person.where(u => u.ID = 12345);
Person myPerson2 = myPerson.GetPersonBySSN(123456789);

Is strange, first, you get the person where ID = ... and then as function on the person you want to make sure that ssn is the same number?

your repo would look like this:

public class PersonRepository
{

 private readonly DBContext _myContext'
 public PersonRepository(DBContext myContext)
 { 
   _myContext = myContext; //<== Dependency INjection 
  }

  public function Person GetBySSN(int ssn)
  {
    return _myContext.FirstOrDefault(p => p.ssn = ssn);
   }
 }

take a look at: https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

just gives a good basis.

and then for later improvements: can you look into Maybe and Results types, but if your new to this, forget it :)

enjoy!

Upvotes: 1

D Stanley
D Stanley

Reputation: 152634

Also, I am reluctant to create a second class to store these "generic" methods (like maybe a PersonGenericMethods class) because it feels like I should be able to do this with only the main class, Person.

How a bout a PersonRepository class? That class would be responsible for interacting with the underlying Person data store - getting, updating, deleting, etc.

What you seem to want is called the Active Record Pattern where entity classes are responsible for creating, updating, and deleting themselves. While that's more common in other platforms like Ruby, in most .NET architectures the separation of the entity from the repository is more common.

The more separation you have (entities, repositories, business logic, UI logic, etc.) the more flexibility and re-usability you have. That comes, however, at the price of additional complexity.

Upvotes: 3

Related Questions