Problem statement
Complete the implementation of AddEntityRepositories so that every entity type in the application is stored through the one generic repository, and no entity type needs a registration line of its own.
Sample Data
An entity is any class that carries an Id, which the provided IEntity declares. The back office keeps orders and customers, and both are entity types. An order record carries an id, a reference and a total, and a customer record carries an id, a name and a region. Nothing is stored when the application starts.
Service
The repository
IRepository<T> is the storage contract for one entity type. It reports the type it stores as EntityType and the identity of the repository object itself as InstanceId, and it holds entities through Add, Get and All. InMemoryRepository<T> is its one implementation, and it serves every entity type. The provided controllers are handed an IRepository<Order> and an IRepository<Customer>, and nothing hands them one yet.
ServiceCollectionExtensions
Program calls AddEntityRepositories while the application is being built. Implement that method so that:
- Asking for IRepository<T> hands back an InMemoryRepository<T> closed over that same T, whichever entity type T is. That includes entity types the samples below never mention and entity types this application does not declare, so adding an entity type later needs no further work here.
- The method adds exactly one registration, and that registration names no entity type in particular.
- Asking a scope for the same IRepository<T> twice hands back the same repository both times, and two scopes get repositories of their own, with their own InstanceId. A request is a scope, so a collection filled during one request is empty again in the next one.
- Each entity type gets a repository of its own inside a scope, so an entity stored under one type is not reachable through another.
Sample Requests & Responses
GET /orders
Answers the order collection the request holds, naming the type it stores and the repository that served the request. A request that has stored nothing holds nothing.
Response code: 200
{
"entityType": "Order",
"repositoryId": "7784bec9-753e-4019-b93d-5415a009afc4",
"items": []
}
POST /orders
Stores the posted order and answers the collection as it then stands, in that same shape.
Request body:
{ "id": 7, "reference": "SO-7", "total": 120.50 }
Response code: 200
{
"entityType": "Order",
"repositoryId": "ea4bb981-5981-4654-a38d-f7326b57216f",
"items": [
{ "id": 7, "reference": "SO-7", "total": 120.50 }
]
}
The rest of the surface follows from those two:
- GET /orders asked again after that POST answers an empty items once more, under a repositoryId that matches neither of the two above, because it is a third request and so a third scope.
- POST /customers stores a customer record and GET /customers answers the customer collection, both reporting Customer as their entityType and a repositoryId of their own.
