0

First lets start to describe my project architecture.
I have a asp.net mvc application called Portal.Web as startup project and multiple asp.net mvc applications which called Plugin.XXX (Plugin.News, Plugin.Cms, etc) and Portal.Web has references from all of these plugins.
I installed StrcutureMap.Mvc5 for each one of these Plugins
As you know, when you install StructureMap a folder created called DependencyResolution and it contains some files, one of them is IoC.cs where you can initialize your container.
Now my problem is since all plugins has their own IoC.cs, it seems like they override each other containers so at the end I get this error :

No parameterless constructor defined for this object

// inner exception
No default Instance is registered and cannot be automatically determined for type 'Portal.Plugins.Page.Interfaces.IPage'
There is no configuration specified for Portal.Plugins.Page.Interfaces.IPage
1.) new RouteController(*Default of IUnitOfWork*, *Default of IPage*)
2.) Portal.Web.Controllers.RouteController
3.) Instance of Portal.Web.Controllers.RouteController
4.) Container.GetInstance(Portal.Web.Controllers.RouteController)

There is no configuration specified for Portal.Plugins.Page.Interfaces.IPage

I could use ONE IoC.cs in Portal.Web project but I need to keep modularity and make Plugins independent as much as possible.
is there any way to keep plugins containers independent?

namespace Portal.Plugins.Account.DependencyResolution {
    using StructureMap;

    public static class IoC {
        public static IContainer Initialize() {
            return new Container(c =>
            {
                c.AddRegistry<DefaultRegistry>();
                c.For<IUnitOfWork>().LifecycleIs(new HttpContextLifecycle()).Use<AccountDbContext>();
                c.For<IAccount>().Use<AccountService>();
            });
        }
    }
}


namespace Portal.Plugins.Cms.DependencyResolution {
    using StructureMap;

    public static class IoC {
        public static IContainer Initialize() {
            return new Container(c =>
            {
                c.AddRegistry<DefaultRegistry>();
                c.For<IUnitOfWork>().LifecycleIs(new HttpContextLifecycle()).Use<CmsDbContext>();
                c.For<IPage>().Use<PageService>();
                c.For<IMenu>().Use<MenuService>();
                c.For<IMedia>().Use<MediaService>();
            });
        }
    }
}
mhesabi
  • 1,140
  • 3
  • 22
  • 48

1 Answers1

0

If I understand your problem correctly, couldn't you create a DependencyResolution class library that contains the StructureMap.Mvc5 package that's then used throughout your plugins (this saves each of your plugins containing versions of StructureMap.Mvc5) and having a single reference to your DependencyResolution class library.

From here your individual plugins can then contain their own StructureMap registries, with more global registries being kept in the DependencyResolution class library.

Joseph Woodward
  • 9,191
  • 5
  • 44
  • 63