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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:28:00+00:00 2026-05-27T18:28:00+00:00

I have been using Unity as my IoC container for a while and I

  • 0

I have been using Unity as my IoC container for a while and I could not find the correct solution for a recurrent problem. I need to create some instances of AuditInsertLogWriter and AuditUpdateLogWriter (they implement IAuditOperationLogWriter interface) depending on the operation argument as you can see bellow:

public IAuditOperationLogWriter Create(Operation operation)
{
    switch (operation)
    {
        case Operation.Insert:
            return UnityContainer.Resolve<AuditInsertLogWriter>();
        case Operation.Update:
            return UnityContainer.Resolve<AuditUpdateLogWriter>();
        default:
            break;
    }
}

The thing is that those instances are complex to create because of their own dependencies. Moreover, I want to remove the dependency with Unity in this factory. So, my question is: how can I achieve that Unity resolves the correct type to create depending on some context?

  • 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-27T18:28:00+00:00Added an answer on May 27, 2026 at 6:28 pm

    If you want to remove the dependence of the factory on the container you will have to wire up all of the dependencies ahead of time.

    An elegant way to do this would be with Unity’s support for automatic factories.

    You could define your factory like this:

    public class AuditOperationLogWriterFactory
    {
        private Dictionary<Operation, Func<IAuditOperationLogWriter>> auditCreator = 
            new Dictionary<Operation, Func<IAuditOperationLogWriter>>();
    
        public AuditOperationLogWriterFactory(Func<AuditInsertLogWriter> insert,
            Func<AuditUpdateLogWriter> update)
        {
            auditCreator[Operation.Insert] = insert;
            auditCreator[Operation.Update] = update;
        }
    
        public IAuditOperationLogWriter Create(Operation operation)
        {
            if (auditCreator.ContainsKey(operation))
            {
                return auditCreator[operation]();
            }
    
            return null;
        }
    }
    

    And then you can just resolve the factory and Unity should wire up the dependencies and the object creation is deferred until runtime (based on context). If they are complicated you may have to help via configuration (config file, code, or attributes).

    IUnityContainer container = new UnityContainer();
    
    var factory = container.Resolve<AuditOperationLogWriterFactory>();
    
    var logWriter = factory.Create(Operation.Insert);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using Unity for quite a while but I have always used
I have been using the Windsor IoC Container for my web-based application, to resolve
I have been using IoC for a little while now and I am curious
We have been using CruiseControl for quite a while with NUnit and NAnt. For
I have been using C# for a while now, and going back to C++
I've been using Unity for some time in my project. I have a singleton
I've been using EntLib for some time, and have recently found Unity. At first
So I have been searching for a solution for this a while now, and
I have been using CPPUnit as a unit testing framework and am now trying
I have been writing unit tests using NUnit and Moq with my Silverlight code

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.