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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:48:57+00:00 2026-05-14T04:48:57+00:00

In my project I register many ISerializers implementations with the assembly scanner. FWIW this

  • 0

In my project I register many ISerializers implementations with the assembly scanner. FWIW this is the code that registers my ISerializers

Scan(scanner =>
{
    scanner.AssemblyContainingType<ISerializer>();
    scanner.AddAllTypesOf<ISerializer>().NameBy(type => type.Name);
    scanner.WithDefaultConventions();
});

Which then correctly registers

ISerializer (...ISerializer)
Scoped as:  Transient

JsonSerializer    Configured Instance of ...JsonSerializer
BsonSerializer    Configured Instance of ...BsonSerializer

And so forth.

Currently the only way I’ve been able to figure out how to resolve the serializer I want is to hardcode a service location call with

jsonSerializer = ObjectFactory.GetNamedInstance<ISerializer>("JsonSerializer");

Now I know in my class that I specifically want the jsonSerializer so is there a way to configure a rule or similar that says for ISerializer’s to connect the named instance based on the property name? So that I could have

MySomeClass(ISerializer jsonSerializer, ....)

And StructureMap correctly resolve this scenario? Or am I approaching this wrong and perhaps I should just register the concrete type that implements ISerializer and then just specifically use

MySomeClass(JsonSerializer jsonSerializer, ....)

for something along these lines with the concrete class?

  • 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-14T04:48:58+00:00Added an answer on May 14, 2026 at 4:48 am

    When you’re doing Dependency Injection and need to be able to create specially-typed instances of a given interface, the recommended solution is to create specialized factory classes. This allows you to use a named argument without actually injecting the container.

    Example

    This is the abstract type that you’ll be injecting:

    public interface ISerializerFactory
    {
        ISerializer GetSerializer(string name);
    }
    

    Here is the concrete type, which makes use of your container (StructureMap):

    public class StructureMapSerializerFactory : ISerializerFactory
    {
        public ISerializer GetSerializer(string name)
        {
            return ObjectFactory.GetNamedInstance<ISerializer>(name);
        }
    }
    

    Then your class would look like the following:

    public class MyClass
    {
        private readonly ISerializerFactory serializerFactory;
    
        public MyClass(ISerializerFactory serializerFactory)
        {
            if (serializerFactory == null)
                throw new ArgumentNullException("serializerFactory");
            this.serializerFactory = serializerFactory;
        }
    
        public string SerializeSomeData(MyData data)
        {
            ISerializer serializer = serializerFactory.GetSerializer("Json");
            return serializer.Serialize(data);
        }
    }
    

    I’ve written this passing “Json” instead of “JsonSerializer” which won’t automatically work. But I think you should change your registration names to eliminate the redundant “Serializer” suffix (we already know it’s a serializer because we’re asking for an ISerializer). In other words create a method like this:

    private static string ExtractSerializerName(Type serializerType)
    {
        string typeName = serializerType.Name;
        int suffixIndex = typeName.IndexOf("Serializer");
        return (suffixIndex >= 0) ?
            typeName.Substring(0, suffixIndex - 1) : typeName;
    }
    

    And register it like this:

    scanner.AddAllTypesOf<ISerializer>().NameBy(type => ExtractSerializerName(type));
    

    Then you can just use the string “Json” to create it instead of “JsonSerializer”, which will look a little less ugly and feel less coupled.

    If you don’t like the hard-coded strings, then another thing you can do is create an enumeration for your factory:

    public enum SerializationFormat { Json, Bson, Xml };
    
    public interface ISerializerFactory
    {
        ISerializer GetSerializer(SerializationFormat format);
    }
    
    public class StructureMapSerializerFactory : ISerializerFactory
    {
        public ISerializer GetSerializer(SerializationFormat format)
        {
            return ObjectFactory.GetNamedInstance<ISerializer>(format.ToString());
        }
    }
    

    So instead of writing this:

    ISerializer serializer = serializerFactory.GetSerializer("Json");
    

    You get to write this instead:

    ISerializer serializer =
        serializerFactory.GetSerializer(SerializationFormat.Json);
    

    Which is going to be less error-prone in the long run.

    This will probably be more maintainable in the long run because if you start changing the class names of your serializers and/or the names are inconsistent, then you can replace the simple ToString() with a switch statement and actually map the enum values to the class names you’re registering.

    I’d probably put all of this code – including the auto-registration code in your question – in the same namespace, or even the same code file, to clearly indicate that these pieces are all interdependent.

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

Sidebar

Related Questions

I've got a project coming up that will involve connecting to one to many
I'm actually working on a PHP project that will feature a user system (Login,Register,Send
I have many JUnit tests, which are all created by Netbeans' assistant (so nothing
On a recent project I have been working on in C#/ASP.NET I have some
I want to register when the key Tab has been pressed, but can't figure
The ASP.NET MVC 1.0 (final) project template has basic membership built in, but I
I've a project which ask me to do such a BIG search engine but
I'm doing a survey of features in preparation for a research project. Name a
Sorry for long description, however the questions aren't so easy... My project written without

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.