I’m having trouble getting my head around interfaces. After trawling through similar questions on here, I’ve come up with the following interface, for defining the CRUD operations required for all my classes:
public interface IData<T>
{
IData<T> Select(int id);
List<T> SelectMultiple();
void Insert();
void Update();
void Delete();
}
This is then implemented within my partial classes:
public partial class Post : IData<Post>
{
public IData<Post> Select(int id)
{
MyDataContext dc = MyDataContext.Create();
return dc.Posts.Single(p => p.PostID == id);
}
public List<Post> SelectMultiple()
{
MyDataContext dc = MyDataContext.Create();
return dc.Posts.ToList();
}
// Update() and Delete() declarations
}
This all compiles fine, however if I try to use the Post Select() method:
Post p = new Post().Select(1);
It fails with Cannot implicitly convert type ‘IData’ to ‘Post’. An explicit conversion exists (are you missing a cast?)
Which makes sense, but how do I have it so that it doesn’t require a Cast? I want the Select to return a Post (but not define Post as the return type at the interface level). Have I misunderstood interfaces, or is there a quick alteration I can make?
You want to return something of type
T, notIData<T>, so just change the signature (at least I guess this is what you want, as you’d returnList<IData<T>>otherwise):and implement it appropiately:
If you just want this behaviour in the Post class, explicitly implement the
IData<T>interface: