Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8314613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:52:49+00:00 2026-06-08T20:52:49+00:00

I’m trying to get the IoC concept down with a winforms app. Say I

  • 0

I’m trying to get the IoC concept down with a winforms app. Say I have a presenter whose constructor takes its view and a service as constructor arguments. So in the form code I have something that amounts to this:

mnPresenter = new Presenter(this, new AppService());

where, say AppService is an implementation of IAppService. It’s registered in my [autofac] IoC container. What’s the recommended way of getting the “new” out of this presenter construction? Isn’t the whole point of using an IoC framework to lose these “new” calls like I’m making above?

I could do something like

mPresenter = new Presenter(this, MyContainer.Resolve<IAppService>())

but that seems to defeat the purpose of IoC. I’m probably missing something fundamental here.

Apologies in advance if I’m missing something obvious.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:52:50+00:00Added an answer on June 8, 2026 at 8:52 pm

    Your problem is that there is a dependency cycle between the View and the Presenter, since they depend on each other. The general rule in breaking this dependency cycle is to fallback to property injection, which will work in your case as well.

    With MVP, best is to let the View assign itself to a created Presenter by injecting it into a property of the Presenter:

    mPresenter = container.Resolve<Presenter>();
    mPresenter.View = this;
    

    If you can, hide the container from the application. Since you are using MVP, the only things you’ll ever need to directly resolve are presenters. So instead of letting the Forms communicate with the Container, let them communicate with a static PresenterFactory. This factory will use the container under the covers:

    mPresenter = PresenterFactory.Create<MyPresenter>();
    mPresenter.View = this;
    

    Your PresenterFactory might look like this:

    public static class PresenterFactory
    {
        private static IContainer container;
    
        public static TPresenter Create<TPresenter>()
            where TPresenter : IPresenter
        {
            return (TPresenter)
                container.Resolve(typeof(TPresenter));
        }
    
        public static void SetContainer(IContainer container)
        {
            PresenterFactory.container = container;
        }
    }
    

    And your Composition Root might look like this:

    static void Main()
    {
        Bootstrap();
    }
    
    private static void Bootstrap()
    {
        var builder = new ContainerBuilder();
        // TODO: register presenters
    
        var container = builder.Build();
    
        PresenterFactory.SetContainer(container);
    }
    

    UPDATE

    Perhaps even better would it be to do something like this:

    interface IPresenter<TView>
    {
        TView View { get; set; }
    }
    
    public static class PresenterFactory
    {
        private static IContainer container;
    
        public static IPresenter<TView> CreateForView<TView>(TView view)
        {
            var presenter = container.Resolve<IPresenter<TView>>();
            presenter.View = view;
            return presenter;
        }
    }
    
    // View
    mPresenter = PresenterFactory.CreateForView(this);
    

    This hides the actual implementation of the presenter from the view and centralizes the registration of the view to the presenter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.