Which class is responsible for getting data from a database

160 Views Asked by At

I've got a database class which inherits from an interface (IDataSource) and a class which represents an user.

What really grinds my gears is, which class is responsible for getting user-data from the database - should the user class define a method which requires an instance of the database class / IDataSource to execute a query or should the database class implement a method which receives user-data and return an user object?

It's just a general question and I'm looking forward to read an helpful answer.

Edit:

Maybe s.o. explain how the following works:

class ContactValidator : IValidator
{
public Boolean Validate(Contact contact) { ... /* check Name is unique in DB */ }
}

What would the IValidator look like?

as far as i know it would look somehow like this:

public interface IValidator{
bool Validate(???) 
}

I can't set "Contact" as a parameter of the interface otherwise every class which implements the interface would require a Contact as a parameter.

Is it possible to define "object" as the parameter whithout the compiler complaining about the parameter type?

@ Active Records vs. Repository - pros and cons?

The only solution I've found would be:

public interface IValidator<T> {
bool Validate(T obj)
}

or is there another way to get it working for more than one class?

0

There are 0 best solutions below