0

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?

Community
  • 1
  • 1
Th1sD0t
  • 997
  • 2
  • 10
  • 33
  • Usually is the latter. In other words, the [DAO](http://en.wikipedia.org/wiki/Data_access_object) should return an user [DTO](http://en.wikipedia.org/wiki/Data_transfer_object). – rsenna Oct 21 '14 at 19:34
  • See [Active Records vs. Repository](http://stackoverflow.com/questions/6522009/active-records-vs-repository-pros-and-cons). This seems to be opinion-based or off-topic, try http://programmers.stackexchange.com and explain your perceived pros and cons of both approaches. – CodeCaster Oct 21 '14 at 19:36
  • Or see there http://stackoverflow.com/questions/905498/what-are-the-benefits-of-persistence-ignorance – Wiktor Zychla Oct 21 '14 at 19:36
  • Do you have a particular paradigm or principle you're wanting to guide this decision? There are many principles and paradigms and your answer will depend on the ones you choose to follow. – Aaron Hawkins Oct 21 '14 at 19:41

0 Answers0