【问题标题】:Getting Dot.Net Core AppSettings in a Class, and I'm not using MVC在一个类中获取 Dot.Net Core AppSettings,我没有使用 MVC
【发布时间】:2020-04-16 07:31:06
【问题描述】:

这个问题有很多答案,如果您使用的是 MVC,但如果您不使用 MVC 怎么办?我有一堂Student,里面有所有的Crud。由于对类的调用,我无法使用任何示例。我是 Core 新手,但使用 Web 表单和 MVC 已有一段时间了。

当我拨打电话时,例如:

Student s = new Student();
s.FullName = "Sam";
s.Update;

我收到需要 IConfiguration 或 IOptions 的错误,这取决于我尝试调用 AppSettings 的方式。

Startup.cs

   IConfigurationSection appSetting = Configuration.GetSection("AppSettings");
   services.Configure<AppSettings>(appSetting);

学生班

   public class Student
     {
         private readonly IOptions<AppSettings> _appSetting;

              public Student(IOptions<AppSettings> appSettings)
              {
                   _appSetting = appSettings;
              }

它在 Student 类中的调用位置

public Student GetStudent(Guid StudentGUid)
      {
           var Conn = _appSetting.Value.ApplicationConn;

           using (IDbConnection con = new SqlConnection(Conn))
           {

我在页面 .cs 上调用类的地方

      Student sa = new Student();
      sa.GetStudent(new Guid("2b583b82-0337-425b-ac73-3830bc20253f"));
      var s = sa.LastName;

错误:

*没有给出与所需形参'appSetting ....相对应的参数,这是有道理的。但这是我出轨的地方,因为我不能只输入 IOption 值,因为它总是为空。

AppSettings 类

public class AppSettings
     {
          public string ApplicationID { get; set; }
          public string ApplicationUrl { get; set; }
          public string ApplicationConn { get; set; }
     }

JSON 文件

 "Logging": {
      "LogLevel": {
           "Default": "Information",
           "Microsoft": "Warning",
           "Microsoft.Hosting.Lifetime": "Information"
      }
 },
 "AppSettings": {
      "ApplicationID": "Some Guid",
      "ApplicationUrl": "xxxxxx",
      "ApplicationConn": "Connection String"
 },
 "AllowedHosts": "*"

【问题讨论】:

  • 为什么Student 需要了解应用程序设置?看来您的设计是这里的问题。
  • 这是我拉连接字符串的地方。
  • 同样,Student 对象永远不需要知道连接字符串——这不是它的责任。相反,您应该拥有某种存储库来检索和持久化Student 对象。
  • 我明白了,但是我们的对象类是自包含的,其中包含 CRUD 操作。因此需要一个连接字符串,app Id ... ectra

标签: c# asp.net asp.net-core .net-core


【解决方案1】:

您正在尝试使用 new Student(); 实例化一个 Student 对象,但在构造函数中您需要一个 IOptions&lt;AppSettings&gt; 作为参数,这就是您得到的异常。所以,是的,它总是会显示为 null,因为您没有提供它。

正如 Ian 在他的评论中建议的那样,您应该重新考虑您的设计。为什么学生本人需要连接字符串?您将域对象与数据库访问相结合。我可能建议的是 StudentRepository,我可能会这样编码:

public interface IStudentRepository
{
   Student GetStudentById(Guid studentId);
}
public class StudentRepository : IStudentRepository 
{
   private readonly IOptions<AppSettings> _appSetting;

   public StudentRepository (IOptions<AppSettings> appSettings)
   {
      _appSetting = appSettings;
   }

   public Student GetStudentById(Guid studentId)
   {
      // your code here
   }
}

您将其注册为: services.AddSingleton&lt;IStudentRepository, StudentRepository&gt;();

然后在您提到的页面中,您必须注入IStudentRepository并使用它而不是new Student();

旁注: 上述提议可能会帮助您测试(在开发时或在单元测试中)您的代码,因为您可以将基于 DB 的实现替换为模拟的实现

【讨论】:

  • 这是我试图避免的,将 CRUD 方法从 Student 类中拉出。
  • 那么在调用 StudentRepository 时会不会遇到同样的问题,它需要 IOptions?
  • 是的,但是您不必每次都创建一个新实例,您将拥有一个实例,并且 AppSettings 将被注入到构造函数中。您构建 Student 类的方式,您需要一个 IOptions 但您从不提供它,您使用 new Student();。另外,您说您没有使用 MVC,而是在谈论页面。您使用的是什么技术?
  • 带有 Razor 页面的 Asp.net 核心 Web 应用程序......不是 MVC
  • 也许这会解决问题:stackoverflow.com/questions/47463206/…
猜你喜欢
  • 1970-01-01
  • 2021-01-05
  • 2011-09-12
  • 2015-01-27
  • 1970-01-01
  • 2021-09-08
  • 2019-06-12
  • 2021-05-07
  • 1970-01-01
相关资源
最近更新 更多