views:

82

answers:

3

Hi I am trying architect a small project. I planning to have Data acess, Business Service, WcfService, UI Layer.

I am trying to use EF 4.0 and MVC 2.0. My question is where to generate the entities and ObjectContext through EF. I initially planned it in DataAccess. but to make entities available across all layer I have to reference DataAccess dll across all layers (which is not a Good approach).

Can I make entities in a new layer called Entities and leave ObjectContext in DA. How well it works.

Basic difference between Entities and POCO? (both should be generated by EF).

Is these Entities are available as DataContract (Seralized) by default?

I am trying to avoid repeating the code as much as possible. let me know how will this work.

Thanks

+2  A: 

I would recommend taking a look at a 'real-world' sample application called NerdDinner and 185-page PDF walkthrough 'how-each-line-was-written' on Code Plex.

Running app is here: http://www.nerddinner.com/

NerdDinner should be suitable for a small project - you will save a lot of overhead of a more complex solution. Otherwise you could introduce DTO objects between layers and use AutoMapper to reduce mundane 'property-by-property-copy' code.

Jakub Konecki
Nerd Dinner is not a layer architecture.
Praneeth
@Jakub : Turns out I DID have a +1 to give! @Praneeth : Maybe you need to explain what you mean by 'layered'. If you mean "artificially splitting an application into multiple projects/assemblies to gain some illusion of 'architecture'" then no; it's not layered. But if you mean, "separation of concerns into different logical sets of objects" then it certainly is. Granted; it's a simple example. But you said yourself your site was small.
Andrew Barber
Thanks for the reply. I clearly mentioned what I am trying to do I have layers in the sln we are not taking about layered architecture here, but how will EF works in this kind of architecture.
Praneeth
@Praneeth - Nerddiner *is* in fact a layered architecture. What it is not is a *tiered* architecture. The difference between a layer and a tier is that layers are seperate logical groups within the same physical group (typically an assembly, but in odd cases like VS Web Site projects it can be multiple assemblies).
Mystere Man
@Mystere Man - tiers usually refer to physical boundaries, not assemblies. You can deploy a 1-tier application that consists of multiple assemblies.
Jakub Konecki
@Jakub Konecki - Traditionally, you're correct, but in more modern uses "tiers" have become fuzzier. For example, if you are using a web service it's a tier, even if it's on the same machine. The accepted definition has come down to the use of assemblies (but again this is fuzzy as a Web Site project compiles each class to it's own assembly). My personal definition is this. "If a layer can exist on another computer, regardless of whether or not it actually is on another computer or held in a seperate assembly, than it's a tier"
Mystere Man
@Mystere Man - than I'm a traditionalist ;-)
Jakub Konecki
+1  A: 

Business Logic should be cleanly separated from Data Access; as you've correctly said, putting common objects to pass between all layers in the Data Access is bad.

  • Use your POCO's to pass data between layers, define these in a common assembly that's very free of dependencies (because all projects that need to exchange data will need to reference it.
  • Separate the Business Logic and Data Access with an interface, the interface will define the methods that are called to pass data in and out - and that data will be passed either as a primative base type (int, string, bool, etc) or a POCO (defined in your common assembly).
  • Within your data Access impementation use what ever you want - which in your case is EF. This means you'll have to convert the EF objects into POCOs but it means your architecture is clean.

But how would I create POCO (in code generation) as like Entities?

I work from the point of view that the Business Logic is where things "start" conceptually; to say that it embodies the Domain Model would also be fairly accurate.

POCO's are how we pass the information around - and for the most part their design will be driven by the needs of the Business Logic (or Domain Model). In the case of the Domain Model / DDD way of thinking the POCO's could possibly be part of that domain (at this point I'm still unsure if that's an issue or not).

So - how they are generated (conceptually) is by the needs of the Business Logic; however, if performance is a key aspect of your requirements you might also have some that are driven by performance related issues (such as getting a lot of data back in one big DTO rather than many more discrete calls).

How they are physcially generated? Well, I write them either by hand or using a small tool I hacked together. I tend to use Structs (and Collections) for my POCO's but you could use classes instead.

I haven't looked into generating auto-magically off the Business Logic or Domain Model for a couple of reasons:

  • It's hard.
  • Once generated they don't tend to change much - nor would you want to if they are used by all your assembilies, you'd quickly break your whole system.
  • I build different POCO's for different reasons, and that's definately a human judgement kind of thing.
Adrian K
Its a great answer. But how would I create POCO (in code generation) as like Entities? Is it possible and how will the association between POCO's and ContexObject will work (Same like having entities generated by EF) like Persistence and few other thinks.
Praneeth
so as of today we dont have POCO's generated by Microsoft through any template. to my other question who will ContexObject work with this POCO's
Praneeth
Anything that's specific to the physical Data Access code can't be used in other layers (assuming you want to adhere to the architecture described above). If you're refering to the ContexObject in EF then you can't do anything with that outside of your EF based Data Access (DA) Implementation. Within your EF based DA, you can use your POCO's - pass data from them into the queries you run aganist your ContextObjects.
Adrian K
A: 

Hey I recently looked at this article. This is what i was looking for. POCO's can't be used in WCF scenario. The best thing to use would be Self tracking entities and for this we can use t4 templates for code generation. I idle answer for my question would be Self tracking entities.

The advantage of using STE is explained in this article clearly.

Praneeth