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 6972121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:55:38+00:00 2026-05-27T16:55:38+00:00

Using Autofac, I have the following scenario: public class MainClass { public delegate MainClass

  • 0

Using Autofac, I have the following scenario:

public class MainClass
{
     public delegate MainClass Factory();
     public MainClass(Report report, SecondaryClass secClass)
     {
         Report=report;
         SecondaryClass = secClass;
     }
     public Report Report {get; private set;}
     public SecondaryClass SecondaryClass {get; private set;}
}

public class SecondaryClass
{   
     public SecondaryClass(Report report)
     {
         Report=report;
     }
     public Report Report {get; private set;}
}

What I would like to achieve is that every time I invoke the Factory() delegate on MainClass, both MainClass and SecondaryClass are injected with the same instance of Report.

Basically

public void MyMethod()
{
     var myMainClass = _MainClassFactory.Invoke();
     //How do I do achieve this????
     Debug.Assert (object.ReferenceEquals(myMainClass.Report , myMainClass.SecondaryClass.Report));
}

How can I configure MainClass and SecondaryClass?
My current configuration is the following, but it does not seem to achieve what I want.
As a matter of fact, every time I call the Factory method, I get the same instance of Report.

        builder.RegisterType<MainClass>().AsSelf();
        builder.RegisterType<SecondaryClass>().AsSelf();
        builder.RegisterType<Report>().AsSelf().InstancePerLifetimeScope();
  • 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-27T16:55:39+00:00Added an answer on May 27, 2026 at 4:55 pm

    As usual, it depends.

    If you want to express the fact that MainClass deliberately requires the same instance of Report as the instance of SecondaryClass uses, then you should either take the Report from the SecondaryClass as @adrift has suggested in comments, or use parametrization:

    public class MainClass
    {
     public delegate MainClass Factory();
     public MainClass(Report report, Func<Report, SecondaryClass> secClassFactory)
     {
         Report=report;
         SecondaryClass = secClassFactory(report);
     }
     public Report Report {get; private set;}
     public SecondaryClass SecondaryClass {get; private set;}
    }
    
    public class SecondaryClass
    {   
     public SecondaryClass(Report report)
     {
         Report=report;
     }
     public Report Report {get; private set;}
    }
    

    (see autofac wiki for reference)
    With these approaches you directly show that you require that two objects use the same Report.

    If your SecondaryClass and MainClass do not care if they use the same Report, and it’s just your wish to make them share it, you could use a separate lifetime scope as @adrift has suggested. But beware that if you use other components that depend on Report, they’ll also get the same instance inside that lifetime scope. It will be kind of a singleton inside the scope.

    I do not recomment using lifetime scope if your components do depend on using the same instance. If you did, the requirement would be obfuscated which is bad.

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

Sidebar

Related Questions

Assuming that I have the following interface and class: public interface IFooRepo : IDisposable
I have been looking for a way to register a factory class in autofac
I have run into an issue while creating a data service and using Autofac
I have started to use Autofac following this tutorials: http://flexamusements.blogspot.com/2010/09/dependency-injection-part-3-making-our.html Simple class with no
I'm using Autofac with ASP.NET MVC so all my controllers have their dependencies resolved
I'm using Autofac 2.4.4.705. The output of the following code is: 1 (which means
I am using Autofac 2.4.5.724 under vb.net 2010. I have several classes without default
I'm using autofac framework with xml configuration. I have a question, here is the
I am bit confused I have a snippet on domainevents where ` public class
I am using Autofac as my IoC container. I have: IRepository<> , my repository

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.