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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:05:29+00:00 2026-05-25T00:05:29+00:00

This class injects all dependencies in the constructor, but only one of the dependencies

  • 0

This class injects all dependencies in the constructor, but only one of the dependencies are used at a time. Is this considered bad design?

public class OrderPayment
{
    ICreditCardPayment _ccPayment;
    ICashPayment _cashPayment;

    public OrderPayment(ICreditCardPayment ccPayment, ICashPayment cashPayment)
    {
        _ccPayment = ccPayment;
        _cashPayment = cashPayment;
    }

    private void PrepareOrder(Order order)
    {
        // Do stuff with the order
    }

    public PaymentResult PayByCreditCard(Order order)
    {
        PrepareOrder(order);
        return _ccPayment.Pay(order);
    }

    public PaymentResult PayByCreditCard(Order order)
    {
        PrepareOrder(order);
        return _cashPayment.Pay(order);
    }   
}

An alternative is this:

public class OrderPayment
{
    private void PrepareOrder(Order order)
    {
        // Do stuff with the order
    }

    public PaymentResult PayByCreditCard(Order order, ICreditCardPayment ccPayment)
    {
        PrepareOrder(order);
        return ccPayment.Pay(order);
    }

    public PaymentResult PayByCreditCard(Order order, ICashPayment cashPayment)
    {
        PrepareOrder(order);
        return cashPayment.Pay(order);
    }   
}

This one complicates the function call somewhat. Would you use the first, cleaner looking one, even though not every constructor parameter is used? Considering a DI framework has to instantiate potentially heavy classes even though they may not all be used, I’m not sure how good this is.

So which one would you use? Or maybe a different implementation?

  • 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-25T00:05:30+00:00Added an answer on May 25, 2026 at 12:05 am

    I would refactor or extract a common interface from ICashPayment and ICreditCardPayment. Your code sample indicates your methods are both invoking xPayment.Pay, which looks like a good candidate for your common interface method.

    public interface IPayment 
    {
        PaymentResult Pay(Order order);
    }
    

    Your more specialized interfaces can inherit from and build upon it.

    In general, I would avoid having constructors (or any method) accept arguments that go unused, or if one argument is used, the other is not. That’s normally an indication that you are either not operating at the proper level of abstraction, or that your class/method has too many responsibilities.

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

Sidebar

Related Questions

In my design all of my daos inherit from a parent class, this parent
Given this class public partial class Default : Page { private IRepository repo; ...
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
Take this class as example: public class Category : PersistentObject<int> { public virtual string
Consider this class: class test { public function __set($n, $v) { echo __set() called\n;
in asp.net-mvc I have the Windsor Controller Factory that injects all the dependencies into
I see this: public class MyGenericClass<T> where T:IComparable { } and I have this:
Let's make the statement All Rocks have Minerals. : public class Mineral { //
Given this class class Foo { // Want to find _bar with reflection [SomeAttribute]
Consider this class hierarchy: Book extends Goods Book implements Taxable As we know, there

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.