8/8/2019 Leveraging C# 3.0 and LINQ-MM
1/33
8/8/2019 Leveraging C# 3.0 and LINQ-MM
2/33
Leveraging C# 3.0 &
LINQKrishnan Subramanian
8/8/2019 Leveraging C# 3.0 and LINQ-MM
3/33
Agenda
Architectures
Layers
Data access Business logic
Service logic
UI
Wrap up
8/8/2019 Leveraging C# 3.0 and LINQ-MM
4/33
Architectures
8/8/2019 Leveraging C# 3.0 and LINQ-MM
5/33
LINQ to SQLApplication
SQL Server
LINQ to SQL
from c in db.Customers
where c.City == "London"
select c.CompanyName
LINQ Query
SQL Query
SELECT CompanyName
FROM dbo.Customers
WHERE City = 'London'
Rows
ObjectsSubmitChanges()
DML or SProcs
db.Customers.InsertOnSubmit(c1);
c2.City = "Seattle";
db.Customers.DeleteOnSubmit(c3);
INSERT INTO Cust
UPDATE Cust
DELETE FROM Cust
8/8/2019 Leveraging C# 3.0 and LINQ-MM
6/33
Data access layer
What defines a data access layer?
Or, how do you recognize a class belongs to a data access
layer?
LINQ to SQL / LINQ to entities
Is it a DAL?
Do not confuse Language Integrated Queries (LINQ) with
SQL
8/8/2019 Leveraging C# 3.0 and LINQ-MM
7/33
LINQ to SQL: validation
LINQ to SQL is all generated code
Domain entities + relationships
Simple validation vs complex validation
8/8/2019 Leveraging C# 3.0 and LINQ-MM
8/33
LINQ to SQL: simple validation
8/8/2019 Leveraging C# 3.0 and LINQ-MM
9/33
LINQ to SQL: validation
Simple validation:
Dos:
Leverage partial classes Leverage partial methods
Donts:
Directly modify generated code
8/8/2019 Leveraging C# 3.0 and LINQ-MM
10/33
LINQ to SQL: advanced mapping
M : N relationships
Should be easy to navigate (read) M : N relationships
Custom aggregations/associations E.g. customer has an 'Address' property
8/8/2019 Leveraging C# 3.0 and LINQ-MM
11/33
LINQ to SQL: advanced mapping
8/8/2019 Leveraging C# 3.0 and LINQ-MM
12/33
LINQ to SQL: advanced mapping
Advanced mapping:
Dos:
Leverage partial classes (again!)
8/8/2019 Leveraging C# 3.0 and LINQ-MM
13/33
LINQ to SQL: data shaping
Querying in the object world! = SQL
LINQ (to SQL) is not constrained like SQL
Can manufacture hierarchies & arbitrary nestings
Can manufacture relationships not in the domain model
8/8/2019 Leveraging C# 3.0 and LINQ-MM
14/33
LINQ to SQL: data shaping
8/8/2019 Leveraging C# 3.0 and LINQ-MM
15/33
LINQ to SQL: data shaping
Data shaping
Dos:
Think about querying the domain model and not SQL Leverage C# 3.0s anonymous types & object initializers
Leverage LoadWith and AssociateWith methods on
DataLoadOptions class
8/8/2019 Leveraging C# 3.0 and LINQ-MM
16/33
LINQ to SQL: query composability
Leverage deferred query execution
8/8/2019 Leveraging C# 3.0 and LINQ-MM
17/33
Query composability
8/8/2019 Leveraging C# 3.0 and LINQ-MM
18/33
LINQ to SQL: query composability
Query composability
Dos:
Return types are IQueryable and notIEnumerable
Judicious use; can be abused!
DataContext DOs apply
Donts:
Create N methods forN query variants
8/8/2019 Leveraging C# 3.0 and LINQ-MM
19/33
LINQ to SQL: paging
8/8/2019 Leveraging C# 3.0 and LINQ-MM
20/33
LINQ to SQL: paging
8/8/2019 Leveraging C# 3.0 and LINQ-MM
21/33
LINQ to SQL: paging
Paging
Dos:
Paging is built-in! Super simple! Works with SQL Server 2000, 2005, 2008 (VS 2008
SP1)
Donts:
Re-invent the wheel
8/8/2019 Leveraging C# 3.0 and LINQ-MM
22/33
LINQ to SQL: dynamic queries
I dont know what the 'where'-clause looks like!
8/8/2019 Leveraging C# 3.0 and LINQ-MM
23/33
LINQ to SQL: dynamic queries
8/8/2019 Leveraging C# 3.0 and LINQ-MM
24/33
LINQ to SQL: dynamic queries
Dynamic queries
Dos:
U
nderstand expression trees Build expression trees:
Dynamically (can be complex)
From stringified values (simple, but not type safe)
8/8/2019 Leveraging C# 3.0 and LINQ-MM
25/33
Service logic
8/8/2019 Leveraging C# 3.0 and LINQ-MM
26/33
Service logic
WS - *
Explicit contracts (document centric)
Data on the outside! = data on the inside Translation required
Data services
REST/POX based
8/8/2019 Leveraging C# 3.0 and LINQ-MM
27/33
Service logic
8/8/2019 Leveraging C# 3.0 and LINQ-MM
28/33
Service logic: WS - *
WS - * based:
Dos:
Leverage object initializers Easy to write
Code readability and maintainability increase
Use extension methods for translations
Queries & chaining of extension methods
8/8/2019 Leveraging C# 3.0 and LINQ-MM
29/33
Clients
8/8/2019 Leveraging C# 3.0 and LINQ-MM
30/33
Clients
8/8/2019 Leveraging C# 3.0 and LINQ-MM
31/33
Summary
Explicit data access layer almost disappears
Rest of the layers 'slimmed' down
LINQ + C# 3.0 features results in code that:
Is readable
Is maintainable
Specifies intent vs being imperative (just makes it work )
8/8/2019 Leveraging C# 3.0 and LINQ-MM
32/33
Join the 2-day LINQ masterclass
www.microsoft.nl Training & Events MSDN
Or drop me an email: [email protected]
Microsoft Services
8/8/2019 Leveraging C# 3.0 and LINQ-MM
33/33
Top Related