纳入泛型

时间:2009-08-05 10:24:41

标签: c# .net generics

我正在使用C#.net进行开发,我相信我需要使用泛型来解决我遇到的问题,但我不知道从哪里开始。

以下是我需要做的步骤:

  1. 从用户处获取ID - DONE
  2. 使用ID - DONE
  3. 查询数据库表
  4. 拉回一行 - 完成
  5. 创建依赖于ID的对象类型
  6. 根据类型填充对象
  7. 不同的约会需要从数据库中拉回(填充)不同的信息。例如,一个约会可能只需要用户forename / surname,其他人可能需要forename / surname / dob。

    我希望首先执行子类,然后调用父方法。问题是myAppointment在通过Switch语句后仍然被视为Appointment对象而不是DoctorAppointment / OtherAppointment。在声明myAppointment的地方,我实际上不知道它将是什么类型的对象

    以下是我目前的代码:

            tblAppointment getAppointment = (from getApp in dc.tblAppointments
                                             where getApp.appID == Id
                                             select getApp).SingleOrDefault();
    
    // Needs to be set the something. I really want this be to set to a generic type as I don’t really know what type it will be until it has been through the switch statement
    Appointment myAppointment = null; 
    
                switch (getAppointment.tblAppointment.appTypeID)
                {
                    case 2:
                        {
    // Changes myAppointment from Appointment to DoctorsAppointment
                            myAppointment = new DoctorsAppointment();
    
                        }
                        break;
                    case 1:
                        {
                            myAppointment = new OtherAppointment();
                        }
                        break;
                    default:
    
                        break;
    
                }
    
    // I want this to set myAppointment to DoctorsAppointment.Populate(getAppointment)
                return myAppointment.Populate(getAppointment);
    

    约会是父类。 DoctorsAppointment / OtherAppointment是儿童班。

    子类中的Populate需要一个参数,而父类则不需要。我目前收到的错误是:

    No overload for method 'Populate' takes '1' arguments
    

    希望我已经足够解释了自己。

    谢谢,

    克莱尔

6 个答案:

答案 0 :(得分:4)

一种解决方案是在父类Appointment中创建这样的方法:

public virtual void Populate( getAppointment) 
{
   // does nothing
}

然后在儿童班:

public override void Populate( getAppointment) 
{
// do what you need
}

答案 1 :(得分:2)

听起来像是继承的案例

abstract class Appointment
{
    public abstract void Populate(AppointmentData data);
}

class DoctorsAppointment : Appointment
{
    public override void Populate(AppointmentData data)
    {
        // implementation specific to DoctorsAppointment
    }
}
class OtherAppointment : Appointment
{
    public override void Populate(AppointmentData data)
    {
        // implementation specific to OtherAppointment
    }
}

答案 2 :(得分:2)

建议您采取上述继承答案之一

我认为你是否在使用LINQ to SQL是值得的?如果您有一个存储不同类型对象的表,则可以部分实现所需的功能。

请参阅here以获取示例

答案 3 :(得分:1)

Appointment类应该声明适当的Populate方法。您还可以设置约会工厂类:

static class AppointmentFactory
{
    public static Appointment Create(int type) 
    {
        switch (type)
        {
            case 2:
                return new DoctorsAppointment();
            case 1:
                return new OtherAppointment();
            default:
                throw new AppointmentTypeNotSupportedException();
        }
    }
}

class Appointment
{
    public virtual void Populate(getAppointment data) { }
}

class DoctorsAppointment : Appointment
{
    public override void Populate(getAppointment data) {  }
}

class OtherAppointment : Appointment
{
    public override void Populate(getAppointment data) {  }
}

答案 4 :(得分:0)

你应该将约会的Populate方法改为抽象(可能是虚拟的)并采用任何类型的getAppointment。子类DoctorsAppointment和OtherAppointment通过覆盖Appointment的默认值来实现Popuplate。

答案 5 :(得分:0)

abstract class Appointment 
{
    public abstract string Populate(AppointmentData d);
}

class OtherAppointment : Appointment
{
    public override string Populate(AppointmentData d)
    {

    }
}

使Appointment成为一个抽象类,它不能自己实例化,并使populate方法也是抽象的,所以它必须由它的继承类实现