I am creating a solution that has an API (Microsoft.NET.Sdk.Web), domain (Microsoft.NET.Sdk), and MVC projects (Microsoft.NET.Sdk.Web).
In my domain I have two classes that look like this:
Code:
public class ApplicationUserQuery
{
private readonly string _connectionString;
public ApplicationUserQuery(string connectionString)
{
_connectionString = connectionString;
}
// ...
}
public class ApplicationUserService
{
private readonly ApplicationUserQuery _applicationUserQuery;
public ApplicationUserService(ApplicationUserQuery applicationUserQuery)
{
_applicationUserQuery = applicationUserQuery;
}
// ...
}
To use the classes in my API project I am doing the following:
Code:
var applicationDatabaseConnectionString = builder.Configuration.GetConnectionString("ApplicationDatabase");
if (applicationDatabaseConnectionString == null)
{
throw new ArgumentNullException(nameof(applicationDatabaseConnectionString));
}
builder.Services.AddScoped(_ => new ApplicationUserQuery(applicationDatabaseConnectionString));
builder.Services.AddScoped<ApplicationUserService>();
This works, but it seems like a lot of boilerplate just for two classes. Is there anyway to simplify this using the built-in .NET core stuff? I'm wanting to avoid using a 3rd party library like Ninject because of issues we recently started running into at work. This solution I'm building is a personal project.