I am currently designing an n-tier solution which is using Entity Framework 5 (.net 4) as its data access strategy, but am concerned about how to incorporate dependency injection to make it testable / flexible.

My current solution layout is as follows (my solution is called Alcatraz):

Alcatraz.WebUI: An asp.net webform project, the front end user interface, references projects Alcatraz.Business and Alcatraz.Data.Models.

Alcatraz.Business: A class library project, contains the business logic, references projects Alcatraz.Data.Access, Alcatraz.Data.Models

Alcatraz.Data.Access: A class library project, houses AlcatrazModel.edmx and AlcatrazEntities DbContext, references projects Alcatraz.Data.Models.

Alcatraz.Data.Models: A class library project, contains POCOs for the Alcatraz model, no references.

My vision for how this solution would work is the web-ui would instantiate a repository within the business library, this repository would have a dependency (through the constructor) of a connection string (not an AlcatrazEntities instance). The web-ui would know the database connection strings, but not that it was an entity framework connection string.

In the Business project:

public class InmateRepository : IInmateRepository
    private string _connectionString;

    public InmateRepository(string connectionString)
        if (connectionString == null)
            throw new ArgumentNullException("connectionString");

        EntityConnectionStringBuilder connectionBuilder = new EntityConnectionStringBuilder();

        connectionBuilder.Metadata = "res://*/AlcatrazModel.csdl|res://*/AlcatrazModel.ssdl|res://*/AlcatrazModel.msl";
        connectionBuilder.Provider = "System.Data.SqlClient";
        connectionBuilder.ProviderConnectionString = connectionString;

        _connectionString = connectionBuilder.ToString();

    public IQueryable<Inmate> GetAllInmates()
        AlcatrazEntities ents = new AlcatrazEntities(_connectionString);

        return ents.Inmates;

In the Web UI:

IInmateRepository inmateRepo = new InmateRepository(@"data source=MATTHEW-PC\SQLEXPRESS;initial catalog=Alcatraz;integrated security=True;");

List<Inmate> deathRowInmates = inmateRepo.GetAllInmates().Where(i => i.OnDeathRow).ToList();

I have a few related questions about this design.

  1. Does this design even make sense in terms of Entity Frameworks capabilities? I heard that Entity framework uses the Unit-of-work pattern already, am I just adding another layer of abstract unnecessarily?

  2. I don't want my web-ui to directly communicate with Entity Framework (or even reference it for that matter), I want all database access to go through the business layer as in the future I will have multiple projects using the same business layer (web service, windows application, etc.) and I want to have it easy to maintain / update by having the business logic in one central area. Is this an appropriate way to achieve this?

  3. Should the Business layer even contain repositories, or should that be contained within the Access layer? If where they are is alright, is passing a connection string a good dependency to assume?

Thanks for taking the time to read!


The way you are doing DI is wrong.

First, the connection string belongs in the data layer. Or in the web.config file.

The next abstraction you will be dealing with is the DbContext, not a connection string. Your repositories should not know about connection strings. Your business logic will not know about DbContext etc.

Your UI will have no idea and will not instantiate anything related to EF.

Concrete answers to your points:

  1. Do not add abstractions, until you are very familiar with EF. It already adds good abstractions like UoW, queries, using POCOs etc.

  2. For DI to work, you have a Composition Root which references all components needed. This might or might not be in the WebUI project. If it isn't, you should expect that it does not reference EF or any other data related tech.

  3. Stop right here. Stop adding abstractions over abstractions. Start with direct and 'naive' architecture and develop it over time.

Abstractions are a tool to deal with complexity. Absence of complexity means no abstractions needed (yet).

  • To be clear that I understand what you're saying: The repository (which the interface exists in business, and concrete exists in Alcatraz.Data.Access?) accepts a DbContext as a its dependency. The business classes have repositories as a dependency. For the dependency injection, I'm doing this manually (so I understand what's going on). The reason I want to be able to set the connection string on the DbContext is I use database sharding so in certain cases I need to have entity framework to connect to different databases (of the same structure). Do I understand you properly? – Matthew Sep 2 '12 at 0:44
  • From the code you provided, it seems you are not doing DI at all. The main objective of DI is to free you and your code from managing the dependencies. I can't imagine you doing it effectively manually without a DI container. – Boris Yankov Sep 2 '12 at 18:40
  • also keep your mind open with DI. i already, for fun, asked the exactly same question here then on other forum to get opposite answers. DI is a pattern, not an architecture. Depending on your objective you can decide to use it or not. I use it but not for the reasons most of people tell me to use it. – Bastien Vandamme Jul 2 at 4:36

A few quick comments. I personally probably wouldn't pass a connection string. If anything I would try and create interfaces maybe for the repositories and just pass the interfaces around? Have the repositories implement or expose a IOW interface.

This way it doesn't event need to be a database that implements your repositories. they could be an in memory cache, or anything. Maybe then you could use some sort of dependancy injection framework to instantiate these even?

So in answer to some of your questions:

  1. Yes, I think it's ok
  2. I would still have the UI reference the EF project and the buisness layer reference interfaces that the EF repository layer implements. That way other projects could still use the same assemblies but they have the flexibility to swap out if desired?
  3. hmmm, Probably the repositories in the access layer but implementing an interface definition exposed in the business layer??

These are just some thoughts to mull over.

  • About point 2, one goal I was trying to make is to not have CRUD directly within the ui layer. What I mean is I want to make sure only CRUD can happen by going through the business layer, that way it is managed. – Matthew Sep 1 '12 at 17:53

Your Answer

