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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:17:13+00:00 2026-05-24T10:17:13+00:00

A quick description of the environment: I have a class that represents a chatroom

  • 0

A quick description of the environment:

I have a class that represents a chatroom and has a dependency on a logger. It’s not the same as a system-wide logger with cross-cutting concerns, but a logger that’s tied to that specific chatroom. It logs all activity in that chatroom to it’s unique log file. When the chatroom is created I want to open the log file, and when it’s destroyed I want to close the log file.

The Problem

Here’s the relevant code I’m using.

public interface IChatroomLogger
{
    void Log(ServerPacket packet);
    void Open();
    void Close();
}

public class ChatroomLogger : IChatroomLogger
{
    // chatroom name will be used as a file name
    public ChatroomLogger(string chatroomName) { ... }
    public void Log(ServerPacket packet) { ... }
}

public class Chatroom
{
    public Chatroom(string name, IChatroomLogger logger)
    {
        this.name = name;
        this.logger = logger;
        this.logger.Open();
    }

    public IChatromLogger Logger { get { return this.logger; } }
}

public interface IChatManager
{
    Chatroom Get(chatroomName);
}

It’s used in the application like this:

var room = ChatManager.Get(chatroomName);
romm.DoStuff();
room.Logger.LogPacket(receivedPacket);

The ChatManager is a class which holds references to all chatrooms and is responsible for creating and removing them. I haven’t written it yet but that’s the interface I’ve been coding against.

The Question

How do I get ChatManager to create new instances of Chatroom and still use dependency injection??

I’m using Unity to do all my other DI stuff. So far it’s worked great. But I’m not sure how to work around this conundrum.

When my concrete implementation of ChatManager creates new chatrooms, it has to pass in an IChatroomLogger. It doesn’t know how to construct that…but Unity does. But then I have to pass in IUnityContainer into the ChatManager.

public class ChatManager : IChatManager
{
    public ChatManager(IUnityContainer container)
    {
        this.container = container;
    }

    public Chat Get(string chatroomName)
    {
        // get logger from Unity somehow. Not sure how I'd 
        // pass chatroomName to the concrete instance
        var logger = ...
        return new Chatroom(chatroomName, logger);
    }
}

That just seems wrong for some reason. It seems cleaner to not have the domain know anything about what DI container I’m using.

How could I get new instances of class Chatroom while my application in the middle of running without resorting to some sort of service locator design? Am I overthinking it? Is it not a big deal to pass around Unity? Any thoughts are welcome!

  • 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-24T10:17:13+00:00Added an answer on May 24, 2026 at 10:17 am

    You’re right. Using the IUnityContainer like this is no longer using Dependency Injection. Instead it’s using the “Service Locator” pattern. What I usually do in cases like this is create an IFactory<T> interface, like this:

    public IFactory<T>
    {
        T Get();
    }
    

    Then implement the interface with a class that does know about and use the IUnityContainer. Set up your bindings so that IFactory<> requests will create an instance of this factory class. That way, you can inject the IFactory<Logger> interface into your ChatManager, and call .Get() any time you want a logger instance.

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

Sidebar

Related Questions

Quick question... I have a query that checks for duplicates that looks like this:
Quick question. I have an app that use a native DLL through PInvoke, this
I had a quick question regarding RegEx... I have a string that looks something
I have a question that I'm sure it's very quick to answer, but I
So the quick background is that I have a page listing a set of
quick and very basic newbie question. If i have list of dictionaries looking like
Quick background: I'm programming in PHP, I have a domain model with a separate
Quick one. I'm using mod rewrite and have most replacements in place: empty space
Quick version: How do I get an image that was generated on the users
I'm having trouble swapping Short Description (Quick Overview) with Additional Information (Attributes) Here's an

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.