Bidirectional Dependency Problem

1,662 views
Skip to first unread message

AOL

unread,
Feb 15, 2013, 12:12:09 PM2/15/13
to structure...@googlegroups.com

Hi,
What is cause of this error?How i must solve this problem?

StructureMap.StructureMapExceptionStructureMap Exception Code: 295 Bidirectional Dependency Problem detected with RequestedType: MyProject ServiceLayer.Interfaces.IPostService, Name: 747c7d93-5bec-49b5-b137-7d6c9678f7e3, ConcreteType: MyProject.ServiceLayer.EFServices.PostService. The BuildStack is: 1.) RequestedType: MyProject.Web.Areas.Admin.Controllers.PostController, Name: e311f4b1-7575-459c-a789-25126f39fe5c, ConcreteType: MyProject.Web.Areas.Admin.Controllers.PostController 2.) RequestedType: MyProject.ServiceLayer.Interfaces.IPostService, Name: 747c7d93-5bec-49b5-b137-7d6c9678f7e3, ConcreteType: MyProject.ServiceLayer.EFServices.PostService 3.) RequestedType: MyProject.ServiceLayer.Interfaces.IPostTagService, Name: 0b4e4c1b-e90e-44fd-8721-f75f67b62473, ConcreteType: MyProject.ServiceLayer.EFServices.PostTagService

Brandon Behrens

unread,
Feb 15, 2013, 12:26:23 PM2/15/13
to structure...@googlegroups.com
It looks like you've got a bidirectional dependency.  It looks like you are trying to build PostService which has a dependency on PostController.  PostController has a dependency on PostService.  Here's a link to a stack overflow discussion on it.

http://stackoverflow.com/questions/2782299/circular-dependencies-in-structuremap-can-they-be-broken-with-property-injecti


--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-us...@googlegroups.com.
To post to this group, send email to structure...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/structuremap-users/-/_kYjWRs6py8J.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Brandon Behrens
512.659.7171 (c)
Message has been deleted

AOL

unread,
Feb 15, 2013, 1:17:28 PM2/15/13
to structure...@googlegroups.com
Please read again my Question, my question is How solve this problem, But you give me a link which not solved my problem !!!?

Kevin Miller

unread,
Feb 15, 2013, 1:22:19 PM2/15/13
to structure...@googlegroups.com
Settle down there. AOL. Brandon was trying to help you and you yell back with a double post? 

How about I give you something to chew on and then you can go read that link. When I've run into this problem I worked around it with the Func<T> trick. 

Find the type that is causing the problem. And rather than taking a direct dependency on it. Take a dependency on Func<TypeYouHaveACircularDependencyWith>

Hope this helps.

KevM 


On Fri, Feb 15, 2013 at 12:17 PM, AOL <kiyar...@gmail.com> wrote:
Please read again my Question, my question is How solve this problem, But you give me a link which not solved my problem !!!?

--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-us...@googlegroups.com.
To post to this group, send email to structure...@googlegroups.com.

AOL

