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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:29:04+00:00 2026-06-12T01:29:04+00:00

I’m working with .net 4.5 and MVC4. I implemented Unity IoC as described in

  • 0

I’m working with .net 4.5 and MVC4.
I implemented Unity IoC as described in the following post: http://kennytordeur.blogspot.com/2011/05/aspnet-mvc-3-and-unity-using.html

But I would like to be able to have “register” my repository types using an external XML or within web.config. Is that possible?, samples would be greatly appreciated.

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-12T01:29:05+00:00Added an answer on June 12, 2026 at 1:29 am

    Unless there is a really strong reason to, you should register as much as possible in code. XML configuration is much more error prone, verbose and can become a maintenance nightmare very quickly. Instead of registering (all) your repository types in XML (which is possible with Unity), just put the assembly name containing that contains the repository types in the config and register them dynamically in code. This saves you from having to change the configuration every time you add a new repository implementation.

    Here is an example.

    In your configuration file, add a new appSetting with the name of the assembly:

    <appSettings>
        <add key="RepositoryAssembly" value="AssemblyName" />
    </appSettings>
    

    In your composition root, you can do the following:

    var assembly = Assembly.LoadFrom(
        ConfigurationManager.AppSettings["RepositoryAssembly"]);
    
    // Unity misses a batch-registration feature, so you'll have to
    // do this by hand.
    var repositoryRegistrations =
        from type in assembly.GetExportedTypes()
        where !type.IsAbstract
        where !type.IsGenericTypeDefinition
        let repositoryInterface = (
            from _interface in type.GetInterfaces()
            where _interface.IsGenericType
            where typeof(IRepository<>).IsAssignable(
                _interface.GetGenericTypeDefinition())
            select _interface)
            .SingleOrDefault()
        where repositoryInterface != null
        select new
        {
            service = repositoryInterface, 
            implemention = type
        };
    
    foreach (var reg in repositoryRegistrations)
    {
        container.RegisterType(reg.service, reg.implementation);
    }
    

    The LINQ query has a lot of subtle defects (for instance, it lacks checks for generic type constraints), but it will work for the common scenarios. If you work with generic type constraints, you should definitely switch to a framework that has support for this, because this is something that’s really hard to get right.

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

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.