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

  • Home
  • SEARCH
  • 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 7918159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:24:44+00:00 2026-06-03T15:24:44+00:00

I have an application (Legacy code) that contains interface Icomponent with save() methods and

  • 0

I have an application (Legacy code)

that contains interface Icomponent with save() methods

and many classes that implement it.

I want to add log after every save().

updae:

I had few design ideas:

  • Singelton Logger – that will be called after every logged-able action

I think LogService is a classic case of singelton, but I read it would be hard for unit-testing.

  • Decorator + logger init with Ioc

use the decorator pattern to Icomponent with additional log() method.

create LogService class which is called from each decorator’s log()

  • AOP – aspect orient programming

I read about this a bit, but don’t know it.

Is it applicable to c# ?

  • What would be your design to that solution?

update2

After reading the code, I see there is another layer of interfaces

before the concrete layer. I don’t think I should decorate every specific interface. right?

I_AComponente : IComponente

I_BComponente : IComponente

A : I_AComponente

B : I_BComponente
  • 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-03T15:24:45+00:00Added an answer on June 3, 2026 at 3:24 pm

    It doesn’t have to be singleton at all. Consider:

    public void ComponentDecorator : IComponent
    {
        private IComponent component;
        private ILogger logger;
    
        public ComponentDecorator(IComponent component, ILogger logger)
        {
            this.component = component;
            this.logger = logger;
        }
    
        public void Save()
        {
            this.component.Save();
            this.logger.Log("Some important message");
        }
    }
    

    Since you’ll be injecting decorated components to decorator anyways, might just as well inject logger (which then is rather easy to test). Where logger comes from will be irrelevant from unit test perspective.

    Also, instead of rolling your own loggers you should check existing ones, like Apache log4net.

    Edit:

    The claim that singleton logger is hard to test might come from that fact that singleton in C# will usually be implemented with static field somewhere, or static class. Say you had:

    public static LoggerService
    {
        public static Log(string message) { ... }
    }
    

    Now, in your decorators’ methods you’ll probably have to do something along those lines:

    public void Save()
    {
        this.component.Save();
        LoggerService.Log("Some important message");
    }
    

    This makes testing of the Save method nearly impossibly, as you have tightly coupled dependency inside your method. It’s not that bad if you can configure LoggerService for tests not to write to file system (but still, it’s rather going around the problem instead of solving it).

    In the inject via interface that problem is gone naturally. LoggerService might still be static class, but it could simply provide non-static loggers (and manage their lifetime/scope). This doesn’t make unit testing difficult at all.

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

Sidebar

Related Questions

We have an application with legacy code that relies on prototype, but we've found
In one of my programs, I have to interface with some legacy code that
Very strange question but I have some legacy code that interacts with another application
I have some legacy code that needs to be used in a new application.
I have a legacy VB6 application which contains this code: Begin VB.Label LblStDate Alignment
I want to start using guice in an existing application that has legacy code.
I have some legacy code that was used to monitor my applications cpu,memory etc
I have a legacy application that I inherited that passes a lot of XML
We have an legacy application that accesses the registry. Because it is a 32bit
I have some data (produced by a legacy application) that I know is invalid

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.