There is an interface
public interface ISomeInterface
{
Task<int> SomeMethodAsync();
Task<int> OtherMethodAsync();
}
and a class that implements it
public class SomeClass : ISomeInterface
{
public async Task<int> SomeMethodAsync() {... return x; }
public async Task<int> OtherMethodAsync() {... return y; }
}
I want to decorate the class to add some feature (e.g. logging) to some of the methods of it
public class LogDecoratedSomeClass : ISomeInterface
{
private readonly ISomeInterface _decoratee;
private readonly ILogger _logger;
public LogDecoratedSomeClass(ISomeInterface decoratee, ILogger logger)
{
if (decoratee == null) throw new ArgumentNullException("decoratee");
if (logger == null) throw throw new ArgumentNullException("logger");
_decoratee = dedcoratee;
_logger = logger;
}
public async Task<int> SomeMethodAsync()
{
_logger.log( <some message> );
var result = await _decoratee.SomeMethodAsync();
_logger.log( <even more messages> );
return result;
}
public async Task<int> OtherMethodAsync()
{
return await _decoratee.OtherMethodAsync();
}
}
the last method may be written as
public Task<int> OtherMethodAsync()
{
return _decoratee.OtherMethodAsync();
}
which one of these two ways is better than another?
thanks
UPDATE:
what about webapi method?
public class SomeController : ApiController
{
private readonly ISomeInterface _service;
<constructor ...>
[HttpGet]
public async Task<int> Get()
{
return await _service.SomeMethodAsync();
}
}