从接口使用特定服务
这个问题是针对应用程序控制台和.NET Framework 4.7
我有 1 个接口
public interface IPrepareDataService<T>
{
string Name{ get; }
IEnumerable<T> GetImportData();
}
并且来自该接口的 3 个类
public class APrepareDataService : IPrepareDataService<T>
{
public string Name{ get => "A"; }
public IEnumerable<T> GetImportData()
{
return GetRecords<T>();
}
}
public class BPrepareDataService : IPrepareDataService<T>
{
public string Name{ get => "B"; }
public IEnumerable<T> GetImportData()
{
return GetRecords<T>();
}
}
public class CPrepareDataService : IPrepareDataService<T>
{
public string Name{ get => "C"; }
public IEnumerable<T> GetImportData()
{
return GetRecords<T>();
}
}
T 类对于每个类都可以不同。
public class AClass
{
public string Name{ get; set; }
public string Desc{ get; set; }
}
public class BClass
{
public string Name{ get; set; }
public string Surname{ get; set; }
}
public class CClass
{
public string Name{ get; set; }
public int Age{ get; set; }
}
在program.cs中,我已经注册了接口服务:
private static readonly ServiceProvider _serviceProvider =
new ServiceCollection()
.AddSingleton<IPrepareDataService<AClass>, APrepareDataService>()
.AddSingleton<IPrepareDataService<BClass>, BDataService>()
.AddSingleton<IPrepareDataService<CClass>, CPrepareDataService>()
现在问题来了,在主函数中,我从配置文件中获取一个定义我要使用的服务的值:
var prepareDataService = _serviceProvider.GetServices<IPrepareDataService<AClass>>().First(x => x.Name.ToUpper() == ValueFromConfig.ToUpper());
问题是当我尝试获取服务时,我必须设置一个特定的类:
_serviceProvider.GetServices<IPrepareDataService<AClass>>
,所以如果 ValueFromconfig = A,它会正常工作,但如果它是 B,它就会以正确的方式工作。
问题是:有没有办法获取与配置中的值相关的接口的 GetService 如果 value = A 那么接口为 AClass,如果 B 接口为 BClass?
提前致谢
this question is for an app console and .NET framework 4.7
I got 1 interface
public interface IPrepareDataService<T>
{
string Name{ get; }
IEnumerable<T> GetImportData();
}
And 3 classes from this interface
public class APrepareDataService : IPrepareDataService<T>
{
public string Name{ get => "A"; }
public IEnumerable<T> GetImportData()
{
return GetRecords<T>();
}
}
public class BPrepareDataService : IPrepareDataService<T>
{
public string Name{ get => "B"; }
public IEnumerable<T> GetImportData()
{
return GetRecords<T>();
}
}
public class CPrepareDataService : IPrepareDataService<T>
{
public string Name{ get => "C"; }
public IEnumerable<T> GetImportData()
{
return GetRecords<T>();
}
}
T class can be different for every class.
public class AClass
{
public string Name{ get; set; }
public string Desc{ get; set; }
}
public class BClass
{
public string Name{ get; set; }
public string Surname{ get; set; }
}
public class CClass
{
public string Name{ get; set; }
public int Age{ get; set; }
}
In program.cs i've registered services of interface:
private static readonly ServiceProvider _serviceProvider =
new ServiceCollection()
.AddSingleton<IPrepareDataService<AClass>, APrepareDataService>()
.AddSingleton<IPrepareDataService<BClass>, BDataService>()
.AddSingleton<IPrepareDataService<CClass>, CPrepareDataService>()
Problem comes now, in Main function i get from config file a value that defines what service im gonna use:
var prepareDataService = _serviceProvider.GetServices<IPrepareDataService<AClass>>().First(x => x.Name.ToUpper() == ValueFromConfig.ToUpper());
Problem is that i have to set an specific class when i try to get service:
_serviceProvider.GetServices<IPrepareDataService<AClass>>
, so if ValueFromconfig = A, it's gonna work fine, but if it's B it doenst works in a right way.
Question is: is there any way to GetService for the interface related with the value in config if value = A Then interface for AClass, if B interface for BClass?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以执行一次此操作,也可以在运行时执行多次。
要多次执行此操作(我认为这就是您想要的),您可以使用 GetService 重载来传入类型而不是使用泛型。像这样的事情:
要做一次(例如,一旦您的应用程序启动,读取配置,然后就完成了),您需要有选择地绑定服务集合。
You can do this once, or at runtime multiple times.
To do it multiple times (And I think this is what you want), you can use the GetService overload to pass in a type rather than using the generic. Something like :
To do it once (e.g. Once your app starts up, read the config, and then you are done), you would need to selectively bind the service collection.