I remember a while back some discussions about whether Repositories are a good or a bad thing. The case given as a bad example was one where the Repository had a large number of methods on it, whether Repositories are good or not was irrelevant, it was crap code :-)
Personally I DO use Repositories. Or, to be more accurate, I use A Repository.
public interface IDomainRepository
{
IEnumerable<T> AllInstances<T>();
}
To get a list of instances of a specific class I can obviously ask the DomainRepository for AllInstances<User> for example. This is just about as far as my Repository goes, so that in Unit Test code I can control which instances are “retrieved from the database” without having a database.
[TestMethod]
public void GetErrors_ReturnsError_ForDuplicateUserName()
{
var user1 = new User() { UserName = "A" };
var user2 = new User() { UserName = "A" };
var allUsers = new List<User> { user1, user2 };
var mockDomainRepository = MockRepository.GenerateMock<IDomainRepository>();
mockDomainRepository.Stub(x => x.AllInstances<User>()).Return(allUsers);
var validator = new UserValidator(mockDomainRepository);
Assert.IsTrue(validator.GetErrors(user1).Any(x => x.FieldName == "UserName" && x.ErrorMessage == "Already in use"));
}
I’ll write about filtering data soon.