views:

155

answers:

3

I'm working on using the Repository methodology in my App and I have a very fundamental question.

When I build my Model, I have a Data.dbml file and then I'm putting my Repositories in the same folder with it.... IE:

Data.dbml
IUserRepository.cs
UserRepository.cs

My question is simple. Is it better to build the folder structure like that above, or is it ok to simply put my Interface in with the UserRepository.cs?

Data.dbml
UserRepository.cs              which contains both the interface and the class

Just looking for "best practices" here. Thanks in advance.

+2  A: 

General best practice is to have one class or one interface per file.

Here's the more generic discussion, which I think applies to your case: http://stackoverflow.com/questions/2434990/one-class-per-file-rule-in-net

As a developer new to your project, I would appreciate knowing that IUserRepository exists--without having to fish through your UserRepository.cs file.

ewwwyn
Thanks. I'll use a separate file for each class.
rockinthesixstring
A: 

Do whatever makes sense to you.

Personally I find browsing solutions for anything painful so I have hot keyed Goto Definition/Implementation and Resharpers FindUsages Go to Type, Go to File so I never have to click anything.

Combining interfaces and classes in one file makes sense if the class or interfaces are small.

Also if your following the Liskov substitution principal / a dependency injection strategy and general good design practices you would rarely be interacting with actual implementations anyway. Repositories should almost never be referred to by their concrete implementation.

jfar
Thanks for that. Can you please elaborate on `Repositories should almost never be referred to by their concrete implementation.`?
rockinthesixstring
@rockinthesixstringWhen you need to use a class you don't reference the concrete implementation you use the interface instead. So if you were to use dependency injection, your dependencies would not be the Repository, but the IRepository.
Min
so something like this? `Dim usr As UrbanNow.Core.IUserRepository = New UrbanNow.Core.UserRepository`
rockinthesixstring
he means use an IOC framework such as StructureMap/Ninject to handle the wiring-up in an external location (bootstrapper). Your controller/model only references the interfaces.
RPM1984
A: 

Think that way.

If you put the interface code in the class file, what will happen when you want to create a new class implementing the same interface?

Ghis
is this an answer or a comment?
rockinthesixstring
An answer in a form of question. By answering this question, the orignal poster will figure out by himself the logic behind why it's not a good idea to put the Interface code in the Class file that implement it.
Ghis