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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:55:05+00:00 2026-06-05T17:55:05+00:00

I’m using Autofac and want to resolve the correct implementation of the current assembly

  • 0

I’m using Autofac and want to resolve the correct implementation of the current assembly

I have a DataContextFactory Interface and Class:

Public Interface IDataContextFactory
  Inherits IDisposable

  Function GetDataContext() As IDataContext

End Interface

and the Implementation of the Interface

Public Class CDataContextFactory
  Implements IDataContextFactory

  Private m_oDbContext As IDataContext

  Public Sub New(ByVal i_oDbContext As IDataContext)
    m_oDbContext = i_oDbContext
  End Sub

  Public Function GetDataContext() As CoreData.IDataContext Implements CoreData.IDataContextFactory.GetDataContext
    Return m_oDbContext
  End Function

End Class

So now I have in every registered assembly different IDataContext Implementations. For example I have an assembly called ReportData with the data context

Public Class CReportDataContext
  Inherits DbContext
  Implements IDataContext

    ---
End Class

And also one implementation inside an other Assembly CommonData

Public Class CFacadeDataContext
  Implements IDataContext

    ---
End Class

Then I have in every Assembly an implementation of my IRepository. For example

Public MustInherit Class CBaseReadRepository(Of T As {IEntity, Class})
  Implements IReadRepository(Of T)

  Private m_oDataContextFactory As IDataContextFactory
  Private m_oDataContext As IDataContext

  Protected ReadOnly m_oObjectDataSet As CQuery(Of T)

  Public Sub New(ByVal i_oDataContextFactory As IDataContextFactory)
    m_oDataContextFactory = i_oDataContextFactory
    m_oObjectDataSet = DataContext.ObjectDataSet(Of T)()
  End Sub

    ----
End Class

So how can I solve that the DataContextFactory will resolve the CReportDataContext inside the Assembly ReportData and the CFacadeDataContext inside the Assembly CommonData

Here is my ContainerBuilder registration:

Dim builder As New ContainerBuilder()

Dim oData = Assembly.Load("ReportData")
builder.RegisterAssemblyTypes(oData).Where(Function(t) t.Name.EndsWith("DataContext")).As(Of IDataContext) _
  .AsImplementedInterfaces.SingleInstance

oData = Assembly.Load("CommonData")
builder.RegisterAssemblyTypes(oData).Where(Function(t) t.Name.EndsWith("DataContext")) _
  .AsImplementedInterfaces().SingleInstance

builder.RegisterAdapter(Of IDataContext, IDataContextFactory)(Function(x) New CDataContextFactory(x))

Thanks

  • 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-05T17:55:07+00:00Added an answer on June 5, 2026 at 5:55 pm

    Autofac doesn’t have built-in support for this sort of use case. Generally it’s recommended that you try not to tie specific implementations to consumers because that breaks the whole IoC pattern – you may as well “new-up” the dependency type you need right in the class rather than injecting it.

    If you absolutely must tie them together, you only have a couple of options. Neither is pretty, and both will require you to change the way you register things – you may not be able to do the RegisterAssemblyTypes assembly scanning like you do now.

    First, you could use named registrations. When you register your IDataContext, you give it a name. When you register your consuming class, you tell the builder which named instance you expect to use.

    builder.RegisterType<MyDataContext>().Named<IDataContext>("some-name");
    var contextParam = ResolvedParameter.ForNamed<IDataContext>("some-name");
    builder.RegisterType<MyConsumer>().As<IConsumer>().WithParameter(contextParam);
    

    Second, you could register an expression rather than a type for the consumer:

    builder.Register(c => new Consumer(new SomeContext())).As<IConsumer>();
    

    Finally, you could create a special module that does the work of figuring out which assembly the consumer is coming from and try to match it to a corresponding IDataContext. This is more “automatic” but is a lot more complex. A stub might look like this:

    public class DataContextModule : Autofac.Module
    {
      protected override void AttachToComponentRegistration(
        IComponentRegistry componentRegistry,
        IComponentRegistration registration)
      {
        registration.Preparing += OnComponentPreparing;
      }
    
      public static void OnComponentPreparing(object sender, PreparingEventArgs e)
      {
        Type typeBeingResolved = e.Component.Activator.LimitType;
        // TODO: Do some reflection to determine if the type takes an IDataContext
        // in the constructor. If it doesn't, bail. If it does...
    
        var parameter = new ResolvedParameter(
          (p, i) => p.ParameterType = typeof(IDataContext),
          (p, i) => {
            // TODO: Use i (the IComponentContext for the resolution)
            // to locate the right IDataContext from the list of registrations,
            // resolve that one, and return it so it can be used in
            // constructing the consumer object.
          });
      }
    }
    

    Like I said, not pretty.

    If you have the ability to influence your design, it might be easier to make marker interfaces, like:

    public interface ICoreDataContext : IDataContext { }
    

    And then in your constructors take the specific interface:

    public SomeClass(ICoreDataContext context);
    

    That way type resolution would just work. (Marker interfaces aren’t the greatest pattern in the world, either, but it’s arguably better than tying individual implementations of things to specific consuming types.)

    • 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 have a French site that I want to parse, but am running into
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the

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.