0

I am trying to make an invoice managing application.

When creating DAL object, I have to repeat Add and Delete functions for every table.

For example:

public class MyDBProcessor :IMyDBProcessor {
    tpEntities tp=new tpEntities();
    public void AddCustomerToDB(tblCustomers Customer) { tp.tblCustomers.Add(Customer); tp.SaveChanges();}
    public void AddOrderToDB(tblOrders Order) { tp.tblOrders.Add(Order); tp.SaveChanges(); }

    public tblCustomers GetCustomerFromDB(string CustomerID) { return tp.tblCustomers.Where(x => x.ID == CustomerID);   }
    public tblOrders GetOrderFromDB(string OrderID) { return tp.tblOrders.Where(x => x.ID == OrderID); }
}

It is very frustrating because there are so many tables. I want to change DAL interface like this:

public interface IMyGenericDBProcessor {
    void AddToDB<T>(T Record);
    T GetFromDB<T>(int RecordID);
}

So, when I change db engine MySQL to SQL Server or vice versa. How can I write a class that implemets IMyGenericDBProcessor?

Thank you,

6
  • If you don't have a real requirement to support multiple databases it's really overkill to use an abstraction just to support multiple databases. In a real world app you would probably just translate the contents of each DAL class to use the other database engine instead. If you on the other hand want to use abstractions to reduce complexity, then ask a new question since the approach is different than for the current question. As for the current question, repository pattern is one way to go: blog.gauffin.org/2013/01/repository-pattern-done-right Commented May 26, 2014 at 14:34
  • @jgauffin you say that I would need a complete new DAL object? Commented May 26, 2014 at 14:40
  • 1
    I would not put all DB access code into a single class. That would be painful to maintain as the application grows. Commented May 26, 2014 at 14:41
  • 1
    You're using Entity Framework? In my mind that already is a DAL. You might need Data Transfer Objects in order to abstract away information, or lay it out in a different schema, but if you're wiring a custom model <=> database / DAL layer you should not use EF (because why should you?) Commented May 26, 2014 at 14:42
  • 1
    That's fine, but if your checks are specific, won't it be hard to write those in the generic DBProc? I mean, if it is generic, it is - right? Anyways, good luck :) Commented May 26, 2014 at 14:52

1 Answer 1

1

Adding is easy:

public interface IMyGenericDBProcessor
{
    void AddToDB<T>(T Record) where T : class;
}

public class MyDBProcessor : IMyGenericDBProcessor 
{
    public void AddToDB<T>(T record) where T : class
    {
        using(var tp = new tpEntities())
            tp.Set<T>().Add(record);
    }
}

GetFromDb would be more complicated because you'd have to determine, based on the entity context's metadata, which property comprises the ID, and then construct an expression tree based on that.

Sign up to request clarification or add additional context in comments.

2 Comments

that works perfect! for selecting records, can we use something like this: return tp.Database.SqlQuery(T,"select * from " + Fix(TableName) + " where ID='" + Fix(RecordID) + "'"); I couldn't figure out. (We give table name as a string and every table's primary key is ID)
@mustafaöztürk: Yes, if you're consistent with your conventions you can take advantage of that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.