I'm trying to learn better software design and recently found the Repository and Service Layer patterns. From my understanding, the repository basically contains the data access code and the service layer calls the repository to get that data and then performs some logic and processing to that data.
So far from reading up on these there is, generally, not a set series of methods that the repository has. However, the repository usually has methods along these lines:
I'm trying to understand the naming conventions for repositories. What should I call the "List/Get/Read/etc." method? I'll give an example.
I'm currently working on a project that will read from a bunch of directories and files. These files represent sensor readings that are being generated by a completely separate and already existing system.
Should method names be specific to that particular type of repository or go for more generic sounding names? Such as:
Generic names:
interface ISensorRepository
{
IEnumerable<Sensor> GetAll(); / IEnumerable<Sensor> ListAll(); / etc.
Sensor GetByID(int id);
}
Entity specific names:
interface ISensorRepository
{
IEnumerable<Sensor> GetAllSensors(); / IEnumerable<Sensor> ListAllSensors(); / etc.
Sensor GetSensorByID(int id);
}
I'll go with OP's first option the "generic naming"
Why? its to avoid smurf naming convention (it's a programming jargon), its what you do when you keep repeating yourself.
You already named the type of entity the repository represents, why go through all the work repeating yourself? If you stick with Single Responsibility Principle, you don't have to worry about your abstraction being leaky.
The following is what I try to avoid:
Employee
- EmployeeID
- EmployeeFirstName
- EmployeeLastName
- EmployeeAddress
- EmployeeNumber
- EmployeeAge
- EmployeeEmail
- EmployeeHiringDate
- EmployeeThis
- EmployeeThat
- EmployeeBlabla
- Employee...
- Employee...
Its kind of crazy right?
Martin Fowler in POEAA defined Repository
as a mediator between domain and data mapping layers and that it acts like an in-memory domain object collection.
As it should act like an in memory collection, I like naming my methods as follows.
public interface IRepository<T> where T : class {
T Create(T entity);
void Create(IList<T> entities);
T Update(T entity);
T FirstOrDefault(Expression<Func<T, bool>> clause);
IEnumerable<T> Where(Expression<Func<T, bool>> clause);
IEnumerable<TResult> Select<TResult>(Expression<Func<T, TResult>> selector);
T First();
IEnumerable<T> All();
}
See http://martinfowler.com/eaaCatalog/repository.html for a short discussion around the pattern
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With