2
votes

I am looking to use an IoC container such as StructureMap in a new solution. The solution projects are as follows:

  • Desktop.UI
  • Desktop.Models
  • Desktop.Views
  • Desktop.Presenters
  • BusinessLogic
  • BusinessObjects
  • DataAccess

Where Desktop.Models, BusinessLogic and DataAccess all have a reference to BusinessObjects.

Desktop.UI project would be the entry point where DI should be configured but I don't really want to add a reference to DataAccess just for DI.

I have seen a few other questions along these lines on SO and one answer was to create a separate project for IoC that references the relevant projects and then reference IoC project from Desktop.UI. However Desktop.UI needs DI configuring to instantiate presenters for views so not sure a separate project would work.

I have seen some basic examples of defining Registry classes and using the StructureMap scanning feature to auto detect the Registry classes. My first thought was to put these Registry classes in the projects for which they are configuring however that results in all projects having to reference StructureMap.

What's the recommended approach to setting up DI in a layered application?

2
If I were you I'd create a Framework project to store a singleton IoC and have your boot strapping code in there. - james lewis
@jameslewis How would that work with the UI project as the UI project needs DI for presenters so if the IoC was in a separate project that UI references then IoC project couldn't also reference UI? - Matt F

2 Answers

1
votes

In my experience I've found that I've used one form of dependency injection throughout my server side code, and one in my client side code. For example, the [sigh] WinForms project I was working on recently used Unity on the server side (business logic, data access) and smart client software factory on the client side (MVP framework for winforms that includes its own form of DI). So in your example, I'd say if you were having difficulty trying to get a DI framework to work accross all layers of your application, it's because you shouldn't be. You've probably got a case where you'd want to have two forms of DI, one for the client and one for the server.

In addition to this, I think I'm right in saying that most DI frameworks allow you to bootstrap your dependencies in a config file. The config file tells the framework everything about your dependencies including what assemblies they're in. This means you don't need to reference any of your actual projects in the project that you instantiate your IoC container. So in my example of a Framework project, you'd have a singleton that instantiated your IoC container and read all the dependencies in from a config file.

Here's some links for you:

Hope some of that helps!

Regards,

James

1
votes

For WindowsForms I have a separate project for my IoC stuff. I call it CompositionRoot.

Entry point of CompositionRoot (startup project):

public static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        BootsTrapper.Boot();

        using (var mainForm = ObjectFactory.GetInstance<IPresenter<IMainView>>())
        {
            Application.Run((Form)mainForm.CurrentView);
        }
    }
}

The BootsTrapper is responsible for registring and is a part of the CompositionRoot.

The MainViewPresenter shows the first form. MainViewPresenter can show other forms by using the facade service IMainViewPresenterFacade that contains abstract presenter factories.

public class MainViewPresenter : Presenter<IMainView>
{
    readonly IArticleRepository _articlesRepository;
    readonly IMainViewPresenterFacade _presenterFactory;
    readonly IUnitOfWork _unitOfWork;

    public MainViewPresenter(IMainView currentView, IArticleRepository articlesRepository, IUnitOfWork unitOfWork,
                             IMainViewPresenterFacade presenterFactory)
        : base(currentView, unitOfWork)
    {
        _articlesRepository = articlesRepository;
        _unitOfWork = unitOfWork;
        _presenterFactory = presenterFactory;

        Ensure.That(articlesRepository).IsNotNull();
        Ensure.That(presenterFactory).IsNotNull();

        CurrentView.DetailsClick += View_DetailsClick;
        CurrentView.CloseClick += ViewCloseClick;
        CurrentView.CreateClick += View_CreateClick;
        CurrentView.DeleteClick += View_DeleteClick;

        CurrentView.BindModel(_articlesRepository.GetAll().Select(x => new ArticleViewModel { Id = x.ArticleId, Name = x.Description }));
    }

    public override void Dispose()
    {
        base.Dispose();

        CurrentView.DetailsClick -= View_DetailsClick;
        CurrentView.CloseClick -= ViewCloseClick;
        CurrentView.CreateClick -= View_CreateClick;
        CurrentView.DeleteClick -= View_DeleteClick;
    }

    void View_DeleteClick(object sender, EventArgs e)
    {
        var selectedArticle = CurrentView.GetSelectedArticle();
        var article = _articlesRepository.GetById(selectedArticle.Id);
        _articlesRepository.Delete(article);
        _unitOfWork.Commit();
    }

    void View_CreateClick(object sender, EventArgs e)
    {
        using (var createPresenter = _presenterFactory.CreateCreatePresenter())
        {
            ShowDialog(createPresenter.CurrentView, CurrentView);
        }
    }

    void ViewCloseClick(object sender, EventArgs e)
    {
        CurrentView.Close();
    }

    void View_DetailsClick(object sender, EventArgs eventArgs)
    {
        var article = CurrentView.GetSelectedArticle();

        if (article == null) return;

        using (var detailPresenter = _presenterFactory.CreateDetailPresenter(article))
        {
            ShowDialog(detailPresenter.CurrentView, CurrentView);
        }
    }
}

Take a look at my SimpleMVP project or at this question Dependency Injection and project structure for Console applications and feel free to ask further questions :)