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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:22:31+00:00 2026-05-27T13:22:31+00:00

I have a console app with config: <?xml version=1.0 encoding=utf-8 ?> <configuration> <configSections> <section

  • 0

I have a console app with config:

<?xml version="1.0" encoding="utf-8" ?> <configuration>
    <configSections>
        <section name="unity" type=
           "Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, 
             Microsoft.Practices.Unity.Configuration" />
    </configSections>
    <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
        <assembly name="UnityDi.Contracts" />
        <assembly name="UnityDi.Domain" />
        <assembly name="UnityDi.Services" />
        <assembly name="UnityDi.Repositories" />
        <namespace name="UnityDi.Contracts" />
        <namespace name="UnityDi.Domain" />
        <namespace name="UnityDi.Services" />
        <namespace name="UnityDi.Repositories" />
        <container>
            <register type="IUser" mapTo="User"></register>
            <register type="IUserService" mapTo="UserService"></register>
            <register type="IUserRepository" mapTo="UserRepository"></register>
        </container>
    </unity> 
</configuration>

and Program.cs

namespace UnityDi.Console
{
    using System;
    using Contracts;
    using Microsoft.Practices.Unity;
    using Microsoft.Practices.Unity.Configuration;

    public static class Program
    {
        private static readonly IUnityContainer Container = 
           new UnityContainer().LoadConfiguration();

        public static void Main()
        {
            var dummyUserOne = GetUserReference();

            dummyUserOne.UserId = 1;
            dummyUserOne.FullName = "Bilbo Baggins";

            var dummyUserTwo = GetUserReference();

            dummyUserTwo.UserId = 2;
            dummyUserTwo.FullName = "Frodo Baggins";

            var userService = GetUserServiceReference(); 

            userService.Add(dummyUserOne);
            userService.Add(dummyUserTwo);

            var users = userService.GetAllUsers();

            foreach (var user in users)
            {
                Console.WriteLine(user.FullName);
            }

            Console.ReadLine();
        }

        private static IUser GetUserReference()
        {
            return Container.Resolve<IUser>();
        }

        private static IUserService GetUserServiceReference()
        {
            return Container.Resolve<IUserService>();
        }
    }
}

That works really well.

I’ve been doing a lot of searching around and find that solutions published integrating Unity with MVC3 seem to be very verbose in their approach.

Within the context of the code above (and with a requirement to wire up Entity Framework code first), what’s the SIMPLEST way for me to integrate Unity with MVC3 and work with my data like above?

I want to use the exact same approach to creating my objects in MVC as above.

Thanks!

Richard

P.s. The closest I found was Unity.Mvc but I can’t get it to work like above. I’m probably being stupid though. This is new to me.

EDIT: Working with the proposed answer.

In Web.config for the MVC app (with Unity.Mvc package added)

[snip]
<container>
    <register type="IUser" mapTo="User"></register>
    <register type="IUserService" mapTo="UserService"></register>
    <register type="IUserRepository" mapTo="UserRepository">
        <lifetime type="HierarchicalLifetimeManager" />
    </register>
</container>

Then in Bootstrapper.cs

namespace Unity.Mvc.Resources
{
    using System.Web.Mvc;
    using Microsoft.Practices.Unity;
    using Microsoft.Practices.Unity.Configuration;
    using Unity.Mvc3;

    public static class Bootstrapper
    {
        public static void Initialise()
        {
            var container = BuildUnityContainer();

            DependencyResolver.SetResolver(new UnityDependencyResolver(container));
        }

        private static IUnityContainer BuildUnityContainer()
        {
            var container = new UnityContainer().LoadConfiguration();

            container.RegisterControllers();

            return container;
        }
    }
}

Then in the controller (bit of an a-ha moment when I realised the Interface can be any Unity knows about, including it’s own):

private readonly IUnityContainer container;

public HomeController(IUnityContainer container)
{
    this.container = container;
}

public ActionResult Index()
{
    var dummyUserOne = GetUserReference();

    dummyUserOne.UserId = 1;
    dummyUserOne.FullName = "Bilbo Baggins";

    var dummyUserTwo = GetUserReference();

    dummyUserTwo.UserId = 2;
    dummyUserTwo.FullName = "Frodo Baggins";

    var userService = GetUserServiceReference();

    userService.Add(dummyUserOne);
    userService.Add(dummyUserTwo);

    var users = userService.GetAllUsers();

    return View(users);
}

private IUser GetUserReference()
{
    return container.Resolve<IUser>();
}

private IUserService GetUserServiceReference()
{
    return container.Resolve<IUserService>();
}

And again, it works 🙂

That just leaves the Entity Framework aspect. Need to get DbContext wired up to work with my Domain objects without introducing additional dependencies… Any pointers? Really, very much appreciated.

  • 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-27T13:22:32+00:00Added an answer on May 27, 2026 at 1:22 pm
    1. Install the Unity.Mvc3 nuget package.
    2. go to the bootstrapper code and add your mapping (get it working like that then fiddle around with doing it in a config file if you so desire)
    3. The bootstrapper will ‘hook’ into mvc to let it know for controller resolution to use unity.
      ex:

      container.RegisterType<ICustomerRepository, CustomerRepository>();

    4. Any constructors for controllers will look for any interfaces unity needs to do mappings on. If it has to inject IService, it will (As long as it knows about it).
      If it has to repeat this process throughout the object grab (IService required IRepository, etc on down the graph) as long as unity knows about it, it will wire everything up.

    For the additional question on the Entity Framework, I use an IContext class. In here I define IDbSet Customers. This is similar to this implementation:
    http://leomburke.wordpress.com/category/entity-framework/

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

Sidebar

Related Questions

If I create an app.config file in a console apps like this <?xml version=1.0
I have a C# .NET Console application exe with an app.config specifying a handful
I have console app A that references another console app B. B app has
I have a console app that needs to display the state of items, but
I have one console app that is doing some lengthy syncing to an ftp
I have a console app that performs a lengthy process. I am printing out
I have a console app that's geared to be automatically ran as a Scheduled
I have a C# console app App1 that reads a row of data from
I have a .Net console App which using a scheduled event will start, call
What am I doing wrong? I have a simple console app in VS08. When

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.