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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:36:51+00:00 2026-05-15T05:36:51+00:00

I have a search form that can search in different provider. I started out

  • 0

I have a search form that can search in different provider.
I started out by having a base controller

public SearchController : Controller
{

    protected readonly ISearchService _searchService

    public SearchController(ISearchService searchService)
    {
        _searchService= searchService;
    }

    public ActionResult Search(...)
    {
        // Use searchService to query and return a view.
    }

}

And child controllers

TwitterController : SearchController
{
    ...
}

NewsController : SearchController
{
    ...
}

I use StructureMap to insert all my dependencies in the controller. With this setup, I was able to change the SearchService depending on the type of the controller being instanciated.

x.For<ISearchService>().ConditionallyUse(o =>
      {
            o.TheDefault.Is.OfConcreteType<NewsSearchService>();

            o.If(c => c.ParentType == typeof(TwitterController))
             .ThenIt.Is.OfConcreteType<TwitterSearchService>();

             ...

      });

That even allowed me to set different Views for each controller, (just putting the corresponding folder (Twitter, News…) and the Parent controller is still handling all the Search, with a simple

return View(results) 

which is displaying the correct view specific to twitter, news, or other

Now that was cool and looked great, I a single form and the different views are displayed in tabs on the same page. That’s where it starts to get complicated with this approach. The form has to post to /Twitter to search in twitter, to /News to search in news… which means I should change the action parameter of the form depending on which tab I am and display the correct tab on when the form returns depending on.. the url? craziness follows.

If you have built something like this already or know what’s the best approach to this, please advices are welcome.

Now I think I would have less pain using a parameter in the form and posting to a single controller. I am thinking of injecting the correct SearchService depending on this parameter. What would be the best approach? I thought of using a model binder,

So I would have my ActionMethod that look like this:

public ActionResult Search(ISearchService service, Query query)
{
    var results = service.Find(query);
}

But I think would need to make a call like this in the ModelBinder

ObjectFactory.GetInstance(...);

Based on the querystring parameter that describe which provider to use, and that doesn’t seem more elegant to me. I feel stuck, help :(.

  • 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-15T05:36:51+00:00Added an answer on May 15, 2026 at 5:36 am

    I was trying to figure out how to use the abstract factory pattern and still let structuremap resolve all the dependencies of my components.

    I believe that is the way I am going to implement it, but I submit this here to get some feedback if someone would read this.

    As explain in the previous answer, I do not want to build the whole object graph depending on which provider I need in the Abstract factory.

    ie :

    class StatServiceFactory : IStatServiceFactory
    {
        public IStatService Create(string provider)
        {
            switch(provider)
            {
                case "blog":
                    return new  StatService(IFacetRepository,ISearchManager,IConfigManager,BooleanQueryBuilder);
                           //How to resolve the Config, the SearchManager, and BooleanQueryBuilder?   
                           //Add more abstract factories? It starts to get messy in my opinion...
             }
        }
    
    }
    

    What I can do is have the abstract factory use my container to create an instance of my search managers depending on a parameter (coming from the querystring in my case)

    Structuremap allows to create named instances this way :

    x.For<ISearchManager>().Use<AbcSearchManager>().Named("Abc");
    x.For<ISearchManager>().Use<DefSearchManager>().Named("Def");
    

    I need a way to inject the container in my Abstract factory.
    I would probably wrap the container in a wrapper defined like this. That would keep me from leaking Structuremap into my project. I dont need more that those 2 features within the abstract factory anyway, but it is not necessary:

    public interface IContainerWrapper
    {
        object GetInstance<T>();
        object GetNamedInstance<T>(string key);
    }
    

    and the implementation :

    public class ContainerImpl : IContainerWrapper
    {
         private readonly Container _container
         public ContainerImpl(Container container)
         {
              _container = container;
         }
    
         ...
    }
    

    And setup StructureMap to resolve dependencies to my abstract factory like that :

    x.For<IContainer>.Use(new ContainerImpl(this));
    x.For<IFactory>.Use<Factory>()
    

    My factory would be then much simpler and would create my instance like that :

    public class SearchmanagerFactory
    {
        private readonly IContainerWrapper _container;
    
        public SearchmanagerFactory(IContainerProvider containerProvider)
        {
            _container = containerProvider;
        }
    
        public ISearchManager Create(string provider)
        {
           //eed to handle the bad input for provider.
            return (ISearchManager)
                _container.Resolve<ISearchManager>(provider);
        }
    }
    

    That seems pretty clean this way :).
    Thoughts?

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

Sidebar

Related Questions

I have a search form that allows users to search on several different fields
I have a form that is going to be used to search through a
I have made an normal form that you can enter a user´s fullname in
I have a search form with a query builder. The builder is activated by
I have a search form in an app I'm currently developing, and I would
I have a search form on each of my pages. If I use form
I have a search form and result list. The form allows the user to
I have a really simple search form with the following Label (Search) Textbox (fixed
I have search in MSDN and I can't figure where are the POST parameters
I have a search box that doesn't have a submit button, I need to

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.