unread,
Feb 15, 2013, 1:54:10 PM2/15/13
to structure...@googlegroups.com
@KevM, Unity haven't this problem, But StructureMap have such an this problem.
I did not understand your solution :(

Jeremy D Miller

unread,
Feb 15, 2013, 2:02:28 PM2/15/13
to structure...@googlegroups.com
AOL, 

StructureMap does not support bidirectional dependencies, period.  You need to either change your object structure (most likely) to eliminate the bidirectional dependency, or you can use the Func dependency workaround that Kevin suggested.

Sent from my iPhone

On Feb 15, 2013, at 12:54 PM, AOL <kiyar...@gmail.com> wrote:

@KevM, Unity haven't this problem, But StructureMap have such an this problem.
I did not understand your solution :(

--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-us...@googlegroups.com.
To post to this group, send email to structure...@googlegroups.com.

AOL

unread,
Feb 15, 2013, 2:07:21 PM2/15/13
to structure...@googlegroups.com
Jeremy Miller , please give a Complete example of Func dependency.

Kevin Miller

unread,
Feb 15, 2013, 2:26:31 PM2/15/13
to structure...@googlegroups.com
From this: 

public class MyClass 
{
  public MyClass(IBidirectionalDependency bd) {
    //....
  }

To this: 

public class MyClass 
{
  public MyClass(Func<IBidirectionalDependency> bd) {
    //....
  }

Remember it is a Func<T> so before you use the instance you need to invoke the function. 

_bd().myMethod()

KevM 


On Fri, Feb 15, 2013 at 1:07 PM, AOL <kiyar...@gmail.com> wrote:
Jeremy Miller , please give a Complete example of Func dependency.

--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-us...@googlegroups.com.
To post to this group, send email to structure...@googlegroups.com.
Message has been deleted

AOL

unread,
Feb 15, 2013, 3:18:42 PM2/15/13
to structure...@googlegroups.com
    This is true?


    public class PostTagService: IPostTagService
    {
        private readonly IPostService _posts;
        public PostTagService(Func<IPostService> posts)
        {
            _posts = posts.Invoke();
        }
 
 
        public void SomeMethod()
        {
            _posts.AnotherMethod(21, "A");
        }
    }

Kevin Miller

unread,
Feb 15, 2013, 3:40:10 PM2/15/13
to structure...@googlegroups.com
Do you have a problem with your email app? Why the double posts? 

More like this:

    public class PostTagService: IPostTagService
    {
        private readonly Func<IPostService> _postsFunc;
        public PostTagService(Func<IPostService> postsFunc)
        {
            _postsFunc = postsFunc;
        }
  
        public void SomeMethod()
        {
            _postsFunc().AnotherMethod(21, "A");
        }
    }

KevM 


--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-us...@googlegroups.com.
To post to this group, send email to structure...@googlegroups.com.

AOL

unread,
Feb 15, 2013, 4:00:39 PM2/15/13
to structure...@googlegroups.com
    public class PostTagService: IPostTagService
    {
        private readonly IPostService _posts;
        public PostTagService(Func<IPostService> posts)
        {
            _posts = posts.Invoke();
        }
 
 
        public void SomeMethod()
        {
            _posts.AnotherMethod(21, "A");
        }
    }
when i use top approach get to this error: An unhandled "StackOverflowException" in StructureMap.dll, Why?


Kevin Miller

unread,
Feb 15, 2013, 4:09:42 PM2/15/13
to structure...@googlegroups.com
Why not try the approach I sent?

KevM 


--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-us...@googlegroups.com.
To post to this group, send email to structure...@googlegroups.com.

AOL

unread,
Feb 15, 2013, 4:13:34 PM2/15/13
to structure...@googlegroups.com
yes i used that, But out of curiosity i want know couse of this error

thanks in advance.

Kevin Miller

unread,
Feb 15, 2013, 4:20:19 PM2/15/13
to structure...@googlegroups.com
Glad it worked. Thanks for letting us know. 

My guess. Something is calling itself recursively. Hence the stack overflow. Likely it is structure map and its cyclical dependency being evaluated in the ctor. When you move the evaluation of the dependency out of the ctor the cycle gets broken somehow.

KevM 


On Fri, Feb 15, 2013 at 3:13 PM, AOL <kiyar...@gmail.com> wrote:
yes i used that, But out of curiosity i want know couse of this error

thanks in advance.

--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-us...@googlegroups.com.
To post to this group, send email to structure...@googlegroups.com.

AOL

unread,
Feb 15, 2013, 11:17:14 PM2/15/13
to structure...@googlegroups.com
Behind the Curtain, in your solution, a new instance of PostService will create Or point to existing instance of PostService?

AOL

unread,
Feb 16, 2013, 11:51:57 AM2/16/13
to structure...@googlegroups.com
Please answer to my question.

Kevin Miller

unread,
Feb 16, 2013, 12:03:20 PM2/16/13
to structure...@googlegroups.com
Likely new. A unit test would know for sure. 

On Saturday, February 16, 2013, AOL wrote:
Please answer to my question.

--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-us...@googlegroups.com.
To post to this group, send email to structure...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/structuremap-users/-/r7Rd37PtBJ0J.

For more options, visit https://groups.google.com/groups/opt_out.
 
 


--
KevM 
Message has been deleted

AOL

unread,
Feb 16, 2013, 1:51:14 PM2/16/13
to structure...@googlegroups.com
What is your opinion about this approach?

    public class PostTagService: IPostTagService
    {
        public PostTagService()
        {
            // ...
        }
 
 
        public void SomeMethod()
        {
            var postService = ObjectFactory.GetInstance<IPostService>();
            postService.AnotherMethod(21, "A");
        }
    }


Now, your solution (Func<T>) is better Or top approach in Here?

Kevin Miller

unread,
Feb 16, 2013, 2:08:56 PM2/16/13
to structure...@googlegroups.com
I prefer DI over service location where ever possible. 


On Saturday, February 16, 2013, AOL wrote:
--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-us...@googlegroups.com.
To post to this group, send email to structure...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/structuremap-users/-/w6jBtoC5suQJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 


--
KevM 

AOL

unread,
Feb 16, 2013, 2:20:44 PM2/16/13
to structure...@googlegroups.com
Why your solution (Func<T>) is better? (couse of excellence)

Kevin Miller

unread,
Feb 16, 2013, 3:07:34 PM2/16/13
to structure...@googlegroups.com
Service location is a tighter coupling than dependency injection. I've gone down both paths and SI always bites me in the ass eventually. 


On Saturday, February 16, 2013, AOL wrote:
Why your solution (Func<T>) is better? (couse of excellence)

--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-us...@googlegroups.com.
To post to this group, send email to structure...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/structuremap-users/-/EUUGLesbLZAJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 


--
KevM 

AOL

unread,
Feb 16, 2013, 3:25:15 PM2/16/13
to structure...@googlegroups.com
what mean this : "Service location is a tighter coupling than dependency injection" ?

Kevin Miller

unread,
Feb 16, 2013, 4:17:46 PM2/16/13
to structure...@googlegroups.com

KevM 


On Sat, Feb 16, 2013 at 2:25 PM, AOL <kiyar...@gmail.com> wrote:
what mean this : "Service location is a tighter coupling than dependency injection" ?

--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-us...@googlegroups.com.
To post to this group, send email to structure...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages