Question

I have one EF edmx(BaseEntities). As business requirement, I created two files called. QTDAL and AdminDAL.

Please see the following codes. My two classes (QTDAL, AdminDAL) inheritances to BaseEntities. (Note: I am not sure this is allowed to do that)

(No errors now) I would like to have suggestions if there is any better solution to achieve as the below code.

Any advise, please ?

public void TestMethod1()
    {
        IQTDAL qtContext = new QTDAL();

        var value = qtContext.AllCampaigns().FirstOrDefault();
        //var value = qtContext.AddOrUpdateCampaign(TestCampaign);

        Assert.IsNotNull(value);
    }

Examples:

    public partial class BaseEntities : DbContext
{
    public BaseEntities()
        : base("name=BaseEntities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public virtual DbSet<Campaign> Campaigns { get; set; }
    public virtual DbSet<User> Users { get; set; }

}

    public class QTDAL : BaseEntities, IQTDAL
{
    public IEnumerable<Campaign> AllCampaigns()
    {
        return Campaigns.ToList();
    }

    public int AddOrUpdateCampaign(Campaign campaign)
    {
        Campaigns.AddOrUpdate(campaign);
        return SaveChanges();
    }

    public int DeleteCampaign(Campaign campaign)
    {
        Campaigns.Remove(campaign);
        return SaveChanges();
    }
}


    public class AdminDAL : BaseEntities, IAdminDAL
{
    public IEnumerable<User> AllUsers()
    {
        return Users;
    }

    public int AddOrUpdateUser(User user)
    {
        Users.AddOrUpdate(user);
        return SaveChanges();
    }

    public int DeleteUser(User user)
    {
        Users.Remove(user);
        return SaveChanges();
    }
}
Was it helpful?

Solution

Well, you certainly CAN inherit from your context, but the real question is why would you?

Inheritance is a concept that is used to create an "is a" relationship. In other words, your QTDAL "is a" BaseEntities. So you can treat a QTDAL as a BaseEntities object. You do not appear to be using it that way. Instead, you seem to be wanting to wrap the BaseEntities functionality.

In which case, you should simply be using BaseEntities as a member of your QTDAL class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top