StaffCoder
.NET
.NET.NET 10
12 modules · 48 challengesSign up
Module 8 · Advanced Dependency Injection

Open Generic Repository Registration

medium—No data yet

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.

More in Advanced Dependency Injection

See the module
Factory-Based RegistrationThis problem covers wiring up a service whose starting value is only known once the application is running, by handing the container a piece of code that builds the service instead of a ready-made object.—No data yetmediumKeyed Payment ProvidersThis problem covers wiring up several payment providers that share one interface so the checkout picks the right one for the payment method it was handed.—No data yetmediumPluggable Export EngineThis problem covers wiring up a set of report exporters so the right one is picked for the format that was asked for, the whole set can be listed, and the one that needs a value only known at run time is still built correctly.—No data yetmedium

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.