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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:24:41+00:00 2026-06-06T10:24:41+00:00

The Guice IoC container provides MapBinder, which is often useful. We’re also using Windsor

  • 0

The Guice IoC container provides MapBinder, which is often useful.
We’re also using Windsor v3.0 in .NET.
Given the following slightly contrived example, can anybody suggest how Windsor could be used in the same way?

enum Format {
    Xml,
    Json
}
interface Formatter {
    String format(Object o);
}
class XmlFormatter implements Formatter {
    @Override
    public String format(Object o) {
        throw new NotImplementedException();
    }
}
class JsonFormatter implements Formatter {
    @Override
    public String format(Object o) {
        throw new NotImplementedException();
    }
}
class FormattersModule extends AbstractModule {
    @Override
    protected void configure() {
        MapBinder<Format, Formatter> formattersMapBinder
                = MapBinder.newMapBinder(
                        binder(), Format.class, Formatter.class);
        formattersMapBinder.addBinding(Format.Xml).to(XmlFormatter.class);
        formattersMapBinder.addBinding(Format.Json).to(JsonFormatter.class);
    }
}
class FormattersClient {
    private final Map<Format, Formatter> formatters;
    @Inject
    FormattersClient(Map<Format, Formatter> formatters) {
        this.formatters = formatters;
    }
    public void Format(Format format, Object o) {
        String s = formatters.get(format).format(o);
        // ... do something with s ...
    }
}
public class Main {
    private static Injector injector;
    public static void main(String[] args) {
        injector = Guice.createInjector(new FormattersModule());
        FormattersClient formattersClient
                = injector.getInstance(FormattersClient.class);
        formattersClient.Format(Format.Json, new String("Foo"));
        formattersClient.Format(Format.Xml, new String("Foo"));
    }
}
  • 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-06T10:24:42+00:00Added an answer on June 6, 2026 at 10:24 am

    So this example is quite straightfoward, if you’re happy to use a string value for the key.

    class FormattersInstaller : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(
                Component
                    .For<IFormatter>()
                    .ImplementedBy<XmlFormatter>()
                    .Named(Format.Xml.ToString()));
            container.Register(
                Component
                    .For<IFormatter>()
                    .ImplementedBy<JsonFormatter>()
                    .Named(Format.Json.ToString()));
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var container = new WindsorContainer();
            container.Install(new FormattersInstaller());
            var xmlFormatter = container.Resolve<IFormatter>(Format.Xml.ToString());
            var jsonFormatter = container.Resolve<IFormatter>(Format.Json.ToString());
            // use the formatters...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a Java Swing application with Google Guice as an IOC container.
I have trouble running Guice 3 within an OSGi container. Following is a simple
I'm working on Java Swing application with Google Guice as an IOC container. Things
I'm using Google Guice with Jersey (jax-rs). Following method throws an JAXB-Exception (JAXB can't
I'm using the Unity IOC container and I'm just wondering what is the best
I'm using Google Guice for dependency injection. Suppose I have the following: public interface
I have the following problem: Given a Guice type literal TypeLiteral<T> template and a
Guice provides two variations of so-called binding annotations , which seem to really break
I'm using Guice 3 on Google App Engine 1.6.4. Today, I saw something bad
I am using Guice with JDO and Datanucleus in my desktop app. I am

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.