When implementing the repository pattern should lo

2019-07-16 23:31发布

I am creating RESTful services for several database entities based on a modified version of the BISDM. Some of these entities have associated lookup tables, such as depicted below:

Snippet of modified BISDM schema

I have decided to use the repository pattern to provide a clean separation between data persistance / retrieval; however, I am not sure how lookups ( as opposed to entities ) should be represented in the repository.

Should lookups get their own repository interface, "share" one with the associated entity, or should there be a generic ILookupRepository interface?

For the moment, these lookups are read-only; however, there will be a time where we may want to edit the lookups via services.

Option 1:
   ISpaceRepository.GetSpaceCategoryById(string id);
Option 2:
   ISpaceCategoryRepository.GetById(string id);
Option 3:
   ILookupRepository.GetSpaceCategoryById(string id);

Incidentally, this question is related to another one regarding look-up tables & RESTful web services.

1条回答
一夜七次
2楼-- · 2019-07-17 00:38

No. Repositories should represent domain model concepts, not entity level concepts, and certainly not database level. Think about all the things you would want to do with a given component of your domain, for example, Spaces.

One of the things that you'll want to do, is GetSpaceCategories(). This should definitely be included in the Spaces repository, as anyone dealing with Spaces will want access to the Space categories without having to instantiate some other repository.

A generic repository would be fairly counter-productive I would think. Treating a repository like a utility class would virtually guarantee that any moderately complex operation would have to instantiate both repositories.

查看更多
登录 后发表回答