Unity (IoC) with Strategy Pattern, replacing Switch statements

Posted on 2013-09-24
Medium Priority
Last Modified: 2013-10-05

Any tips how to use a IoC container (unity) when applying Strategy Pattern for replacing  Switch Statements

Applying Strategy Pattern Instead of Using Switch Statements

Implementing a Strategy Pattern using the Unity Framework
Question by:ulf-jzl
  • 2
LVL 25

Expert Comment

ID: 39525176
You can't use Ioc for a strategy as far i see.  Since you have to deceide which class you want to instantiate based on some value. Use factory partern instead, a simple way.

It is not as straight as it looks like for repository pattern.

Accepted Solution

ulf-jzl earned 0 total points
ID: 39525294
I solved it, don't know if there is a better way, but it works fine .

         var dataExporters = new object[]  
            var dataExporterTypes = new object[]  

            var ic= new InjectionConstructor(
                new ResolvedParameter<IExporter>("CsvExporter"),
                new ResolvedArrayParameter<DataExporterType>(dataExporterTypes),
                new ResolvedArrayParameter<IDataExporter>(dataExporters));

            container.RegisterType<IExporterFactory, ExporterFactory>("ExporterFactory", (ic));

public class ExporterFactory : IExporterFactory
    private readonly Dictionary<DataExporterType, IDataExporter> strategies =
        new Dictionary<DataExporterType, IDataExporter>();

    private readonly IExporter exporter;

    public ExporterFactory(
        IExporter exporter,
        DataExporterType[] measurementType,
        IDataExporter[] dataExporters)
        this.exporter = exporter;

        for (int i = 0; i < measurementType.Length; i++)
            strategies.Add(measurementType[i], dataExporters[i]);

    public IExporter CreateExporter(DataExporterType measurementType)
        return this.exporter.Create(strategies[measurementType]);

Open in new window


Author Closing Comment

ID: 39548983
Solved it

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…
To export Lotus Notes to Outlook PST or Exchange and Domino Server files to Exchange Server or PST files with ease, go for Kernel for Lotus Notes to Outlook conversion tool. Through the video, you can watch the conversion process. A common user with…

587 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question