选项模式 - Interface/Abstract 属性
Options pattern - Interface/Abstract property
我想将以下对象与 ServiceCollection 中的 appsettings.json 数据绑定,但是我无法更改设计class 或界面:
public class TransferOptions :ITransferOptions
{
public IConnectionInfo Source {get;set;}
public IConnectionInfo Destination {get;set;}
}
public class ConnectionInfo : IConnectionInfo
{
public string UserName{get;set;}
public string Password{get;set;}
}
public interface ITransferOptions
{
IConnectionInfo Source {get;set;}
IConnectionInfo Destination {get;set;}
}
public interface IConnectionInfo
{
string UserName{get;set;}
string Password{get;set;}
}
这是我在appsettings.json
中的数据
{
"TransferOptions":{
"Source ":{
"UserName":"USERNAME",
"Password":"PASSWORD"
},
"Destination":{
"UserName":"USERNAME",
"Password":"PASSWORD"
}
}
}
这是我在服务提供商上的配置:
var provider=new ServiceCollection()
.Configure<TransferOptions>(options => _configuration.GetSection("TransferOptions").Bind(options))
.BuildServiceProvider();
这是我出错的部分
Cannot create instance of type 'IConnectionInfo' because it is either abstract or an interface:
var transferOptions =_serviceProvider.GetService<IOptions<TransferOptions>>()
An interface cannot be instantiated directly.
因此,如果您不能更改 class' 设计 - 即使用 ConnectionInfo 的具体实现 - 我想您可以实例化 ConnectionInfo class 并将其分配给IConnectionInfo 接口如下:
public class TransferOptions : ITransferOptions
{
public IConnectionInfo Source { get; set; } = new ConnectionInfo();
public IConnectionInfo Destination { get; set; } = new ConnectionInfo();
}
由于接口和规定的限制,您需要自己构建选项成员
var services = new ServiceCollection();
IConnectionInfo source = _configuration.GetSection("TransferOptions:Source").Get<ConnectionInfo>();
IConnectionInfo destination = _configuration.GetSection("TransferOptions:Destination").Get<ConnectionInfo>();
services.Configure<TransferOptions>(options => {
options.Source = source;
options.Destination = destination;
});
var provider = services.BuildServiceProvider();
我想将以下对象与 ServiceCollection 中的 appsettings.json 数据绑定,但是我无法更改设计class 或界面:
public class TransferOptions :ITransferOptions
{
public IConnectionInfo Source {get;set;}
public IConnectionInfo Destination {get;set;}
}
public class ConnectionInfo : IConnectionInfo
{
public string UserName{get;set;}
public string Password{get;set;}
}
public interface ITransferOptions
{
IConnectionInfo Source {get;set;}
IConnectionInfo Destination {get;set;}
}
public interface IConnectionInfo
{
string UserName{get;set;}
string Password{get;set;}
}
这是我在appsettings.json
中的数据{
"TransferOptions":{
"Source ":{
"UserName":"USERNAME",
"Password":"PASSWORD"
},
"Destination":{
"UserName":"USERNAME",
"Password":"PASSWORD"
}
}
}
这是我在服务提供商上的配置:
var provider=new ServiceCollection()
.Configure<TransferOptions>(options => _configuration.GetSection("TransferOptions").Bind(options))
.BuildServiceProvider();
这是我出错的部分
Cannot create instance of type 'IConnectionInfo' because it is either abstract or an interface:
var transferOptions =_serviceProvider.GetService<IOptions<TransferOptions>>()
An interface cannot be instantiated directly.
因此,如果您不能更改 class' 设计 - 即使用 ConnectionInfo 的具体实现 - 我想您可以实例化 ConnectionInfo class 并将其分配给IConnectionInfo 接口如下:
public class TransferOptions : ITransferOptions
{
public IConnectionInfo Source { get; set; } = new ConnectionInfo();
public IConnectionInfo Destination { get; set; } = new ConnectionInfo();
}
由于接口和规定的限制,您需要自己构建选项成员
var services = new ServiceCollection();
IConnectionInfo source = _configuration.GetSection("TransferOptions:Source").Get<ConnectionInfo>();
IConnectionInfo destination = _configuration.GetSection("TransferOptions:Destination").Get<ConnectionInfo>();
services.Configure<TransferOptions>(options => {
options.Source = source;
options.Destination = destination;
});
var provider = services.BuildServiceProvider();