liveBook · Manning

Open generic

As explained in ASP.NET Core in Action, 3e

An open generic is a type or type definition that contains one or more unspecified generic type parameters.

An open generic is a type definition with one or more unspecified generic parameters, such as Repository<T>. It defines reusable behavior for multiple closed generic types. section 9.2

In dependency injection, register open generics with the non-generic Add* overload. The container can then match IRepository<Customer> with DbRepository<Customer> when constructing a service. section 9.2

Registering an open generic

builder.Services.AddScoped(typeof(IRepository<>), typeof(DbRepository<>));

public record EmailServerSettings(string Host, int Port);

Compare the open-generic registration with ordinary generic registration methods, which cannot register open generics. section 9.2

How ASP.NET Core in Action, 3e covers it

How deep it goes
Defines open generics and demonstrates one registration, but stops short of detailed generic type mechanics.
Examples
Uses IRepository<T> and DbRepository<T> to illustrate matching generic service requests and implementations.
What it assumes
Assumes familiarity with generic types and dependency injection while distinguishing open from constructed generic types.
What you can do after
Apply open-generic registration through IServiceCollection and the non-generic AddScoped overload.
How it connects
Connects open generics with dependency injection, generic types, service containers, and constructor injection.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.