I have a few interfaces that are
public interface ISaleOrderManager
{
Task<IOrderDto> CreateAsync(ICreateOrderDto request);
}
public interface ICreateOrderDto
{
long? ContactId { get; set; }
DateTime OrderDate { get; set; }
double TotalAmount { get; set; }
}
public interface IOrderDto:ICreateOrderDto,IEntityDto
{ }
public interface IEntityDto
{
long Id {get;set;}
bool isDeleted {get; set;}
}
also, I have some DTOs as well that are
public class CreateSaleOrderDto: ICreateOrderDto
{
public long? ContactId { get; set; }
public DateTime OrderDate { get; set; }
public double TotalAmount { get; set; }
}
public class SaleOrderDto : IOrderDto
{
public long Id {get;set;}
public bool isDeleted {get; set;}
public long? ContactId { get; set; }
public DateTime OrderDate { get; set; }
public double TotalAmount { get; set; }
}
And I implement ISaleOrderManager on a class that is
public class SaleOrderManager: ISaleOrderManager
{
public SaleOrderManager(IRepository<SaleOrder, long> entityRepository)
{
}
public async Task<SaleOrderDto> CreateAsync(CreateSaleOrderDto request)
{
var entity = ObjectMapper<SaleOrder>(request);
entity.Id = await _entityRepository.InsertAndGetIdAsync(entity);
return ObjectMapper.Map<SaleOrderDto>(entity);
}
}
So as my code depicts that I have implemented ICreateOrderDto through CreateSaleOrderDto and IOrderDto through SaleOrderDto but I'm still getting an error of "Interface Member is not implemented."
Why do I receive this error and how can I overcome it?
CodePudding user response:
You didn't implement the Task<IOrderDto> CreateAsync(ICreateOrderDto request)
method in the SaleOrderManager
class. You made the CreateAsync
method return a Task<SaleOrderDto>
instead of a Task<IOrderDto>
. Also you used a parameter of type CreateSaleOrderDto
instead of ICreateOrderDto
.
CodePudding user response:
This is the declaration of the method
Task<IOrderDto> CreateAsync(ICreateOrderDto request);
and this is the attempt for the implementation
public async Task<SaleOrderDto> CreateAsync(CreateSaleOrderDto request)
{
var entity = ObjectMapper<SaleOrder>(request);
entity.Id = await _entityRepository.InsertAndGetIdAsync(entity);
return ObjectMapper.Map<SaleOrderDto>(entity);
}
I perfectly know that SaleOrderDto
implements IOrderDto
and that CreateSaleOrderDto
implements ICreateOrderDto
, but this does not qualify as an implementation of the method in the interface
.
In order to understand the problem, let's consider the possibility that some other class, that may be different from CreateSaleOrderDto
may also implement ICreateOrderDto
. If I declare a variable to be an ISaleOrderManager
and I instantiate this other class, let's call it MyCustomCreateSaleOrderDto
and pass it to CreateAsync
, then I justifiably expect your method to work, but it will not work, because your method mistakenly assumed the following:
- It assumed that each and every
iCreateOrderDto
is aCreateSaleOrderDto
- It assumed that each and every
IOrderDto
is aSaleOrderDto
You should aim to write your code in an as abstract manner as possible, so, you should aim to implement your method in a way that it expects a parameter specified as an interface and its return type should also be corresponding to the one specified by the interface, even though they are actual objects at runtime.