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>();
});
}
}
}