2

Im creating a 4 layer project like this

  • a Data Access Layer
  • a Business Logic Layer
  • A Domain Model with only POCO classes relating to my entities (via EF5)
  • a website as the frontend

i have always until now mixed the DAL and the BLL together and referenced the DAL from the website directly. this time, i would like to have some real separation of concerns here and i want to create a DAL which is a real DAL and unit testable coupled with a BLL that is really persistence agnostic (you know, like the pros do it) Im planning on using EF5

I have read lot of sites like

soo basically i know i´ll have to use the factory, the repository and unit of work patterns but i dont know what goes where and what is a simple (but clear enough example) i could follow

what i know is that i shouldnt reference the DAL on the website bt i really dont know how to make the bridge.

Is there any example of this with say, a Product and an Order table?

Community
  • 1
  • 1
sergio
  • 1,026
  • 2
  • 19
  • 43

3 Answers3

1

Build a Service Layer that the Presentation Layer (web app) references. Then in the Service Layer you have a reference to the BLL, EF5 Entities and DAL. The Service Layer can be just a Class Library (ASP.NET Web API for example) or a Web Services layer (WCF for example).

Now your web application has no hard reference to the DAL, but rather only knows of the Service Layer and the EF5 Entities.

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
1

I believe that you are searching for the onion architecture.

... the “core” of the onion is the object model, which represents your domain. This layer would contain your POCO entities. Surrounding your domain entities are repository interfaces, which are in turn surrounded by service interfaces. Representing your repositories and services as interfaces decouples consumers from concrete implementations, enabling you to swap one out for another without affecting consumers, such as client UI’s or tests. The data access layer is represented in the outer layer as a set of repository classes which implement the repository interfaces. Similarly the logging component implements a logging interface in the service interfaces layer.

(from here)

You should also consider exploring inversion of control/dependency injection. I adopted SimpleInjector (see here and here) a few months ago. The man behind SimpleInjector has some enlightening posts that will help you (starting with this one).

Steven
  • 166,672
  • 24
  • 332
  • 435
qujck
  • 14,388
  • 4
  • 45
  • 74
1

soo basically i know i´ll have to use the factory, the repository and unit of work patterns but i dont know what goes where and what is a simple (but clear enough example) i could follow

You say you know you'll have to use them but do you have a precise idea why you need each of them in the first place ? ;)

Aren't you taking the wrong approach trying to mimic how "the pros" do and throw all of these patterns at your system at once ? Maybe you could just start writing the simplest, most naive implementation that fulfills your requirements (testable DAL + persistence-ignorant BLL) based on more general principles.

Design patterns are only a means to an end. Chances are you'll feel the need for them while designing or trying to improve your implementation, but maybe it won't be the case.

As far as good general application architecture guidelines, I'd recommend Uncle Bob's work on Clean Architecture. I've found that even when he comes to describing specific software constructs with his own vocabulary, the way he explains them makes it easy for you to see them as placeholders - general concepts that you can later equate with Repositories, Unit of work and so on.

guillaume31
  • 13,738
  • 1
  • 32
  • 51