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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:35:23+00:00 2026-06-02T16:35:23+00:00

I have a IDataContext interface implemented by a InMemoryDataContext and MyApplicationDataContext . This is

  • 0

I have a IDataContext interface implemented by a InMemoryDataContext and MyApplicationDataContext. This is consumed by all of my repositories which are defined as BananaRepository : IBananaRepository and take the data context in their constructor:

interface IDataContext {}
class InMemoryDataContext : IDataContext {}
class MyApplicationDataContext : IDataContext {}

interface IBananaRepository {}
class BananaRepository : IBananaRepository 
{
    public BananaRepository(IDataContext dataContext) {}
}

So far my consumers of the interfaces and services are ASP.NET MVC controllers, synchronous commands and queries. NInject is configured in my Web project and IDataContext is bound to MyApplicationDataContext with InRequestScope().

 kernel.Bind<IDataContext>().To<MyApplicationDataContext>().InRequestScope();
 kernel.Bind<IBananaRepository>().To<BananaRepository>();

I have reached the point in the evolution of my project where I want to start adding asynchronous processing (commands, events+handlers, etc). The problem I am facing is that for those I need to get a transient IDataContext (new one each time) but the binding is already set-up for a IDataContext instance per request for my controllers.

Consider this simple scenario, where my DoSomethingAsyncCommand will execute on a new thread:

class DoSomethingAsyncCommand : IAsyncCommand<TArgs>
{
    public DoSomethingAsyncCommand(IBananaRepository repository) {}

    public bool Execute(TArgs args) {}
}

I want when NInject instantiates a class implementations of IAsyncCommand the IBananaRepository (and all my other repositories) to be initialized with a new instance of IDataContext rather than the one for the web request to be reused (effectively I want for IAsyncCommand my IDataContext to be bound as InTransientScope())

How can I do that?

P.S: I am using the CommonServiceLocator rather than the Ninject kernel directly to instantiate IAsyncCommand instances.

  • 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-02T16:35:24+00:00Added an answer on June 2, 2026 at 4:35 pm

    Look at https://github.com/ninject/ninject/blob/master/src/Ninject/Planning/Bindings/BindingConfigurationBuilder.cs

    There you find IsAnyAnchestorNamed. You can use that same loop and combine it with the condition you find in WhenInjectedInto and call it from a custom When.

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

Sidebar

Related Questions

I have a custom DataContext with a Table member... Entry is defined as... [Table(Name=Entry)]
I have a grid bound to a BindingSource which is bound to DataContext table,
I have implemented the Unity DI in a project of mine, but I have,
I have the following (C#) code namespace A { public interface IX { bool
I have an ObservableCollection which is dataContext for my treeview when I try to
Can someone tell my why when I have wcf contract: [ServiceContract] public interface IService1
I have some interface and class that contain List public interface IListContain { Int32
I have started using Linq to SQL in a (bit DDD like) system which
I have the following Linq to SQL query, in which I'm trying to do
I have an entity like this: public class Person { public string Name {

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.