use*_*192 10 c# asp.net asp.net-mvc asp.net-core-mvc asp.net-core
我正在努力解决ASP.NET 5(vNext)中的一些概念.
其中之一是用于配置的依赖注入方法.好像我必须在堆栈中一直传递一个参数.我可能误解了某些事情或做错了.
想象一下,我有一个名为"contactEmailAddress"的配置属性.我会使用该配置属性在发出新订单时发送电子邮件.考虑到这种情况,我的ASP.NET 5堆栈将如下所示:
Startup.cs
public class Startup
{
public IConfiguration Configuration { get; set; }
public Startup(IHostingEnvironment environment)
{
var configuration = new Configuration().AddJsonFile("config.json");
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings"));
services.AddMvc();
}
public void Configure(IApplicationBuilder app)
{
app.UseErrorPage();
app.UseMvc(routes =>
{
routes.MapRoute("default",
"{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index" });
}
);
app.UseWelcomePage();
}
Run Code Online (Sandbox Code Playgroud)
AppSettings.cs
public class AppSettings
{
public string ContactEmailAddress { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
config.json
{
"AppSettings": {
"ContactEmailAddress":"support@mycompany.com"
}
}
Run Code Online (Sandbox Code Playgroud)
OrderController.cs
[Route("orders")]
public class OrdersController : Controller
{
private IOptions<AppSettings> AppSettings { get; set; }
public OrdersController(IOptions<AppSettings> appSettings)
{
AppSettings = appSettings;
}
[HttpGet("new-order")]
public IActionResult OrderCreate()
{
var viewModel = new OrderViewModel();
return View(viewModel);
}
[HttpPost("new-order")]
public IActionResult OrderCreate(OrderViewModel viewModel)
{
return new HttpStatusCodeResult(200);
}
}
Run Code Online (Sandbox Code Playgroud)
Order.cs
public class Order()
{
public void Save(IOptions<AppSettings> appSettings)
{
// Send email to address in appSettings
}
public static List<Order> FindAll(IOptions<AppSettings> appSettings)
{
// Send report email to address in appSettings
return new List<Order>();
}
}
Run Code Online (Sandbox Code Playgroud)
如上例所示,我正在AppSettings通过整个堆栈.这感觉不正确.为了进一步担心,如果我尝试使用需要访问配置设置的第三方库,这种方法将无效.第三方库如何访问配置设置?我误会了什么吗?有一个更好的方法吗?
Dav*_*ich 11
您正在纠缠2个不同的运行时资源提供程序,AppSettings和依赖注入.
AppSettings,提供对特定于应用程序的值的运行时访问,如UICulture字符串,联系电子邮件等.
DI容器是管理服务访问及其生命周期范围的工厂.例如,如果 MVC Controller需要访问您的 EmailService,您将进行配置
public void ConfigureServices(IServiceCollection services)
{
// Add all dependencies needed by Mvc.
services.AddMvc();
// Add EmailService to the collection. When an instance is needed,
// the framework injects this instance to the objects that needs it
services.AddSingleton<IEmailService, EmailService>();
}
Run Code Online (Sandbox Code Playgroud)
然后,如果我们的Home Controller需要访问你的EmailService,我们通过将它作为参数添加到Controller构造函数来添加对它的接口的依赖
public class HomeController : Controller
{
private readonly IEmailService _emailService;
private readonly string _emailContact;
/// The framework will inject an instance of an IEmailService implementation.
public HomeController(IEmailService emailService)
{
_emailService = emailService;
_emailContact = System.Configuration.ConfigurationManager.
AppSettings.Get("ContactEmail");
}
[HttpPost]
public void EmailSupport([FromBody] string message)
{
if (!ModelState.IsValid)
{
Context.Response.StatusCode = 400;
}
else
{
_emailService.Send(_emailContact, message);
Run Code Online (Sandbox Code Playgroud)
Dependancy Injection的目的是管理服务的访问和生命周期.
在前面的例子,在我们的应用Startup中,我们配置了DI厂应用请求相关联IEmailService用EmailService.因此,当我们的控制器由MVC框架实例化时,框架会注意到我们的Home Controller 期望IEmailService,框架会检查我们的Application Services Collection.它找到映射指令并将一个Singleton EmailService(占用接口的后代)注入我们的Home Controller.
Super Polymorphic Factorific - alodocious!
为什么这很重要?
如果您的联系电子邮件发生更改,则更改该AppSetting值并完成.所有对"ContactEmail"的请求ConfigurationManager都是全局更改的.字符串很容易.我们可以哈希时不需要注入.
如果您的存储库,电子邮件服务,日志记录服务等发生更改,您需要一种全局方式来更改对此服务的所有引用.服务引用不像不可变字符串文字那样容易转移.服务实例化应由工厂处理以配置服务的设置和依赖关系.
所以,在一年中你开发了一个 RobustMailService:
Class RobustMailService : IEmailService
{
....
}
Run Code Online (Sandbox Code Playgroud)
只要您的新RobustMailService继承并实现了IEmailService接口,您就可以通过更改以下内容替换所有对您的邮件服务的引用:
public void ConfigureServices(IServiceCollection services)
{
// Add all dependencies needed by Mvc.
services.AddMvc();
// Add RobustMailService to the collection. When an instance is needed,
// the framework injects this instance to the objects that needs it
services.AddSingleton<IEmailService, RobustMailService>();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7628 次 |
| 最近记录: |