I am using MVC3, Entity Framework v4.3 Code First and SimpleInjector. I have a few simple classes that look like this:
public class SomeThing { public int Id { get; set; } public string Name { get; set; } }
I have another object that looks like this:
public class MainClass { public int Id { get; set; } public string Name { get; set; } public virtual AThing AThingy { get; set; } public virtual BThing BThingy { get; set; } public virtual CThing CThingy { get; set; } public virtual DThing DThingy { get; set; } public virtual EThing EThingy { get; set; } }
Each Thingy (currently) has its own Manager class, for example:
public class SomeThingManager { private readonly IMyRepository<SomeThing> MyRepository; public SomeThingManager(IMyRepository<SomeThing> myRepository) { MyRepository = myRepository; } }
Therefore, My MainController follows:
public class MainController { private readonly IMainManager MainManager; private readonly IAThingManager AThingManager; private readonly IBThingManager BThingManager; private readonly ICThingManager CThingManager; private readonly IDThingManager DThingManager; private readonly IEThingManager EThingManager; public MainController(IMainManager mainManager, IAThingManager aThingManager, IBThingManager bThingManager, ICThingManager cThingManager, IDThingManager dThingManager, IEThingManager eThingManager) { MainManager = mainManager; AThingManager = aThingManager; BThingManager = bThingManager; CThingManager = cThingManager; DThingManager = dThingManager; EThingManager = eThingManager; } ...various ActionMethods... }
In fact, this controller has twice as many nested dependencies. It smells. The smell is worse when you also know that there is OtherController with all or most of the same dependencies. I want to reorganize it.
I already know enough about DI to know that property nesting and a service locator are not good ideas.
I cannot share my MainController because this is the only screen that requires all these things to be displayed and edited with the click of a Save button. In other words, one post-action method saves everything (although I am open to change if it makes sense, if only it remains with one Save button). This screen is built using Knockoutjs and saves Ajax messages, if that matters.
I enjoyed using the Ambient context, but I'm not sure if this is the right way. I enjoyed using the Facade injection. I am also wondering if I should implement the Command architecture at this point. (Not all of the above just move the smell somewhere else?)
Finally, and perhaps regardless of the three above approaches, should I instead have one, say, LookupManager with explicit methods such as GetAThings (), GetAThing (id), GetBThings (), GetBThing (id), etc. .? (But then this LookupManager will need several repositories attached to it or a new type of repository.)
My thoughts aside, my question is to repeat: what is a good way to reorganize this code to reduce the crazy number of nested dependencies?