DotNetCore cross-platform ~ In the componentization era, dotnetcore
Back to directory
After dotnetcore is implemented, various objects are produced based on DI. This comes with the object lifecycle. As early as in autofac, there are also related knowledge points, which are similar to Microsoft. extensions. dependencyInjection is completely gentle for everyone to understand. before talking about today's componentization, let's take a look at the three life cycles of DI:
AddSingleton: Singleton, which is unique within a process
AddTransient: instantaneous. It is unique within the scope of the object in the current environment.
AddScoped: Request, the object is unique within an HTTP request
Next let's take a look at today's componentization implementation. For example, I want to encapsulate nosql and register it directly in the Code startup or use the configuration file for registration, we can use this extension method to implement IServiceCollection extension!
/// <Summary> /// nosql service extension /// </summary> public static class NoSqlExtensions {// <summary> /// use Redis /// </ summary> /// <param name = "services"> </param> /// <param name = "options"> </param> /// <returns> </ returns> public static IServiceCollection UseRedis (this IServiceCollection services, action <RedisConfig> options = null) {RedisConfig option = new RedisConfig (); options ?. Invoke (option); ObjectMapper. mapperTo <RedisConfig> (option, ConfigFileHelper. get <RedisConfig> (); // The priority modifier services. addSingleton (option); services. addSingleton <RedisManager, RedisManager> (); return services ;} /// <summary> /// use Mongodb /// </summary> /// <param name = "services"> </param> /// <param name = "options"> </param> // <returns> </returns> public static IServiceCollection UseMongodb (this IServ IceCollection services, Action <MongodbConfig> options = null) {MongodbConfig option = new MongodbConfig (); options ?. Invoke (option); ObjectMapper. mapperTo <MongodbConfig> (option, ConfigFileHelper. get <MongodbConfig> (); // The priority modifier services. addSingleton (option); services. addSingleton <MongodbManager, MongodbManager> (); return services ;}}
It is also very convenient to use in the program. Note that if it is configured in the configuration file, we will take the configuration file as the standard. In this way, in the production environment, your code injection parameters do not need to be commented out or deleted!
# Region service components services. useRabbitMQ (o => {o. exchangeName = "AutoCalculate"; o. mqServerHost = "amqp: // 192.168.200.214: 5672" ;}); services. useRedis (o => {o. host = "192.168.200.214: 6379"; o. authPassword = "2017" ;}); services. useDapper (o => {o. dbType = 2; o. connectionString = "test";}); # endregion
Thank you for reading this article!
Our framework should be componentized!
Our system should be based on microservices!
Our deployment should be based on Automation!
Back to directory