Bug in Objectfactory concerning Servicedependency

Jul 6, 2010 at 11:24 AM

Hello,

as i'm allready wrotten in the issue tracker, there is a bug in the Objectfactory. Coud you please change: if (!attrib.EnsureExists) to if (attrib.EnsureExists)?

 

Kind regards svbirdy

 

 

Coordinator
Jul 9, 2010 at 2:52 PM

I need help on this.  Rather than just make the change, I need to be able to reproduce the problem with a test, then fix it and ensure the test verifies the fix.  I created this test:

    public class OnDemandServiceConsumer : IMockType

    {

        private static int m_instanceCount = 0;

        public OnDemandServiceConsumer()

        {

            Name = "Type OnDemandServiceConsumer - instance " + m_instanceCount;

        }

        [ServiceDependency(EnsureExists=false)]

        public MockTypeB BService { set; get; }

        public string Name { get; set; }

    }

    [TestMethod()]

    [Description("Tests that if we have a service dependency with EnsureExists == false and that serve does not exist, that we get an exception")]

    [ExpectedException(typeof(ServiceMissingException))]

    public void ServiceDependencyAutoCreateFalseMissingServiceTest()

    {

        using (var root = new WorkItem())

        {

            root.Items.AddNew<OnDemandServiceConsumer>("idd");

            var b = root.Items["idd"] as OnDemandServiceConsumer;

        }

    }

And it passes as expected.  If you think you’ve found a bug, I need code to reproduce it.

-----------------

Chris Tacke

Principal Partner

OpenNETCF Consulting, LLC

From: svbirdy [mailto:notifications@codeplex.com]
Sent: Tuesday, July 06, 2010 6:25 AM
To: Chris Tacke
Subject: Bug in Objectfactory concerning Servicedependency [ioc:218439]

From: svbirdy

Hello,

as i'm allready wrotten in the issue tracker, there is a bug in the Objectfactory. Coud you please change: if (!attrib.EnsureExists) to if (attrib.EnsureExists)?

Kind regards svbirdy

Read the full discussion online.

To add a post to this discussion, reply to this email (ioc@discussions.codeplex.com)

To start a new discussion for this project, email ioc@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Jul 10, 2010 at 1:37 PM
Edited Jul 10, 2010 at 1:38 PM

Am i getting something wrong?

You write "EnsureExists = false" but expect a ServiceMissingException. Shoudn't it be the other way around? So if "EnsureExits = true" should throw the Exception?

 

Kind regards svbirdy

Coordinator
Jul 10, 2010 at 1:56 PM

If EnsureExists == true, the IoC framework will create the instance of the service for you (it ensures the instance exists).  If you have it set to false, it assumes that you will create it yourself.

From: svbirdy [mailto:notifications@codeplex.com]
Sent: Saturday, July 10, 2010 8:38 AM
To: Chris Tacke
Subject: Re: Bug in Objectfactory concerning Servicedependency [ioc:218439]

From: svbirdy

Am i getting something wrong?

You write "EnsureExists = false" but expect a ServiceMissingException. Shoudn't it be the other way around? So if "EnsureExits = true" shoud throw the Exception?

Kind regards svbirdy

Read the full discussion online.

To add a post to this discussion, reply to this email (ioc@discussions.codeplex.com)

To start a new discussion for this project, email ioc@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Jul 10, 2010 at 4:36 PM

If you call for example: "RootWorkItem.Services.Get<ISomeService>(false)" it returns null if the service does not exist and if it's set to "true" it throws an exception. I thout the behaviour of the attribute is the same...

 

Coordinator
Jul 12, 2010 at 5:20 PM

I’d need a full code example to be sure, but I’m pretty sure that this is expected behavior.

The reason is this:

You are saying “get me the service that is registered as the ISomeService interface”.  If the service does not exist, it cannot return it.  If you say “true” to ensure exists, you are indicating that you want it to create the instance.  *However* in this case, you are sending an interface.  The framework has no idea what concrete type it should create – it can’t create an instance of an interface – so it fails.

I’ll need to run a test to see if I can improve the exception message, but what you really need is the implementation for issue 2783 (which I have not done yet):

http://ioc.codeplex.com/workitem/2783

-----------------

Chris Tacke

Principal Partner

OpenNETCF Consulting, LLC

From: svbirdy [mailto:notifications@codeplex.com]
Sent: Saturday, July 10, 2010 11:37 AM
To: Chris Tacke
Subject: Re: Bug in Objectfactory concerning Servicedependency [ioc:218439]

From: svbirdy

If you call for example: "RootWorkItem.Services.Get<ISomeService>(false)" it returns null if the service does not exist and if it's set to "true" it throws an exception. I thout the behaviour of the attribute is the same...

Read the full discussion online.

To add a post to this discussion, reply to this email (ioc@discussions.codeplex.com)

To start a new discussion for this project, email ioc@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Jul 13, 2010 at 6:37 AM
Edited Jul 13, 2010 at 6:53 AM

I try to explain what i mean another way. (Excuse my not so good english) For example i have two Services. One implements "ISomeServiceWhichMustNotExist" and the other "ISomeServiceWhichMustExist". The second one is strictly required. So if it's not registered an exception will be thrown. So i do something like: ISomeServiceWhichMustExist mustExistSvc = RootWorkItem.Services.Get<ISomeServiceWhichMustExist>(true); mustExistSvc.DoImportantWork(); The second one i use like this: ISomeServiceWhichMustNotExist mustNotExistSvc = RootWorkItem.Services.Get<ISomeServiceWhichMustNotExist>(false); if(mustNotExistSvc != null) mustNotExistSvc.DoSomethingAdditionalWhenServiceExist(); Then i have at two (or more alternative) Modules which implement the interfaces. The "ISomeServiceWhichMustNotExist" Module is optional. So if it isn't deployed, the programm still works. It just doesn't execute "mustNotExistSvc.DoSomethingAdditionalWhenServiceExist()" So when using the attribute [EnsureExists = false] it should be possible that the decorated Property can be null too. Did i get something wrong?

 

In the ServiceCollection you wrote:

 

ComponentDescriptor desc = GetDescriptor(serviceType);

 if (desc == null)
 {
 if (ensureExists)
 {
 throw new ServiceMissingException(string.Format("Cannot get service of type '{0}'", serviceType.Name));
 }
 return null;
 }



And in ObjectFactory:

 
if (!attrib.EnsureExists)
 {
 throw new ServiceMissingException(string.Format("Type '{0}' has a service dependency on type '{1}'",
 t.Name, attrib.RegistrationType.Name));
 }

That's not clear to me