Abstract DAL – Using Interface with Internal Class in C#

cdesignobject-oriented

We have a business logic layer (BLL) that is tightly coupled to our data access layer (DAL). We make calls like this:

using (FooData data = new FooData())
{
  data.DoSomething();
}

It's important to note that all of our data classes are internal, and they're in the same assembly as the logic classes, so that only the BLL can access the DAL.

In order to decouple these (to help facilitate unit testing), one idea is to create IDataClass interfaces, like IFooData. At first I thought it may be a problem because we'd have to make our data classes public to implement the interfaces. But we should be able to keep the data classes internal, even though we still need the methods to be public:

public interface IFooData
{
  void DoSomething();
}

internal class FooData : IFooData  // Notice the class is internal
{
  public void DoSomething();  // Method is public, but that's ok because class is internal
}

So even though the method is public, since the class itself is internal, we should still allow only our BLL access.

Is there anything inherently wrong with this approach? Is there a better way to abstract the DAL, for unit testing, without opening the DAL to the world by making it public?

Best Answer

Extracting an interface and ensuring the BLL is using only interface types is a good way forward to make the BLL testable without the database.

What you are doing is absolutely fine.