. , ( ), .
:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<DatabaseOptions>(Configuration.GetSection("Data:DefaultConnection"));
services.AddScoped<IRepository<IDataModel>, BaseRepository>();
}
DatabaseOptions
:
public class DatabaseOptions
{
public string ConnectionString { get; set; }
}
DatabaseOptions
BaseRepository
:
public class BaseRepository
{
private readonly DatabaseOptions _options;
public BaseRepository(IOptions<DatabaseOptions> databaseOptions)
{
_options = databaseOptions.Value;
}
}
, BaseRepository
, :
services.AddScoped<PrivacyLevelRepository>();
public class PrivacyLevelRepository : BaseRepository, IRepository<PrivacyLevelDM>
{
public PrivacyLevelRepository(IOptions<DatabaseOptions> databaseOptions)
: base(databaseOptions)
{ }
}
, . , , . , PrivacyLevelRepository
?
PrivacyLevelRepository repo = new PrivacyLevelRepository();
returnValue = repo.GetAllByDomainID(DomainID).ToList();
return returnValue;
, . new
. ( PrivacyLevelRepository
) , .
, PrivacyLevelRepository
, - . . , PrivacyLevelRepository
IOptions<DatabaseOptions>
. , , , , . , , .
, PrivacyLevelRepository
, : , ; - . , , , :
public class MyController
{
private readonly PrivacyLevelRepository _privacyLevelRepository;
public MyController(PrivacyLevelRepository privacyLevelRepository)
{
_privacyLevelRepository = privacyLevelRepository;
}
public IActionResult SomeRoute()
{
var domainId = "whatever";
var data = _privacyLevelRepository.GetAllByDomainID(domainId).ToList();
return View(data);
}
}
, - - . , ASP.NET Core , , - . ConfigureServices
, , , .
, .