0

My rich domain model has some circular reference, and this is intentional.

I am also writing my own ORM for more control, and to detect changes made to properties I am using Unity to intercept any call to setters and trigger the property change notification (similar with how EF works).

The problem is that I'm getting Stack Overflow, because the policy interception is going over the same object, over and over again. Is there a way to make it do reference counting?

I have already made sure that the constructor aren't circularly dependent, but I still need Policy Injection to stop recursing over the same objects repeatedly.

Alwyn
  • 8,079
  • 12
  • 59
  • 107
  • It's best to not inject services into entities at all: http://lostechies.com/jimmybogard/2010/04/14/injecting-services-into-entities/, http://blog.jonathanoliver.com/2009/10/ddd-entity-injection/. – Steven May 26 '13 at 06:42
  • I'm not injecting services. I'm injecting other domain entities. – Alwyn May 26 '13 at 15:54
  • And why exactly are you using the DI container for that? – Steven May 26 '13 at 16:24

1 Answers1

0

Instead of injecting the objects, you can inject the functions to built them, when you have a circular reference:

   Container.RegisterType<IMyService, ImplService>(... );


   public class MyClass {

      private Func<IMyService> _serviceProvider;

      public MyClass(Func<IMyService> serviceProvider) { _serviceProvider = serviceProvider        }

      public void DoStuff() {
         _serviceProvider().DoSomething();
      }

   }

Unity will inject a function that returns IMyService

onof
  • 17,167
  • 7
  • 49
  • 85
  • I suppose I could inject a factory or even Unity itself into it, but it will create a lot of unnecessary plumbing code... But still I guess this is as good as I'm gonna get with Unity. – Alwyn May 30 '13 at 21:45