Maybe I’m going about this all wrong.
I have a bunch of classes that derive from the “Model” class, a base class with a bunch of common properties and methods. I want them all to implement a set of functionality:
public abstract void Create();
public abstract T Read<T>(Guid ID); //<--Focus on this one
public abstract void Update();
public abstract void Delete();
Then I implement it in a child class like “Appointment” like so:
public override T Read<T>(Guid ID)
{
var appt = db.Appointments.First(a => a.AppointmentID.Equals(ID));
var appointment = new Appointment()
{
DateEnd = appt.dateEnd.GetValueOrDefault(),
Location = appt.location,
Summary = appt.summary
};
return appointment;
}
This throws an exception “Can’t implicitly convert type ‘Appointment’ to T”. If I change the method’s signature to “public override Appointment Read(Guid ID)”, then the compiler says that I’ve not implemented the abstract method in the child class.
What am I missing? Can anyone give me some code samples?
It looks like you could use a generic base class! Consider something like the following:
Now your subclasses are all strongly typed. Of course, the tradeoff is that you no longer have a single base class. A
Model<Appointment>isn’t the same thing as aModel<Customer>. I have not generally found this to be a problem, though, because there’s little common functionality– the interfaces are similar, but they all work with different types.If you’d like a common base, you can certainly cheat and implement an
object-based interface that does the same general tasks. E.g., something in the spirit of (untested, but the idea’s there):