1
\$\begingroup\$

I have following model and database created using Entity Framework. Is it proper TPT Inheritance?

Is it possible to make the base class as abstract?

Model

enter image description here

Database

enter image description here

CODE

namespace LijosEF
{
public partial class Book 
{
    public override void Sell()
    {
        this.AvailabilityStatus = "BookSOLD";
    }

}

public partial class DigitalDisc
{
    public override void Sell()
    {
        this.AvailabilityStatus = "DiscSOLD";
    }

}

public partial class SellingItem
{
    public virtual void Sell()
    {
        //Do nothing
    }
}
}

Client

namespace LijosEF
{
class Program
{

    static string connectionStringVal;

    static void Main(string[] args)
    {

        SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
        sqlBuilder.DataSource = ".";
        sqlBuilder.InitialCatalog = "LibraryReservationSystem";
        sqlBuilder.IntegratedSecurity = true;

        // Initialize the EntityConnectionStringBuilder.
        EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
        entityBuilder.Provider = "System.Data.SqlClient";
        entityBuilder.ProviderConnectionString = sqlBuilder.ToString();
        entityBuilder.Metadata = @"res://*/MyModelFirstTest.csdl|res://*/MyModelFirstTest.ssdl|res://*/MyModelFirstTest.msl";

        connectionStringVal = entityBuilder.ToString();


        //AddBook();
        //AddDisc();

        //MakePurchase();

        int billAmount = GetTotalPurchaseValue(1);

    }


    private static void AddBook()
    {
        using (var db = new MyModelFirstTestContainer(connectionStringVal))
        {
            Book book = new Book();
            book.AvailabilityStatus = "Available";
            book.Price = 150;
            book.Title = "Maths Easy";

            db.SellingItems.AddObject(book);
            db.SaveChanges();

        }

    }

    private static void MakePurchase()
    {

        using (var db = new MyModelFirstTestContainer(connectionStringVal))
        {
            DigitalDisc disc = db.SellingItems.OfType<DigitalDisc>().FirstOrDefault(p => p.Artist == "Turtle Violin");
            disc.Sell(); //Changes State

            Book book = db.SellingItems.OfType<Book>().FirstOrDefault(p => p.Title == "Maths Easy");
            book.Sell(); //Changes State

            Purchase purchase = new Purchase();
            purchase.Date = DateTime.Now;
            purchase.SellingItems.Add(book);
            purchase.SellingItems.Add(disc);

            db.Purchases.AddObject(purchase);
            db.SaveChanges();

        }

    }

    private static int GetTotalPurchaseValue(int purchaseID)
    {
        using (var db = new MyModelFirstTestContainer(connectionStringVal))
        {

            var purchase = db.Purchases.FirstOrDefault(p => p.PurchaseId == purchaseID);
            if( purchase == null)
            {
                return 0;
            }

            var total = purchase.SellingItems.Sum(x => Convert.ToInt32(x.Price));
            return total;

        }

    }



    private static void AddDisc()
    {


        using (var db = new MyModelFirstTestContainer(connectionStringVal))
        {
            DigitalDisc disc = new DigitalDisc();
            disc.AvailabilityStatus = "Available";
            disc.Price = 300;
            disc.Artist = "Turtle Violin";

            db.SellingItems.AddObject(disc);
            db.SaveChanges();

        }

    }

}
}

REFERENCE:

  1. https://stackoverflow.com/questions/4254182/inheritance-vs-enum-properties-in-the-domain-model/4259033#comment15499738_4259033

  2. https://stackoverflow.com/questions/11683536/entity-framework-get-subclass-objects-in-repository#comment15499860_11683536

  3. https://softwareengineering.stackexchange.com/questions/158472/why-should-i-add-check-constraint

\$\endgroup\$
2
\$\begingroup\$

Yes, it is a proper TPT inheritance and yes, you can mark your base class as abstract.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.