2

I am new learner in dependency injection and unity framework. My question scenario is that- i have interface

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

and classes are

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

and for class B

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

and for DBLogger (assume class c)

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

as far i know unity resolve it like

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

And it will resolve one by one or all like

IEnumerable<INofifyEventDataService> serviceList = myContainer.ResolveAll<INofifyEventDataService>();

and i need to enumerate all object and then call like for every object

servicelistObjectA.statusupdate(obj,status,message,title)

My requirement is to call every class StatusUpdate from single resolve object.?

5
  • 1
    I don't understand your question. You know how to resolve all instances of INofifyEventDataService, and you know how to call StatusUpdate on an instance of INofifyEventDataService, so what else do you need? Just loop over serviceList and call the method on each item. Commented Aug 8, 2015 at 0:32
  • 1
    btw, ResolveAll only returns instances that were registered with a name, so in this case DBLogger won't be returned. Commented Aug 8, 2015 at 0:32
  • each method which i need to call have same data then don't you think if i enumerate loop and call each method will repetitive. I dont know too much in unity but if i can all methods by common unity resolve object. Commented Aug 8, 2015 at 0:52
  • Resolving All Objects of a Particular Type shows how to do what you are talking about. Except not "call every class StatusUpdate from single resolve object" since that is not possible (Resolve() returns a single object and ResolveAll() returns an IEnumerable of objects). Commented Aug 8, 2015 at 1:19
  • So I guess the problem is that you expect C# to act like jQuery, and it doesn't because it isn't? Commented Aug 8, 2015 at 3:25

1 Answer 1

1

If I understand the question correctly, you want to call the StatusUpdate on all instances of INotifyEventDataService, but with a single call, without looping over the list returned by ResolveAll. This isn't really possible in C#; you can use a method that does it, but internally this method still uses loops.

For instance, you could use List<T>.ForEach:

List<INofifyEventDataService> serviceList = myContainer.ResolveAll<INofifyEventDataService>().ToList();
serviceList.ForEach(service => service.StatusUpdate(obj,status,message,title));

(keep in mind that it does the same thing as a foreach loop where you call the method on each item).

Another approach, that will look as if you were calling all methods at once, is by combining the StatusUpdate method of all instances into a multicast delegate. First, declare a delegate like this (you could use Action<....>, but a named delegate makes the code more readable):

delegate void StatusUpdateDelegate(object objectType, JobStatus status = JobStatus.None, string messageTitle = null, string MessageDetails = null);

Then create a delegate instance like this:

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

You can then invoke the delegate, which will call all the methods:

statusUpdate(obj,status,message,title);

In any case, I wouldn't recommend using this approach. It ends up doing exactly the same thing as a loop, but it makes the code more complex, so there is no real benefit.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Thomas. Its good explanation perhaps i need to change my logic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.