1

我很想从 SQL 服务器加载对象(出于性能原因,我回避了 orm 框架)

我的想法是有一个工厂类从服务器检索记录并创建适当类型的对象。

所有对象都将继承自一个抽象类:aDefinition。尽管从 aDefintion 派生的具体对象都具有一些相似的属性,但它们在基类之上还具有自己的独特属性。

由于我对编程还很陌生,所以我只是想了解一下我是否走在正确的轨道上。

我有几个问题:

  • 在这种情况下使用抽象类是否有意义?

  • 将定义集合存储在 DefinitionCreator 类中List<aDefinition>并为其提供访问对象的方法是一个好主意,还是为不同的对象类型创建不同的具体集合是一个更好的主意?

public class DefinitionTemplate
{
    public int DefinitionId { get; set; }
    public string Name { get; set; }
    public DefintionType DefinitionType { get; set; }
    public string Definition { get; set; }
    public string Parameters { get; set; }

    public DefinitionTemplate()
    {
    }
}

//abstract base class that my concrete classes will inherit from.
public abstract class aDefinition
{
    public int DefinitionId { get; protected set; }
    public string Name { get; protected set; }
    public DefintionType DefinitionType { get; protected set; }

    //abstract methods
    public abstract bool LoadDefinition(DefinitionTemplate template);
    public abstract bool SaveDefinition();
}

//concrete class representing a report-type definition.
public class ReportDefinition : aDefinition
{
    public string Definition { get; private set; }
    public string Parameters { get; private set; }

    public override bool LoadDefinition(DefinitionTemplate template)
    {
        this.DefinitionId = template.DefinitionId;
        this.Name = template.Name;
        this.DefinitionType = template.DefinitionType;
        this.Definition = template.Definition;
        this.Parameters = template.Parameters;
        return true;
    }

    public override bool SaveDefinition()
    {
        throw new NotImplementedException();
    }
}

//concrete class representing a configuration-type definition.
public class ConfigDefinition : aDefinition
{
    public string Parameters { get; private set; }

    public override bool LoadDefinition(DefinitionTemplate template)
    {
        this.DefinitionId = template.DefinitionId;
        this.Name = template.Name;
        this.DefinitionType = template.DefinitionType;
        this.Parameters = template.Parameters;
        return true;
    }

    public override bool SaveDefinition()
    {
        throw new NotImplementedException();
    }
}

//creator class that retreives the definitions from the server and creates the approprite object
public static class DefinitionCreator
{
    public void RetreiveDefinitions()
    {
        //sql statment to retreive definitions
        using(SqlCommand cmd = new SqlCommand("select * from definitions", new SqlConnection()))
        {
            using(SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    switch (reader["DefinitionType"].ToString())
                    {
                        case "ReportDefinition":
                            //create a report definition object
                            break;
                        case "ConfigDefinition":
                            //create a config definition object
                            break;
                    }
                }
            }
        } 
    }
}
4

0 回答 0