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

  • SEARCH
  • Home
  • 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 504871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:32:26+00:00 2026-05-13T06:32:26+00:00

I have a Presenter that takes a Service and a View Contract as parameters

  • 0

I have a Presenter that takes a Service and a View Contract as parameters in its constructor:

public FooPresenter : IFooPresenter {
    private IFooView view;
    private readonly IFooService service;

    public FooPresenter(IFooView view, IFooService service) {
        this.view = view;
        this.service = service;
    }
}

I resolve my service with Autofac:

private ContainerProvider BuildDependencies() {
    var builder = new ContainerBuilder();
    builder.Register<FooService>().As<IFooService>().FactoryScoped();  

    return new ContainerProvider(builder.Build());  
}

In my ASPX page (View implementation):

public partial class Foo : Page, IFooView {
    private FooPresenter presenter;

    public Foo() {
        // this is straightforward but not really ideal
        // (IoCResolve is a holder for how I hit the container in global.asax)
        this.presenter = new FooPresenter(this, IoCResolve<IFooService>());

        // I would rather have an interface IFooPresenter so I can do
        this.presenter = IoCResolve<IFooPresenter>();
        // this allows me to add more services as needed without having to 
        // come back and manually update this constructor call here
    }
}

The issue is FooPresenter’s constructor expects the specific Page, not for the container to create a new one.

Can I supply a specific instance of the view, the current page, to the container for just this resolution? Does that make sense to do, or should I do this another way?

  • 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-05-13T06:32:27+00:00Added an answer on May 13, 2026 at 6:32 am

    The way to solve passing what I like to call data parameters when resolving dependencies in Autofac is by using generated factories.

    (Update: this question discusses the same problem and my article shows how you can avoid large amounts of factory delegates).

    The solution to your problem will look something like this:

    First, declare a factory delegate thath only accepts the data parameters:

    public delegate IFooPresenter FooPresenterFactory(IFooView view);
    

    Your presenter goes unchanged:

    public FooPresenter : IFooPresenter {
        private IFooView view;
        private readonly IFooService service;
    
        public FooPresenter(IFooView view, IFooService service) {
            this.view = view;
            this.service = service;
        }
    }
    

    Next the Autofac container setup:

    var builder = new ContainerBuilder();
    builder.Register<FooService>().As<IFooService>().FactoryScoped();  
    builder.Register<FooPresenter>().As<IFooPresenter>().FactoryScoped();  
    builder.RegisterGeneratedFactory<FooPresenterFactory>();
    

    Now in your page you can in two lines of code resolve the presenter by first getting the factory and then calling the factory to do the resolution for you:

    public partial class Foo : Page, IFooView {
        private FooPresenter presenter;
    
        public Foo() {
            var factory = IoCResolve<FooPresenterFactory>();
            this.presenter = factory(this);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my rails project I have a presenter method that has a helper view
Summary I have written a process monitor command-line application that takes as parameters: The
I have a presenter class that will spawn a new presenter and view upon
Greetings: I have put together a RESTful web service in .NET 3.5 that takes
I have a view controller that gets presented modally and changes some data that
I have an app that presents Table View of different files. I have it
We currently have a production application that runs as a windows service. Many times
I have an excel file with 815 records that includes service name and charge.
The CellTree has a constructor that takes a TreeModel. I can't set the tree
I currently have an application which takes a screenshot of a presenter's desktop and

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.