Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How should I do to make my DI register code more clean?

$
0
0

I am using the default service container in asp.net core 1.0 rc2.  In my project, I need to register many services, like code below.          

//for all type not inherit Asset
services.AddSingleton(typeof(IProvider<>), typeof(Provider<>)); //for those :Asset
services.AddSingleton<IProvider<Asset>, AssetProvider<Asset>>();
services.AddSingleton<IProvider<BastionHostAsset>, AssetProvider<BastionHostAsset>>();
services.AddSingleton<IProvider<CloudAccountAsset>, AssetProvider<CloudAccountAsset>>();
services.AddSingleton<IProvider<DeviceAsset>, AssetProvider<DeviceAsset>>();
services.AddSingleton<IProvider<LoadBalanceAsset>, AssetProvider<LoadBalanceAsset>>();
services.AddSingleton<IProvider<VMAsset>, AssetProvider<VMAsset>>();
services.AddSingleton<IProvider<StoreAllocAsset>, AssetProvider<StoreAllocAsset>>();

Unlike all the generic types register at the first line code, I have to register every type which inherits  Asset  line by line,  boring work.

Can I improve the code more clean? I  wish there is one line code to registerall the providers for types inheriting Asset, may be like below:

services.AddSingleton<IProvider<T>,AssetProvider<T>> where T: Asset 

How should I improve my code ?

Thanks.


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>