简体   繁体   中英

Register simple class in ServiceCollection

In our application we need sometimes some informations about the user context (filled dynamically) or database connexion string (which is dynamic, so not in appsettings.json). These class are very simple and contains only properties, no methods. So we want to use advantages of the Dependency Injection and inject these datas into our services when we need it. Our questions are : - Is the Option pattern THE solution here ? - About this pattern : why always inject IOptions instead of MyType directly ?

IOptions is mainly useful to register dependencies from config files, you can register classes directly without doing that, you just need to also register any dependency objects for each class you register

here is an example just registering a class directly

services.AddScoped<MetaWeblogModelMapper, MetaWeblogModelMapper>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM