C# 使用统一依赖框架解析所有方法

C# 使用统一依赖框架解析所有方法,c#,dependency-injection,unity-container,C#,Dependency Injection,Unity Container,我是依赖注入和统一框架的新手。我的问题是-我有接口 public interface INofifyEventDataService { void StatusUpdate (object objectType, JobStatus status = JobStatus.None,string messageTitle=null ,string MessageDetails=null); } 课程是 Public class A : INofifyEventDataService {

我是依赖注入和统一框架的新手。我的问题是-我有接口

public interface INofifyEventDataService
{
    void StatusUpdate (object objectType, JobStatus status = JobStatus.None,string messageTitle=null ,string MessageDetails=null);
}
课程是

Public class A :  INofifyEventDataService
{ 
 void StatusUpdate (object objectType, JobStatus status = JobStatus.None,string messageTitle=null ,string MessageDetails=null)
{ //implementation form A}}
B班呢

Public class B:  INofifyEventDataService
{ 
 void StatusUpdate (object objectType, JobStatus status = JobStatus.None,string messageTitle=null ,string MessageDetails=null)
{ //implementation form B}}
对于数据库记录器(假设为c类)

据我所知,我会像这样解决它

IUnityContainer myContainer = new UnityContainer();
myContainer.RegisterType<INofifyEventDataService, DBLogger >();
myContainer.RegisterType<INofifyEventDataService, classA>("A");
myContainer.RegisterType<INofifyEventDataService, classB>("B");

我的要求是从单个resolve对象调用每个类StatusUpdate。?

如果我正确理解了这个问题,您希望在
INotifyEventDataService
的所有实例上调用
StatusUpdate
,但只需一次调用,而不必循环调用
ResolveAll
返回的列表。这在C#中是不可能的;您可以使用一个方法来实现它,但在内部,该方法仍然使用循环

例如,您可以使用
List.ForEach

List<INofifyEventDataService> serviceList = myContainer.ResolveAll<INofifyEventDataService>().ToList();
serviceList.ForEach(service => service.StatusUpdate(obj,status,message,title));
然后创建一个委托实例,如下所示:

var statusUpdate =
    container.ResolveAll<INotifyEventDataService>()
        .Aggregate(default(StatusUpdateDelegate), (d, x) => d + x.StatusUpdate);

无论如何,我不建议使用这种方法。它最终做的事情与循环完全相同,但它使代码更加复杂,因此没有真正的好处。

我不理解你的问题。您知道如何解析INofifyEventDataService的所有实例,并且知道如何在INofifyEventDataService实例上调用StatusUpdate,那么您还需要什么?只需在serviceList上循环并调用每个项上的方法。顺便说一句,ResolveAll只返回使用名称注册的实例,因此在这种情况下不会返回DBLogger。我需要调用的每个方法都有相同的数据,那么您不认为如果我枚举循环并调用每个方法会重复吗。我不知道太多的统一,但如果我能通过共同的统一解决对象的所有方法。展示如何做你正在谈论的事情。除了“从单个resolve对象调用每个类StatusUpdate”之外,因为这是不可能的(resolve()返回单个对象,ResolveAll()返回一个IEnumerable对象)。所以我猜问题在于,您希望C#像jQuery一样工作,但事实并非如此,因为它不是。谢谢Thomas。这很好的解释也许我需要改变我的逻辑。
servicelistObjectA.statusupdate(obj,status,message,title)
List<INofifyEventDataService> serviceList = myContainer.ResolveAll<INofifyEventDataService>().ToList();
serviceList.ForEach(service => service.StatusUpdate(obj,status,message,title));
delegate void StatusUpdateDelegate(object objectType, JobStatus status = JobStatus.None, string messageTitle = null, string MessageDetails = null);
var statusUpdate =
    container.ResolveAll<INotifyEventDataService>()
        .Aggregate(default(StatusUpdateDelegate), (d, x) => d + x.StatusUpdate);
statusUpdate(obj,status,message,title);