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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:38:22+00:00 2026-06-01T04:38:22+00:00

Here is my incomplete StructureMap configuration: PS: Big apologies for asking you to write

  • 0

Here is my incomplete StructureMap configuration:

PS: Big apologies for asking you to write my app for me but I’m finding the StructureMap api to be a bit confusing as nearly everything I find when I do google searches refers to the older api.

    public static void Configure(IContainer container)
    {
        container.Configure(c =>
        {
            string connectionString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
            SQLDataContext dataContext = new SQLDataContext(connectionString);

            c.For<SQLDataContext>().HttpContextScoped();

            c.For<IAdminRepository>().Use<SQLAdminRepository_v2>().Ctor<SQLDataContext>().Is(dataContext);
            c.For<IMemberRepository>().Use<SQLMemberRepository_v2>().Ctor<SQLDataContext>().Is(dataContext);
            c.For<IUtilityRepository>().Use<SQLUtilityRepository_v2>().Ctor<SQLDataContext>().Is(dataContext);

            c.For<IAdminService>().Use<AdminService_v2>();
            c.For<IMemberService>().Use<MemberService_v2>();
            c.For<IUtilityService>().Use<UtilityService_v2>();

            c.For<ResourcePool>().Singleton();

        });
    }

Each Service has a dependency on the corresponding Repository which is passed in via its constructor. e.g. :

public class ContactService_v2 : IContactService
{
    IContactRepository contactRepository;

    public ContactService_v2(IContactRepository contactRepository)
    {
        this.contactRepository = contactRepository;
    }

    public IQueryable<Contact> Get_Contacts()
    {
        return contactRepository.GetContacts();
    }

    public void Save_Contact(Contact contact)
    {
        contactRepository.Save_Contact(contact);
    }
}

So the Service layer is just facade with the real Repository lying behind it. [and before you ask or suggest – No I won’t be changing that in a hurry because initially I just want to get the DI working to make the code testable before I start doing anything dramatic.]

Q1: The application uses Linq2Sql data access. It is essential that all the Services sent to each controller have, in their Repository dependency a dependency on the same data context instance, because they may need to join to each other in complex queries made across Repositories. Therefore I want to use a HttpContext lifecycle for the data context. Is this (above) how I do that?

Q2: The ResourcePool used to follow a singleton pattern but I have refactored it to a ordinary class taking a single service as its constructor parameter. Is that right? In this case, will it behave as if it came from the asp.net application cache?

Q3: What are the auto-registration equivalents for the Repository and Service configurations? [PS: Yes I know that the “_v2” suffix is unconventional but there already exist legacy equivalents of each XService and XRepository which I wish to refactor away at my leisure after the DI has been added and is working seamlessly].

Asp.Net MVC allows one to have strong model binding like so :

[HttpPost]
public ActionResult List(AdminDisplay admin)
{
    admin.GetAdminPage();
    return View(admin);
}

In such a case every class (such as AdminDisplay) requires a default parameterless constructor so that the MVC framework can create it. Furthermore any class created inside such a model bound class also needs such a parameterless constructor.

Q4: If I want to replace these parameterless constructors, is it sufficient to just add the entries for them in the StructurMap configuration code? – or do I not need to do that provided that all these model binding classes and their dependents have contructors which only use parameters what StructureMap is able to resolve.

Apologies for the bad formatting above but how do I turn off wysiwyg editing?

PS: It is permissible to provide answer any of the questions Q1, Q2, Q3, Q4 without answering all of them!

  • 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-01T04:38:23+00:00Added an answer on June 1, 2026 at 4:38 am

    Q1:
    Change the code for the data context from:

    SQLDataContext dataContext = new SQLDataContext(connectionString);
    c.For<SQLDataContext>().HttpContextScoped();
    

    to:

    c.For<SQLDataContext>().HybridHttpOrThreadLocalScoped()
    .Use(() => new SQLDataContext(connectionString));
    

    Change the code for each repository from:

    c.For<IAdminRepository>().Use<SQLAdminRepository_v2>()
    .Ctor<SQLDataContext>().Is(dataContext);
    

    to:

    c.For<IAdminRepository>().Use<SQLAdminRepository_v2>()
    .Ctor<SQLDataContext>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the error i'm getting CookBook.Tests.CategoryRepository_Fixture.Can_update_category: FluentNHibernate.Cfg.FluentConfigurationException : An invalid or incomplete configuration
Here is the scenario: I'm writing an app that will watch for any changes
I have googled and searched here on stackoverflow, but haven't been able to find
I'm using OCMock trying to test the behavior of NSURLConnection. Here's the incomplete test:
I'm probably missing something easy, but I seem to be blocked here... I have
I have only found one other solution but it was incomplete so I need
I'm having a bit of a problem here. What I want to do is
The code here is still incomplete because I'm still going to ask you guys
I am sorry for the incomplete information. Here's my two sp's: SP1 - DELIMITER
The documentation is rather incomplete. I could not set up the inbound email. Here

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.