简体   繁体   中英

Ninject Circular Dependencies

there are 2 classes in wpf app

First class

public class Context1 : BaseContext
    {
        private readonly Page1 _page1;

        public Context1(Page1 page1)
        {
            _page1 = page1;
        }
}

Second class

public class Context2 : BaseContext
    {
        private readonly Page2 _page2;

        public Context2(Page2 page2)
        {
            _page2 = page2;
        }
}

Configuration

container = new StandardKernel();
container.Bind<Page1>().ToMethod(context => new Page1() { DataContext = container.Get<Context1>() });
container.Bind<Page2>().ToMethod(context => new Page2() { DataContext = container.Get<Context2>() });

on start stackoverflow exception appears, how to resolve?

The Context requires a Page and the Page requires a Context , so it is a circular dependency. I would recommend refactoring some of the behaviors of each into standalone services so that at least one of them can get its dependencies from the services and break the circular reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM