HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Dependency on overridden method

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
overriddendependencymethod

Problem

In my code I have a base type which is OnlinePaymentTransaction:

public abstract class OnlinePaymentTransaction
{
     public abstract void Complete( PaymentGatewayCallbackArgs args );
}


The problem I am having is that each class that inherits from this base class require different dependency's in the complete method. Currently I have just added the dependency's as extra parameters to the complete method which doesn't seem right. For example my base class is now like this.

public abstract class OnlinePaymentTransaction
{
     public abstract void Complete( Dependency1 dep1, Dependency2 dep2, PaymentGatewayCallbackArgs args );
}


I cannot inject the dependency's in the constructor as the OnlinePaymentTransactions are retrieved from using nhibernate.

What would you recommend because I don't like using ServiceLocator as it hides the dependency and also makes it harder to test. An suggestions would be greatly appreciated.

Solution

I cannot inject the dependency's in the constructor as the OnlinePaymentTransactions are retrieved from using nhibernate.

There's your problem. Your OnlinePaymentTransaction class is/should-be a POCO whose job is to convey data. The Complete() method doesn't belong on that type, it's breaking SRP and making your life much harder than it needs to be.

I'd suggest to introduce another type, call it OnlinePaymentTransactionProcessor or whatever - that type will take the dependencies in its constructor, and have a Complete method that takes an OnlinePaymentTransaction instance.

Kudos for striving to avoid a Service Locator :)

Context

StackExchange Code Review Q#42851, answer score: 5

Revisions (0)

No revisions yet.