使用统一依赖框架解决所有方法
Resolve all method using unity dependency framework
我是依赖注入和统一框架的新手。我的问题是-我有接口
public interface INofifyEventDataService
{
void StatusUpdate (object objectType, JobStatus status = JobStatus.None,string messageTitle=null ,string MessageDetails=null);
}
和class是
Public class A : INofifyEventDataService
{
void StatusUpdate (object objectType, JobStatus status = JobStatus.None,string messageTitle=null ,string MessageDetails=null)
{ //implementation form A}}
和 class B
Public class B: INofifyEventDataService
{
void StatusUpdate (object objectType, JobStatus status = JobStatus.None,string messageTitle=null ,string MessageDetails=null)
{ //implementation form B}}
对于 DBLogger(假设 class c)
Public class DBLogger : INofifyEventDataService
{
void StatusUpdate (object objectType, JobStatus status = JobStatus.None,string messageTitle=null ,string MessageDetails=null)
{ //implementation form DBLogger }
据我所知,unity 会像
一样解决它
IUnityContainer myContainer = new UnityContainer();
myContainer.RegisterType<INofifyEventDataService, DBLogger >();
myContainer.RegisterType<INofifyEventDataService, classA>("A");
myContainer.RegisterType<INofifyEventDataService, classB>("B");
并且会一一解决或者全部解决
IEnumerable<INofifyEventDataService> serviceList = myContainer.ResolveAll<INofifyEventDataService>();
我需要枚举所有对象,然后为每个对象调用 like
servicelistObjectA.statusupdate(obj,status,message,title)
我的要求是从单个解析对象调用每个 class StatusUpdate。?
如果我对问题的理解正确,您想在 INotifyEventDataService
的所有实例上调用 StatusUpdate
,但只需一次调用,而不循环遍历 ResolveAll
返回的列表.这在 C# 中是不可能的;您可以使用执行此操作的方法,但在内部此方法仍然使用循环。
例如,您可以使用 List<T>.ForEach
:
List<INofifyEventDataService> serviceList = myContainer.ResolveAll<INofifyEventDataService>().ToList();
serviceList.ForEach(service => service.StatusUpdate(obj,status,message,title));
(请记住,它与在每个项目上调用方法的 foreach
循环执行相同的操作)。
另一种方法,看起来就像您一次调用所有方法,是将所有实例的 StatusUpdate
方法组合到一个多播委托中。首先,像这样声明一个委托(您可以使用 Action<....>
,但命名委托使代码更具可读性):
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);
无论如何,我不推荐使用这种方法。它最终做的事情和循环完全一样,但是它使代码更复杂,所以并没有真正的好处。
我是依赖注入和统一框架的新手。我的问题是-我有接口
public interface INofifyEventDataService
{
void StatusUpdate (object objectType, JobStatus status = JobStatus.None,string messageTitle=null ,string MessageDetails=null);
}
和class是
Public class A : INofifyEventDataService
{
void StatusUpdate (object objectType, JobStatus status = JobStatus.None,string messageTitle=null ,string MessageDetails=null)
{ //implementation form A}}
和 class B
Public class B: INofifyEventDataService
{
void StatusUpdate (object objectType, JobStatus status = JobStatus.None,string messageTitle=null ,string MessageDetails=null)
{ //implementation form B}}
对于 DBLogger(假设 class c)
Public class DBLogger : INofifyEventDataService
{
void StatusUpdate (object objectType, JobStatus status = JobStatus.None,string messageTitle=null ,string MessageDetails=null)
{ //implementation form DBLogger }
据我所知,unity 会像
一样解决它IUnityContainer myContainer = new UnityContainer();
myContainer.RegisterType<INofifyEventDataService, DBLogger >();
myContainer.RegisterType<INofifyEventDataService, classA>("A");
myContainer.RegisterType<INofifyEventDataService, classB>("B");
并且会一一解决或者全部解决
IEnumerable<INofifyEventDataService> serviceList = myContainer.ResolveAll<INofifyEventDataService>();
我需要枚举所有对象,然后为每个对象调用 like
servicelistObjectA.statusupdate(obj,status,message,title)
我的要求是从单个解析对象调用每个 class StatusUpdate。?
如果我对问题的理解正确,您想在 INotifyEventDataService
的所有实例上调用 StatusUpdate
,但只需一次调用,而不循环遍历 ResolveAll
返回的列表.这在 C# 中是不可能的;您可以使用执行此操作的方法,但在内部此方法仍然使用循环。
例如,您可以使用 List<T>.ForEach
:
List<INofifyEventDataService> serviceList = myContainer.ResolveAll<INofifyEventDataService>().ToList();
serviceList.ForEach(service => service.StatusUpdate(obj,status,message,title));
(请记住,它与在每个项目上调用方法的 foreach
循环执行相同的操作)。
另一种方法,看起来就像您一次调用所有方法,是将所有实例的 StatusUpdate
方法组合到一个多播委托中。首先,像这样声明一个委托(您可以使用 Action<....>
,但命名委托使代码更具可读性):
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);
无论如何,我不推荐使用这种方法。它最终做的事情和循环完全一样,但是它使代码更复杂,所以并没有真正的好处。