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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:19:09+00:00 2026-05-17T23:19:09+00:00

I am currently using/experimenting with autofac as my IoC controller. Previously to this I

  • 0

I am currently using/experimenting with autofac as my IoC controller.

Previously to this I used a simple static class defining two methods, similar to

public static TService Resolve<TService>()  
public static void Register<IType, ImpType>()

where ImpType must be of IType.

Now over to autofac. When registering, you might do something like

builder.RegisterType<ProductRepository>().As<IProductRepository>(); 

however if ProductRepository is-not-a IProductRepository you don’t get a compile error?
Is there some way of wiring things up more safely if desired?

Secondly, when building my Ioc modules I use something like

public static class IoCContainer
{
    public static IContainer BaseContainer { get; private set; }

    public static void Build(ContainerBuilder builder)
    {
        BaseContainer = builder.Build();            
    }
}

After I have called IoCContainer.Build(..) I can no longer register anything ‘into’ the BaseContainer. Compare this with the simple model where you can register anything from anywhere. Perhaps this is by design?

  • 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-17T23:19:10+00:00Added an answer on May 17, 2026 at 11:19 pm

    I think the .RegisterType<Foo>.As<IFoo> pattern is not type safe simply because the C# compiler (or the CLR type system) does not handle such type constraints. For example, the following hypothetical declaration of the As method won’t compile:

    interface IRegistration<TImplementation>
    {
       void As<TContract>() where TImplementation : TContract;
    }
    

    The compiler error is “‘SomeNamespace.IRegistration.As()’ does not define type parameter ‘TImplementation'”.

    Is there some way of wiring things up more safely if desired?

    The following seems to work (though it is discouraged by the best practices section on the Autofac wiki). It will give a compiler error unless Foo implements IFoo:

         var builder = new ContainerBuilder();
         builder.Register<IFoo>(c => new Foo()).SingleInstance();
    
         var container = builder.Build();
         var foo = container.Resolve<IFoo>();
    

    c is the IComponentContext. If the Foo constructor requires a constructor argument then you can write c => new Foo(c.Resolve<IBar>()).

    After I have called IoCContainer.Build(..) I can no longer register anything ‘into’ the BaseContainer

    You can update the container in Autofac 2.2.

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

Sidebar

Related Questions

I'm currently doing a bit of experimenting using Autofac-1.4.5.676, autofac contrib and castle DynamicProxy2.
I'm currently using DPack as this adds a Collapse All Projects option to the
I'm currently experimenting with using OCaml and GTK together (using the lablgtk bindings). However,
I've been experimenting with using the <canvas> tag for drawing simple diagrams and charts,
I am currently learning and experimenting with C and am using Bloodshed's DEV-C++ as
I am currently experimenting with JPA in a desktop application, while using BeansBinding to
I'm currently experimenting with using WCF Data Services as a way to open up
currently using jq 1.4.2 And i have gone through this forum and other forums
i'm currently experiencing a little problem. I'm using nhibernate with around 40 entities mapped
Currently using System.Web.UI.WebControls.FileUpload wrapped in our own control. We have licenses for Telerik. I

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.