DotNetCore cross-platform ~ In the componentization era, dotnetcore

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.