27

2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307

Embed Size (px)

Citation preview

Page 1: 2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307
Page 2: 2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307

2

Entity Framework, LINQ and n-Tier architectures

Guil MagalhaesDevelopment ConsultantMicrosoft

ARC307

Page 3: 2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307

3

Talking Points

Overview of LINQ, Entity Framework

nTier ArchitectureDesigning with requirements in mind

nTier Development with Entity FrameworkService Recommendations and Tips

Page 4: 2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307

4

LINQ

Page 5: 2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307

5

LINQ Overview

Introduced in .NET 3.0Super powerful.It rocks.We are going to see more LINQ throughout the presentation

Everything uses it now. You should too!

Page 6: 2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307

6

nTier

Page 7: 2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307

7

nTier Architecture Overview

Presentation Layer

Business Layer

Data Access Layer

UX LayerPresentation Layer

Communications Layer

You can have a simple or more elaborated design.It’s a good thing if you can use the same Entity everywhere. If you can’t, use at least the same object contract.

The Whole Thing Layer

Page 8: 2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307

8

Service/Business Logic Tier

Data Tier

Consumers/Presentation Tier

Service contract

nTier Architecture Overview

When?Medium to large enterprise applications

CharacteristicsStrong UI/business logic separationEnables service-orientationEnables re-use of services and components

UI in web browser

Entity Framework

Desktop application

Webserver

Page 9: 2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307

9

nTier Architecture Overview

Key points:You create layers to keep things organized, structured, extensible, etc... But definitely not just for fun.There’s no point on having layers if they have absolutely strong binding.Create strong boundaries in between the layers, interfaces with strong and possibly extensible contracts.

Page 10: 2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307

10

nTier Architecture Overview

Demos that you usually see are not real nTier. Even the nTier demos don’t usually map to your real world requirements.

Typical requirements:Granular/Business security on entities.Fast service response. Bulk operations.Consistent programming model.Transactional support.

Page 11: 2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307

11

Entity Framework

Page 12: 2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307

12

Entity Framework Overview

Page 13: 2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307

13

Building a Entity Framework Model

Guilherme MagalhaesTitleCompany

demo

Page 14: 2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307

14

Entity Framework Overview

You can use multiple tables for the same entityToday you can generate the EDM from the RDBMS, in the future you’ll be able to do the other way around.It supports change tracking, detaching and attaching entities from its ObjectContext

Many other good features

Page 15: 2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307

15

ObjectContext

Entity Framework OverviewChange Tracking Across Tiers

ObjectContext

Sales Order #1

Line Item A

Line Item B

detach*&Serializeentities

deserialize& attachentities

ObjectStateEntry

SO1 EntityKey Original ValuesCurrent Values Other ∆ Info

ObjectStateEntry

SO2 EntityKeyOriginal ValuesCurrent ValuesOther ∆ Info

ObjectStateEntry

LIA EntityKeyOriginal ValuesCurrent ValuesOther ∆ Info

ObjectStateEntry

LIB EntityKeyOriginal ValuesCurrent Values

Other ∆ Info

*Detach can be explicit or implicit. Serialization will automatically detach.

Page 16: 2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307

16

Entity Framework Overview

Entity State:UnchangedModifiedAddedDeletedDetached

Page 17: 2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307

17

EF with WCF

Putting it all together

demo

Page 18: 2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307

18

Recommendations - Service Design

Define strong contracts and boundaries.Do not mix entities on the same service.Create homogeneous CRUD operations if you can

This saves time and creates a better programming experience

Page 19: 2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307

19

Recommendations - Service Design

Why not use Read(string entitySQL)? Creates “static” or strongly typed queries. Isn’t that better than 500 “GetClientByID, GetClientByName, GetClientByEnrollmentDataInBetween?”Security: You would check ACL anyway, wouldn't you?Single or Many results. It saves you processing on the client and improves performance.Opinions?

How I used to do it

Page 20: 2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307

20

Recommendations - Service DesignBusiness Services

Think about Entity Services and Business Services

Business Services contain your business logic:ExecuteOrder(Order orderEntity) could check your stock levels, fire off the order orchestration...I like to see them as “business providers”. You can add new ones, replace or delete old ones... But your structured data is still safe and reliable.

Entity Services only handle simple entity information:

Collection<Order> ReadAll() return all your orders on a report.

Page 21: 2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307

21

Recommendations - Service Design

Go ahead and support Transactions! It doesn't hurt.

BPM is a really nice ally. And if you design your services this way it’s easier to adopt it later.

Page 22: 2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307

22

Recommendations - Service Design Warning! Warning!

There’s a few things to remember:Your EF generated WCF contracts don’t really support data member ordering or other advanced attributes.It’s not as fast as raw T-SQL.Silverlight 2.0 should be treated differently.

Page 23: 2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307

24

SummaryThe ADO.NET Entity Framework automatically generates a data-access layer from aconceptual modelThe EDM is serializible, you can use it everywhere.Each application scenario has its specific requirements, and the Entity Framework provides the building blocks to handle them.Service Design is super important to your solution. Using EF features can help you to make it more extensible.

Page 25: 2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307

29

Resources

www.microsoft.com/teched Tech·Talks Tech·Ed BloggersLive Simulcasts Virtual Labs

http://microsoft.com/technet

Evaluation licenses, pre-released products, and MORE!

http://microsoft.com/msdn

Developer’s Kit, Licenses, and MORE!

Page 26: 2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307

32

Please complete anevaluation

Page 27: 2 Entity Framework, LINQ and n- Tier architectures Guil Magalhaes Development Consultant Microsoft ARC307

33

© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED

OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.