Search the web
Sign In
New User? Sign Up
domaindrivendesign · Domain-Driven Design
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Want to share photos of your group with the world? Add a group photo to Flickr.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
IOC circular dependency question   Message List  
Reply | Forward Message #13831 of 16019 |
Re: [domaindrivendesign] IOC circular dependency question

Are you building an API layer that is meant to be completely
self-contained and reused in any context? If so, there are some
additional questions that need to be answered:

- Are you looking to Ninject to stitch together your own internal
dependencies or is it as a way for consumers of your API to swap out
pieces for their own implementations as well?
- If I'm using your API, can I use my own container or do I have to use Ninject?
- If I can swap out pieces of your API can I use my own container to
supply those pieces?

If not, you'd probably be better served by moving away from using
Ninject as an "object factory" and more along the lines of a
"dependency registry" and simply declaring dependencies as constructor
arguments or properties. You could have a default Ninject module that
registers all of your dependency concrete implementations
(ICustomerRegistry bound to CustomerRegistry, etc...) and then a
consumer of your API could use your Ninject module say in a web
application or web service host to resolve dependencies at runtime.
Let's say you were using ASP.NET MVC - in the module you have a
controller implementation registered which has constructor-based
dependencies on IFooRepository and IBarRepository. Those are bound to
FooRepository and BarRepository respectively in the module. When an
instance of the controller is requested Ninject will supply the
dependencies automatically.

Hope that helps,
Matt

On Tue, Jul 14, 2009 at 1:00 PM, Justin Daubenmire<jdaubenm@...> wrote:
> Below is a simplistic example making use of 4 assemblies "IOC", "Domain",
> "Repositories" and "Services".
>
>
>
> The "IOC" assembly contains two classes: 1) The DI container for Ninject,
> and 2) a class that serves as a Ninject Module where the binding rules are
> defined.
>
> The following article was the inspiration for this:
>
>
http://www.codethinked.com/post/2008/08/21/Creating-a-binding-factory-for-Ninjec\
t.aspx

>
>
>
> The "Repositories" assembly will create Domain objects using Ninject
>
> * The "Repositories" assembly needs a reference to "IOC" to invoke the DI
> container for the creation of Domain objects.
>
> * The Domain object bindings need defined in the Ninject Module in the "IOC"
> assembly. As a result, the "IOC" assembly needs a reference to "Domain"
>
>
>
> The "Services" assembly will create Repository objects using Ninject
>
> * The "Services" assembly needs a reference to "IOC" to invoke the DI
> container for the creation of Repository objects.
>
> * The Repository object bindings need defined in the Ninject Module in the
> "IOC" assembly. As a result, the "IOC" assembly need a reference to
> "Repositories"
>
>
>
> At this point the circular dependency looks like this:
>
> "Repositories" -> "IOC" -> "Repositories"
>
>
>
> Hopefully that helps clear up the madness.
>
>
>
> Any suggestions on how I could have done this better?
>
>
>
> Thanks for any feedback!
>
>
>
> Regards,
> Justin
> ----- Original Message -----
> From: "Michael Hart" <michael.hart.au@...>
> To: <domaindrivendesign@yahoogroups.com>
> Sent: Tuesday, July 14, 2009 9:48 AM
> Subject: Re: [domaindrivendesign] IOC circular dependency question
>
>
>> Hi Justin,
>>
>> So it's this step I don't understand:
>>
>>> At any time I need a concrete instance of something in the API, I
>>> add a
>>> reference of MyAppxxx.dll assembly to the IOC assembly.
>>
>> What is this "IOC" assembly? I don't quite get what its purpose is.
>> Can you give an example of a class from that assembly and how you use
>> it?
>>
>> If you're creating a set of libraries, with no entry point, then how
>> are you using your IoC container? You typically only need a container
>> when you're instantiating your app and you want to define which
>> specific implementations get injected.
>>
>> Cheers,
>>
>> Michael
>>
>>
>> ------------------------------------
>>
>> Yahoo! Groups Links
>>
>>
>>
>
>
> ------------------------------------
>
> Yahoo! Groups Links
>
>
>
>



Tue Jul 14, 2009 8:25 pm

matt.burton
Offline Offline
Send Email Send Email

Forward
Message #13831 of 16019 |
Expand Messages Author Sort by Date

Hey all, Quick question. When using an IOC in c#.net assemblies - such as Ninject - how do you prevent circular dependencies? I have this: MyApp.IOC ...
Justin Daubenmire
JDaubenm
Offline Send Email
Jul 13, 2009
10:52 pm

... Have a look at Common Service Locator - http://www.codeplex.com/CommonServiceLocator -- Richard Dingwall http://richarddingwall.name...
Richard Dingwall
rdingwall@...
Send Email
Jul 13, 2009
10:55 pm

Er, I don't think CommonServiceLocator is what you want. CommonServiceLocator is intended for libraries who want to support multiple IoC containers. That's...
Michael Hart
michaelhartau
Offline Send Email
Jul 14, 2009
2:33 am

... Put the interfaces for your repositories/services in your domain assembly: they *are* part of the domain. The repository/service implementations then can...
Dan Haywood
danh024680
Offline Send Email
Jul 13, 2009
11:02 pm

