Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Generics - How do I return a specific type?

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?

like image 291
Rap Avatar asked Feb 11 '10 18:02

Rap


2 Answers

It looks like you could use a generic base class! Consider something like the following:

class Model<T>
{
    public abstract T Read(Guid ID);
}

class Appointment : Model<Appointment>
{
    public override Appointment Read(Guid ID) { }
}

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 a Model<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):

interface IModelEntity
{
    object Read(Guid ID);
}

class Model<T> : IModelEntity
{
    public T Read(Guid ID)
    {
        return this.OnRead(ID); // Call the abstract read implementation
    }

    object IModelEntity.Read(Guid ID)
    {
        return this.OnRead(ID); // Call the abstract read implementation
    }

    protected abstract virtual T OnRead(Guid ID);
}

class Appointment : Model<Appointment>
{
    protected override Appointment OnRead(Guid ID) { /* Do Read Stuff */ }
}
like image 70
Greg D Avatar answered Oct 08 '22 12:10

Greg D


Would this work?

public abstract T Read<T>(Guid ID) where T : IAppointment;
like image 25
Cory Charlton Avatar answered Oct 08 '22 11:10

Cory Charlton