So i think i maybe over-complicating things in my implementation, but here’s what i’m doing:
- I want my database represented by IRepository
- SQLRepository is a concrete implementation of IRepository.
- I want all Tables in the Database represented by ITable.
- I have a table called Items, which is represented by ItemsTable.
- Each record in Items Table is represented by IItem.
- Item is a concrete implementation of IItem.
Now my problem is, in my program when i want to use List items = repository.Items.List(); it won’t compile because in ItemsTable the implementation of ITable return IList and not List. I could return IList but i do want to work with a concrete List of Items.
What can i do better?
public interface ITable
{
IList<IItem> List();
bool Add(IItem item);
bool Delete(long itemId);
bool Find(long itemId);
}
public class ItemsTable : ITable
{
public IList<IItem> List()
{
IList<IItem> items = GetItems(); // GetItems return List<Item>, Item implements IItem.
return items;
}
.....
...
..
}
public class SQLRepository : IRepository
{
ITable items = new ItemsTable();
public ITable Items
{
get { return items; }
}
}
static void Main(string[] args)
{
var repository = new SQLRepository();
List<Item> items = repository.Items.List();
}
The best would be changing your interface to: