views:

61

answers:

1

I have two interfaces, IAuditable and ITransaction.

public interface IAuditable{
    DateTime CreatedOn { get; }
    string CreatedBy { get; }
 }

public interface ITransaction : IAuditable {
    double Amount{ get; }
} 

And a class that implements ITransaction, call Transaction.

public class Transaction : ITransaction{
    public DateTime CreatedOn { get { return DateTime.Now; } }
    public string CreatedBy { get { return "aspnet"; } }
    public double Amount { get { return 0; } }
}

When I bind a list of ITransactions to a datagrid and use auto create columns, only the Amount gets bound. The CreatedBy and CreatedOn are not seen. Is there a way I can get these values visible during databinding?

+4  A: 

Interfaces do not define implementation - they only define required additional interfaces.

What this means is that any class that implements ITransaction is also required to implement IAuditable. It does not mean that ITransaction defines the properties of IAuditable as well.

Therefore, casting something to ITransaction does not define it to have IAuditable properties. It is simply casting to the contract that states "I can do all the methods in ITransaction". The fact that anything implementing ITransaction is also required to implement IAuditable does not mean that ITransaction is defined any differently.

Phil Haack has a great blog post on this.

If you want to have all the properties inherited, you should be using an "Is-A" relationship (i.e. abstract classes), rather than a "Can-do" relationship (interfaces).

womp