patterncsharpModerate
Getting list items
Viewed 0 times
listitemsgetting
Problem
Is there any possibility of refactoring this code?
In class A:
In class B:
I tried to use template method, but due to other params in
In class A:
public List GetItems() {
var result = new List();
foreach(var item in repo.GetItems1()) {
var x = repo.GetOtherItems1(item.Id, "param1", param2); // this part is different
if (x.Value > 5)
result.Add(x);
}
return result;
}In class B:
public List GetItems() {
var result = new List();
foreach(var item in repo.GetItems2()) {
var x = repo.GetOtherItems2(param1, param2, item.Id); // this part is different
if (x.Value > 5)
result.Add(x);
}
return result;
}I tried to use template method, but due to other params in
GetOtherItemsX(...), is it now possible?Solution
Well, you could receive a delegate for getting the elements - that is, to make the action that is different:
And then you could call it like so:
But I notice that you also use
So the difference you pointed out is not the only one.
With this in mind, I would advise rethinking your class structure. These two could inherit from the same base class, and merely extend it. (Or it could be polymorphism, or...)
Maybe:
Or maybe an abstract class?
edit: read @JoeGeeky 's comment on the performance impact of using delegates; this may become relevant if the delegate is used on a very intensive cycle or under high loads.
public List GetItems(Func getOtherItems) {
if( getOtherItems == null ) {
throw new ArgumentException();
}
var result = new List();
foreach(var item in repo.GetItems2()) {
var x = getOtherItems();
if (x.Value > 5)
result.Add(x);
}
return result;
}And then you could call it like so:
// In class A
this.GetItems( repo => repo.GetOtherItems1(item.Id, "param1", param2) );
// In class B
this.GetItems( repo => repo.GetOtherItems2(param1, param2, item.Id) );But I notice that you also use
repo.GetItems1() in A and repo.GetItems2() in B.So the difference you pointed out is not the only one.
With this in mind, I would advise rethinking your class structure. These two could inherit from the same base class, and merely extend it. (Or it could be polymorphism, or...)
Maybe:
public interface IMyList
{
List GetItems();
// If it returns a single item, the name should NOT be pluralized!!
Item GetOtherItems();
}Or maybe an abstract class?
edit: read @JoeGeeky 's comment on the performance impact of using delegates; this may become relevant if the delegate is used on a very intensive cycle or under high loads.
Code Snippets
public List<Item> GetItems(Func<Item, Repository> getOtherItems) {
if( getOtherItems == null ) {
throw new ArgumentException();
}
var result = new List<Item>();
foreach(var item in repo.GetItems2()) {
var x = getOtherItems();
if (x.Value > 5)
result.Add(x);
}
return result;
}// In class A
this.GetItems( repo => repo.GetOtherItems1(item.Id, "param1", param2) );
// In class B
this.GetItems( repo => repo.GetOtherItems2(param1, param2, item.Id) );public interface IMyList<Item>
{
List<int> GetItems();
// If it returns a single item, the name should NOT be pluralized!!
Item GetOtherItems();
}Context
StackExchange Code Review Q#6752, answer score: 11
Revisions (0)
No revisions yet.