Ç°ÑÔ
ÔÚÉÏһƪÎÄÕÂÖÐдÁËÈçºÎÔÚMVCÖÐÅäÖÃÈ«¾Ö·ÓÉǰ׺£¬½ñÌì¸ø´ó¼Ò½éÉÜÒ»ÏÂÈçºÎÔÚÔÚ json ÎļþÖÐÅäÖÃÒÀÀµ×¢Èë¡£
ÔÚÒÔÇ°µÄ ASP.NET 4+ £¨MVC£¬Web Api£¬Owin£¬SingalRµÈ£©Ê±ºò£¬¶¼ÊÇÌṩÁËרÓеĽӿÚÒÔ¹©Ê¹ÓõÚÈý·½µÄÒÀÀµ×¢Èë×é¼þ£¬±ÈÈçÎÒÃdz£ÓõĻáʹÓà Autofac¡¢Untiy¡¢String.Net µÈ£¬ÕâЩµÚÈý·ÅÒÀÀµ×¢Èë×é¼þ»ù±¾É϶¼ÌṩÁËÒ»Ì×ÅäÖÃ×¢Èë»òÕßÅäÖÃÉúÃüÖÜÆڵķ½Ê½£¬³ýÁËÖ±½ÓÅäÖõ½ÀàÀïÃæÖ®Í⣬»¹ÌṩÁËҪôʹÓà xml Îļþ£¬ÒªÃ´Ê¹Óà json µÈ£¬ÄÇôÔÚÐ嵀 ASP.NET Core ÖÐ΢ÈíÒѾĬÈϵĸøÎÒÃÇÌṩÁËÒ»¸öÒÀÀµ×¢ÈëµÄ¹¦ÄÜ£¬ÎÒÃǾͲ»ÔÙÐèÒª½èÖúÓÚµÚÈý·½×é¼þÀ´ÊµÏÖÒÀÀµ×¢ÈëÁË£¬µ«ÊÇÓÐʱºòÎÒÃÇÏëÔÚÅäÖÃÎļþÖÐÀ´ÅäÖÃÒÀÀµ×¢È룬΢Èí±¾ÉíµÄ DI ×é¼þ²¢Ã»ÓиøÎÒÃÇÌṩһ¸ö¿É¹©ÅäÖõÄÎļþ£¬ÄÇôÎÒÃǾÍÐèÒª×Ô¼ºÀ´ÊµÏÖÕâ¸öÅäÖÃÏîµÄ¹¦ÄÜ¡£¸öÈ˾õµÃÆäÖ÷ҪʹÓó¡¾°ÊÇһЩÔÚ±àÒëʱ²»ÄÜÈ·¶¨ÊµÏֵģ¬ÐèÒª¶¯Ì¬ÐÞ¸ÄʵÏֵĵط½¡£
ÏÂÃæ¾ÍÀ´¿´¿´Ó¦¸ÃÈçºÎÀ´×öÕâ¼þÊÂÇé°É¡£
Getting Started
Ê×ÏÈ£¬ÔÚÓ¦ÓóÌÐòÖÐÎÒÃÇ´´½¨Ò»¸ö½Ó¿Ú£¬ÒÔ¹© DIʹÓãº
public interface IFoo { string GetInputString(string input); }
È»ºó£¬Ìí¼ÓÒ»¸ö IFoo ½Ó¿ÚµÄʵÏÖ Foo
public class Foo : IFoo { public string GetInputString(string input) { return $"ÊäÈëµÄ×Ö·û´®Îª£º{ input }"; } }
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª°ÑÒÔÉ쵀 IFoo ½Ó¿ÚºÍËüµÄʵÏÖÌí¼Óµ½ Startup.cs ÎļþÖеÄConfigureServices·½·¨ÖУ¬ConfigureServices Ö÷ÒªÊÇÓÃÀ´ÅäÖÃÒÀÀµ×¢Èë·þÎñµÄ¡£È»ºóͨ¹ý¸Ã·½·¨ÌṩµÄISerciceCollection½Ó¿Ú²ÎÊý×¢Èë Services¡£
public void ConfigureServices(IServiceCollection services) { services.Add(new ServiceDescriptor(serviceType: typeof(IFoo), implementationType: typeof(Foo), lifetime: ServiceLifetime.Transient)); }
ÕâÀÎÒÃÇʹÓõ½ÁË IServiceCollection ÀïÃæµÄ Add ·½·¨£¬Ìí¼ÓÒ»¸öÉúÃüÖÜÆÚΪ˲̬µÄ IFoo µÄʵÏÖ¡£Ë²Ì¬¾ÍÊÇ˵ÔÚÿ´ÎÇëÇóµÄʱºò¶¼½«´´½¨Ò»¸öFooµÄʵÀý¡£
ÒÔÉÏÊÇĬÈÏ΢ÈíΪÎÒÃÇÌṩµÄÌí¼ÓÒÀÀµ×¢ÈëµÄ·½·¨£¬ÏÂÃæÎÒÃÇÀ´¿´Ò»ÏÂÔõôÀ´¸ÄÔì³ÉÎÒÃÇÐèÒªµÄʹÓà json ÎļþµÄ·½Ê½¡£
ʹÓà json ÎļþÅäÖà DI
µ±ÎÒÃÇʹÓÃjsonÎļþÅäÖÃÒÀÀµ×¢ÈëµÄʱºò£¬¿ÉÒÔÑ¡Ôñн¨Ò»¸öjsonÎļþ£¬Ò²¿ÉÒÔÖ±½ÓʹÓà appsettings.json Îļþ¡£ÏÖÔÚÎÒÃǾÍÖ±½ÓÔÚ appsettings.json ÎļþÖÐÌí¼Ó¹ØÓÚDIµÄÅäÖÃÁË¡£
appsettings.json
"Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Debug", "System": "Information", "Microsoft": "Information" } }, "DIServices": [ { "serviceType": "[namesapce].IFoo", "implementationType": "[namesapce].Foo", "lifetime": "Transient" } ] }
Ê×ÏÈ£¬Ìí¼ÓÒ»¸öÃûΪ ¡°DIServices¡± µÄÊý×é½Úµã£¬Êý×éÖаüº¬Ò»¸ö»ò¶à¸öÅäÖÃserviceµÄ¶ÔÏó£¬serviceType´ú±í·þÎñ½Ó¿ÚµÄÀàÐÍ£¬implementationType½Ó¿ÚµÄʵÏÖ£¬lifetime ³õʼ»¯ÊµÀýµÄÉúÃüÖÜÆÚ¡£
×¢Ò⣺ÅäÖÃÎļþÖеÄÀàÐͱØÐëΪȫÃû³Æ£¬¼´°üº¬ÃüÃû¿Õ¼ä¡£
½ÓÏÂÀ´£¬Ìí¼ÓÒ»¸öºÍJsonÎļþÅäÖÃÏîÏà¶ÔÓ¦µÄÒ»¸öserviceÀ࣬ÕâÀïÎÒÃÇÐèҪʹÓà Newtonsoft Õâ¸öjson¿â¡£
using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; using Newtonsoft.Json.Converters; public class Service { public string ServiceType { get; set; } public string ImplementationType { get;set; } [JsonConverter(typeof(StringEnumConverter))] public ServiceLifetime Lifetime { get; set; } }
È»ºóÐèÒª¸ÄÔìÒ»ÏÂConfigureServices£¬ÔÚ ConfigureServices ÖжÁÈ¡ÅäÖÃµÄ jsonÎļþ¼´¿É¡£
public void ConfigureServices(IServiceCollection services) { //services.Add(new ServiceDescriptor(serviceType: typeof(IFoo), // implementationType: typeof(Foo), // lifetime: ServiceLifetime.Transient)); var jsonServices = JObject.Parse(File.ReadAllText("appSettings.json"))["DIServices"]; var requiredServices = JsonConvert.DeserializeObject<List<Service>>(jsonServices.ToString()); foreach (var service in requiredServices) { services.Add(new ServiceDescriptor(serviceType: Type.GetType(service.ServiceType), implementationType: Type.GetType(service.ImplementationType), lifetime: service.Lifetime)); } }
È»ºóÎÒÃDzâÊÔÒ»ÏÂÊÇ·ñÊÇ¿ÉÓõġ£
²âÊÔ
´ò¿ª HomeController.cs £¬Ìí¼Ó×¢ÈëÏ
public class HomeController : Controller { private readonly IFoo _foo; public HomeController(IFoo foo) { _foo = foo; } public IActionResult About() { ViewData["Message"] = _foo.GetInputString("Your application description page."); return View(); } }
ÔÚ HomeControllerµÄ¹¹Ô캯ÊýÌí¼ÓIFoo½Ó¿Ú£¬È»ºóÔÚ About µÄActionÖÐʹÓá£
ÔËÐгÌÐò£¬´ò¿ªÒ³Ã棬µã»÷ About±êÇ©
×ܽá
ÒÔÉϼ´ÎªÔÚ ASP.NET Core ÖÐÅäÖÃÒÀÀµ×¢Èëµ½jsonÎļþÖУ¬ÕâÖ»ÊÇÒ»¸ö¼òµ¥µÄʵÀý£¬²»ÒªÓÃÔÚÉú²ú»·¾³ÖС£ÔÚʵ¼ÊµÄÏîÄ¿ÖÐÄ㻹ÐèÒª´¦Àí¹ØÓÚ¶ÁÈ¡ÅäÖÃÒì³£Çé¿ö£¬·þÎñÊÇ·ñ´æÔÚµÄÒì³£Çé¿ö£¬ÉúÃüÖÜÆڵȵÈÕâЩÎÊÌâ¡£
¡¡