简体   繁体   English

将子对象的属性插入数据库的正确方法?

[英]the correct way of inserting into a database the attributes of child objects?

I have an abstract parent class(Product) and two child classes that inherit from it(Book,Software). 我有一个抽象的父类(产品)和两个从其继承的子类(书,软件)。 I need to insert the common data(the parent class attributes) into the Products table and the specific attributes of the child classes into the BookProducts and SoftwareProducts tables. 我需要将公共数据(父类属性)插入Products表中,并将子类的特定属性插入BookProducts和SoftwareProducts表中。

this is the implementation i have come up with, i was wondering if anyone can provid a better approach. 这是我想出的实现方式,我想知道是否有人可以提供更好的方法。

public static class Database
{


    public static void SaveBook(Book book)
    {
        //save the product to products table and return the ID of the new row
        int ProductID = saveProduct(book);

       SqlConnection connection = DatabaseConnection.GetConnection();


            string insertBookStatement =
            "INSERT BookProducts" +
            "(ProductID,Author)" +
            "VALUES (@ProductID,@Author)";

        SqlCommand insertBookCommand = new SqlCommand(insertBookStatement, connection);

        insertBookCommand.Parameters.AddWithValue("@ProductID",ProductID);
        insertBookCommand.Parameters.AddWithValue("@Author",book.Author );


        try
        {
            connection.Open();
            insertBookCommand.ExecuteNonQuery();

        }
        catch (SqlException ex)
        {
            throw ex;
        }
        finally
        {
            connection.Close();
        }



    }

    public static void SaveSoftware(Software software)

    {
        int ProductID = saveProduct(software);

        SqlConnection connection = DatabaseConnection.GetConnection();


        string insertSoftwareStatement =
        "INSERT SoftwareProducts" +
        "(ProductID,VersionNumber)" +
        "VALUES (@ProductID,@VersionNumber)";

        SqlCommand insertSoftwareCommand = new SqlCommand(insertSoftwareStatement, connection);

        insertSoftwareCommand.Parameters.AddWithValue("@ProductID", ProductID);
        insertSoftwareCommand.Parameters.AddWithValue("@VersionNumber", software.VersionNumber);


        try
        {
            connection.Open();
            insertSoftwareCommand.ExecuteNonQuery();

        }
        catch (SqlException ex)
        {
            throw ex;
        }
        finally
        {
            connection.Close();
        }
    }


    private static int saveProduct(Product p)
    {
        int ProductID;
        SqlConnection connection = DatabaseConnection.GetConnection();

        string insertProductStatement =
            "INSERT Products" +
            "(Code,Description,Category,Price)" +
            "VALUES (@code,@Description,@Category,@Price)";

        SqlCommand insertProductCommand = new SqlCommand(insertProductStatement, connection);
        insertProductCommand.Parameters.AddWithValue("@Code", p.Code);
        insertProductCommand.Parameters.AddWithValue("@Description", p.ShortDescription);
        insertProductCommand.Parameters.AddWithValue("@Category", p.Category);
        insertProductCommand.Parameters.AddWithValue("@Price", p.Price);

        try
        {
            connection.Open();
            insertProductCommand.ExecuteNonQuery();
            string selectStatement = "SELECT IDENT_CURRENT('Products') FROM Products";
            SqlCommand selectCommand = new SqlCommand(selectStatement, connection);
            ProductID = Convert.ToInt16(selectCommand.ExecuteScalar());



        }
        catch (SqlException ex)
        {
            throw ex;
        }
        finally
        {
            connection.Close();
        }


        return ProductID;
    }


}

The Data Access Object pattern will help you. 数据访问对象模式将为您提供帮助。

First of all, you'll want one BaseProduct table, one Software table and one Book table. 首先,您需要一个BaseProduct表,一个Software表和一个Book表。 The SoftwareProduct DAO will call base.Store() (this is pretty pseudo-cody, but it'll get the idea across) to store all the base-class' fields in BaseProduct, while then storing itself to the correct table with a BaseProductId as foreign key. SoftwareProduct DAO将调用base.Store()(这是相当伪的伪造,但可以理解),将所有基本类的字段存储在BaseProduct中,然后使用BaseProductId将其自身存储到正确的表中作为外键。

Database normalization is usually good, and this particular case could probably be used as a text-book example of why, as it shows the connection between OCP/SRP in the domain layer and the normalized tables in the database :) 数据库规范化通常是好的,并且这种特殊情况可能可以用作其原因的教科书示例,因为它显示了域层中的OCP / SRP与数据库中的规范化表之间的联系:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM