简体   繁体   中英

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.

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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