... From: "Richard Dingwall" <rdingwall@...> Have a look at Common Service Locator - http://www.codeplex.com/CommonServiceLocator Thanks! Do you know if...
Justin Daubenmire
JDaubenm
Offline Send Email
Jul 13, 2009
11:25 pm

... Here: http://code.google.com/p/ninject/source/browse/#svn/experiments/ninject2/src/CommonServiceLocator.NinjectAdapter -- Richard Dingwall ...
Richard Dingwall
rdingwall@...
Send Email
Jul 13, 2009
11:42 pm

... From: "Richard Dingwall" <rdingwall@...> ... Thanks Richard! Which .NET IOC do you actually use? Regards, Justin...
Justin Daubenmire
JDaubenm
Offline Send Email
Jul 14, 2009
12:21 am

... We are using Unity at the moment. Maybe not the best but it's doing the job so far :) -- Richard Dingwall http://richarddingwall.name...
Richard Dingwall
rdingwall@...
Send Email
Jul 14, 2009
1:58 am

... From: "Richard Dingwall" <rdingwall@...> We are using Unity at the moment. Maybe not the best but it's doing the job so far :) Cool, what other ones...
Justin Daubenmire
JDaubenm
Offline Send Email
Jul 14, 2009
2:23 am

Hi Michael, The MyApp.Domain name and MyApp.IOC are just made up silly names. I'd never name a real project something like that. :) If understanding you...
Justin Daubenmire
JDaubenm
Offline Send Email
Jul 14, 2009
12:41 pm

Hey Justin, You might need to give us more detail as to what you were trying to do with the projects - I realise that the "MyApp" is just an artificial name of...
Michael Hart
michaelhartau
Offline Send Email
Jul 14, 2009
1:15 pm

Sure, no problem. I am developing an API so I do not have a UI layer. There are a total of 20 assemblies in the API project. At any time I need a concrete...
Justin Daubenmire
JDaubenm
Offline Send Email
Jul 14, 2009
1:37 pm

Hi Justin, ... What is this "IOC" assembly? I don't quite get what its purpose is. Can you give an example of a class from that assembly and how you use it? If...
Michael Hart
michaelhartau
Offline Send Email
Jul 14, 2009
1:49 pm

Below is a simplistic example making use of 4 assemblies "IOC", "Domain", "Repositories" and "Services". The "IOC" assembly contains two classes: 1) The DI...
Justin Daubenmire
JDaubenm
Offline Send Email
Jul 14, 2009
7:59 pm

Are you building an API layer that is meant to be completely self-contained and reused in any context? If so, there are some additional questions that need to...
Matt Burton
matt.burton
Offline Send Email
Jul 14, 2009
8:27 pm

If you're willing to give up have dependencies injected into your domain objects, things can be simplified immensely. No need to use DI or IoC to create your...
Udi Dahan
udidahan7
Offline Send Email
Jul 14, 2009
9:29 pm

Hi Justin, Matt and Udi have given good suggestions on this already, but I'll just add a few things: 1. The page you refer to uses a static singleton to access...
Michael Hart
michaelhartau
Offline Send Email
Jul 15, 2009
12:42 am

... From: "Matt Burton" <matt.burton@...> - Are you looking to Ninject to stitch together your own internal dependencies or... ... Yes, that is why we...
Justin Daubenmire
JDaubenm
Offline Send Email
Jul 14, 2009
11:54 pm

Alright - I would refer you to Udi's response, then - you just need to reel it in a bit. Keep it clean and simple - have your dependencies as constructor...
Matt Burton
matt.burton
Offline Send Email
Jul 15, 2009
6:29 am

Thanks guys for all the feedback and advice... much appreciated! Regards, Justin ... From: "Matt Burton" <matt.burton@...> To:...
Justin Daubenmire
JDaubenm
Offline Send Email
Jul 15, 2009
8:52 am

Hey guys, One more quick question on constructor injection. I have this constructor in my application layer: public fu(IView view) { this._view = view; // how...
Justin Daubenmire
JDaubenm
Offline Send Email
Jul 15, 2009
9:41 am

Hey guys, I came across this quick blog post about this scenario: http://kohari.org/2008/06/18/playing-nice-with-service-locators/ It looks like I have two...
Justin Daubenmire
JDaubenm
Offline Send Email
Jul 15, 2009
9:50 am

Hi Justin, Definitely go for #1 until you find a situation where it's not manageable. Also, maybe any more IoC questions are better to ask on the Ninject list?...
Michael Hart
michaelhartau
Offline Send Email
Jul 15, 2009
10:01 am

So if following you, you are suggesting to use a service locater and an IOC tool. Is that correct? btw I agree about taking Ninject questions / IOC to the...
Justin Daubenmire
JDaubenm
Offline Send Email
Jul 15, 2009
10:10 am

Hi Mike, I understand what you meant by going with option 1. Sorry, programming early mornings does have its limitations! =) Regards, Justin ... From: "Michael...
Justin Daubenmire
JDaubenm
Offline Send Email
Jul 15, 2009
10:38 am
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help