Reputation: 321
I have several methods in a class that has the same logic but I can't see how to avoid the duplication? Each method internally calls another service but otherwise the logic before and after the internal service call are the same, see the code snippet below.
Any help appreciated!
Many thanks,
code
public List<Customer> GetCustomerDetail(int id)
{
_log.debug("xxx");
if(something)
{
_log.debug("yyy");
}
var results = _internalService.GetCustomer(id);
if(results == null)
{
_log.debug("no results");
}
return results;
}
public List<Customer> GetCompanyDetail(int id)
{
_log.debug("xxx");
if(something)
{
_log.debug("yyy");
}
var results = _internalService.GetCompany(id);
if(results == null)
{
_log.debug("no results");
}
return results;
}
...
Upvotes: 0
Views: 136
Reputation: 301
Use delegates:
public List<Customer> GetCustomerDetail(int id)
{
return DoIt( () => _internalService.GetCustomer(id) );
}
public List<Customer> GetCompanyDetail(int id)
{
return DoIt( () => _internalService.GetCompany(id) );
}
private T DoIt<T>( Func<T> func )
{
_log.debug("xxx");
if(something)
{
_log.debug("yyy");
}
var results = func();
if(results == null)
{
_log.debug("no results");
}
return results;
}
Upvotes: 5
Reputation: 9780
Several options, depending on the duplication density:
Upvotes: 0