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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:10:32+00:00 2026-05-20T13:10:32+00:00

I am doing a project in ASP.NET MVC in which I want to implement

  • 0

I am doing a project in ASP.NET MVC in which I want to implement an IoC container. I am new to IoC containers and have been searching to find a solution to my problem, but so far to no avail.

The problem is dat data is distributed across various databases. Once a user is logged in, the database for the user is retrieved from an authorization database and then set in the user session. That user session is passed on to the services and repositories when they are constructed, so they can use it when they need it to access the database.

A typical bare stripped service (without interfaces and ioc) looks like this:

    private CompetitionRepository _CompetitionRepo;

    public CompetitionService(DataContext dataContext)
        : this (new CompetitionRepository(dataContext))
    { }

    public CompetitionService(CompetitionRepository competitionRepo)
    {
        this._CompetitionRepo = competitionRepo;
    }

The “DataContext” contains a couple of properties with which the correct database is selected for the session. Because the Services and Repositories don’t have access to the session itself (which is outside of their scope) they need this object.

My question is, how can I set something like this up in IoC (doesn’t matter which container, I just want to know the general idea). As far as I can see, most containers do not allow objects to be passed in? Or is the design fundamentally flawed?

  • 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-20T13:10:32+00:00Added an answer on May 20, 2026 at 1:10 pm

    As far as I can see, most containers
    do not allow objects to be passed in?

    You can’t be looking far then 😉 Most IoC containers that I know have the ability to do this. You need to register an instance of an object based on its type. How and when that object is constructed is probably a deeper question than the one you’ve asked. However, to get to the point, you can do something like this with the Unity container:

    DataContext context = ..... ;
    container.RegisterInstance(context);
    

    Each time you create an instance of a class that requires a DataContext, you’ll get the one you’ve registered. If the type you want to associate the instance with is different, you can force it like so:

    container.RegisterInstance<IDataContext>(context);
    

    For more information, take a look here.

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

Sidebar

Related Questions

My team has a new Asp.net MVC intranet app. I have been doing some
I have been using telerik extensions for an asp.net mvc project, more specifically the
I have a form in an ASP.NET MVC project which has a select drop
I have an ASP.NET MVC 2 project in which I've created a data transfer
I have a c# asp.net MVC project. I am doing a a search and
I am stuck with redirecting problem in ASP.NET MVC project. I have mapped tables
I have an ASP.NET MVC project that works fine on my local machine (no
I'm doing project in asp.net mvc with Micorsoft Access database and I need to
I am attempting to implement a tagging system into my asp.net MVC project. When
I have an ASP.NET MVC 4 project with a lot of models to be

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.