Reputation: 746
Lots of answers to this question, if you're using MVC, but what if you're not using MVC? I have a class of Student, and it has all the Crud in it. I can't use any of the examples because of the call to the class. I'm new to Core but have been using Web Forms and MVC for some time.
When I make a call as shown for an example:
Student s = new Student();
s.FullName = "Sam";
s.Update;
I get the error that IConfiguration or IOptions is needed, depending on how I'm trying to call the AppSettings.
Startup.cs
IConfigurationSection appSetting = Configuration.GetSection("AppSettings");
services.Configure<AppSettings>(appSetting);
Student Class
public class Student
{
private readonly IOptions<AppSettings> _appSetting;
public Student(IOptions<AppSettings> appSettings)
{
_appSetting = appSettings;
}
Where it's called in the Student class
public Student GetStudent(Guid StudentGUid)
{
var Conn = _appSetting.Value.ApplicationConn;
using (IDbConnection con = new SqlConnection(Conn))
{
Where I call class on page .cs
Student sa = new Student();
sa.GetStudent(new Guid("2b583b82-0337-425b-ac73-3830bc20253f"));
var s = sa.LastName;
The error:
*There is no argument given that corresponds to the required formal parameter'appSetting .... , which makes sense. But this is where I go off the rails, as I can't just put in the IOption value because it always comes out null.
AppSettings Class
public class AppSettings
{
public string ApplicationID { get; set; }
public string ApplicationUrl { get; set; }
public string ApplicationConn { get; set; }
}
JSON File
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AppSettings": {
"ApplicationID": "Some Guid",
"ApplicationUrl": "xxxxxx",
"ApplicationConn": "Connection String"
},
"AllowedHosts": "*"
Upvotes: 0
Views: 153
Reputation: 1613
You are trying to instantiate a Student object using new Student();
, but in the constructor you are requiring an IOptions<AppSettings>
as an argument, and that's the exception you get. So yeah, it will always come off as null because you are not providing it.
As Ian suggested in his comment, you should reconsider your design. Why does the Student himself would require a connection string? You couple your domain object with database access. What I might propose is a StudentRepository, and I might code it like this:
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
}
}
And you will register it as:
services.AddSingleton<IStudentRepository, StudentRepository>();
Then in your page that you mentioned you will have to inject the IStudentRepository
and use this instead of new Student();
Sidenote: The above proposition might help you test (either in development time or in unit tests) your code, as you can swap out the DB-based implementation with a mock one
Upvotes: 1