Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect –...

34
Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect

Transcript of Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect –...

Page 1: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

Real World data access layersDataSet vs. Custom entities

Real World data access layersDataSet vs. Custom entities

Pierre GreborioSoftware Architect – PEWay SrL

Microsoft MVP – Solutions Architect

Page 2: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

AgendaAgenda

Architetture di riferimento Tecniche di accesso al RDBMS Business objects Metodi di accesso al dato persistente

Autonomous business object Data mapper

Page 3: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

LayeringLayering

E’ una delle tecniche più comuni per separare sistemi software complicati

Ogni layer superiore usufruisce dei servizi del layer immediatamente inferiore

Il layer inferiore non sa nulla del layer superiore

Page 4: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

…layering…layering

Il layering presenta anche degli svantaggi: E’ suscettibile delle modifiche a cascata

se si aggiunge un campo nel database che deve essere visualizzato nella UI

Può risultare un decadimento nelle prestazioni

il layering presuppone una trasformazione da una rappresentazione ad un’altra

Page 5: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

I layer principaliI layer principali

Presentazione Fornitura di servizi (facade), visualizzazione di

informazioni (UI), comandi della shell, ecc. Domain (o anche business logic)

Logica applicativa (è il vero cuore del sistema)

Data Source Comunicazione con i database, sistemi di

messaggistica, applicazioni esterne, ecc.

Page 6: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

Layer di presentazioneLayer di presentazione

Contiene la logica di interazione tra l’utilizzatore ed il software

Spazia da un comando shell a un’interfaccia grafica ricca (Windows o Web)

Potrebbe essere anche la facciata di un web service

Page 7: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

Layer di dominioLayer di dominio

Detto anche Business Layer o Business Logic

Include i calcoli basati sui dati di input ed i dati storicizzati

Valida tutti i dati che arrivano dalla presentazione

Page 8: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

Layer di data sourceLayer di data source

Serve a far comunicare l’applicazione con altri sistemi

In una applicazione enterprise solitamente questo layer si occupa della persistenza su database relazionale

Molto spesso le componenti che si occupano della persistenza si indicano sotto il nome di DAL (Data Access Layer)

Page 9: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

Componenti DALComponenti DAL

Creare record nel database da un BO Leggere record dal DB per generare BO Aggiornare record nel DB in base a

modifiche di un BO Cancellare un record nel DB Spesso si parla di CRUD: Create, Read,

Update e Delete

Page 10: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

Layering in praticaLayering in pratica

Solitamente il layer di dominio nasconde completamente il layer di accesso al dato

Capita anche che il layer di presentazione acceda al dato

Una soluzione può non essere pura, ma essere migliore in pratica

Usare il buon senso è la cosa migliore, se poi non è perfetto…refactoring !!

Page 11: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

Accedere al RDBMSAccedere al RDBMS

Uso un ORM Mi scrivo un ORM Mi scrivo una DAL personale Mi basta il designer di Visual Studio .NET Convinco il mio capo a passare a MS

Access o Excel !

Page 12: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

ORMORM

Object Relational Mapping tool Server a mappare il mondo object oriented

al mondo del database relazionale Vanno dai generatori di codice ai mapper

(XML) puri I più famosi: NHibernate, ORM.NET,

EntityBroker, IBatis.NET, NPersist, WilsonORMapper, …

Page 13: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

ORM customORM custom

Ha senso solamente se volete venderlo come prodotto

Richiede un grande investimento di risorse Bisogna conoscere molte casistiche Ce ne sono già tanti

Page 14: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

DAL personaleDAL personale

Sviluppare l’accesso al dato in base alle proprie esigenze

Considerare sempre lo scenario di riferimento (applicazione monolitica, client-server, distribuita, su più database eterogenei, ecc.)

Page 15: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

Designer di VS.NETDesigner di VS.NET

Logica Drag&Drop E’ perfetto per la prototipazione Attenzione, crea un monolite !

Page 16: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

Business ObjectsBusiness Objects

La tecnologia usata può influenzare il DAL Classi fortemente tipizzate DataSet DataSet tipizzati XML

Page 17: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

DemoDemo

Persona

Codice

Nome

Cognome

DataDiNascita

CodiceFiscale

Business Object

Page 18: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

Classi tipizzateClassi tipizzate

Pro Astrazione Performance Serializzazione Manutenzione Tipizzazione forte

Contro Collezzioni

tipizzate complesse

Supporto alla concorrenza

Integrazione limitata

Page 19: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

DataSetDataSet

Pro Funzionalità

native Supporto alle

collezioni Manutenzione Serializzazione Concorrenza

Contro Non supportano la

singola istanza Non supportano il new

Performance Tipizzazione

debole

Page 20: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

DataSet tipizzatoDataSet tipizzato

Pro Funzionalità

native Supporto alle

collezioni Concorrenza Serializzazione Tipizzazione forte

Contro Non supportano la

singola istanza Non supportano il new

Performance

Page 21: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

XMLXML

Pro Integrazione Serializzazione Accoppiamento

flessibile

Contro Tipizzazione

debole Performance Difficile da

manutenere

Page 22: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

Metodi di accesso al DBMetodi di accesso al DB

Autonomous business object L’oggetto di business contiene anche i metodi

di accesso al dato Pattern: Active record

Mappers L’oggetto di business e l’entità nel DB hanno

una differente struttura Molte parti dell’oggetto, quali collezioni e

ereditarietà, non sono presenti nel DB

Page 23: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

Active record patternActive record pattern

L’oggetto contiene sia i dati che il comportamento

I dati (non tutti) devono essere persistiti sul DB

PersonaCodice

Nome

Cognome

DataDiNascita

CodiceFiscale

Insert

Update

Page 24: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

DemoDemo

CREATE TABLE Persone

(

[ID] INT IDENTITY PRIMARY KEY,

[Nome] VARCHAR(50) NULL,

[Cognome] VARCHAR(50) NOT NULL,

[DataDiNascita] DATETIME NULL,

[CodiceFiscale] CHAR(16) NULL

)

Active Record

Page 25: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

MapperMapper

Divisione netta fra il mondo di business ed il mondo della persistenza

E’ l’approccio standard degli ORM Ideale per:

Astrarre la persistenza Sviluppo in team Unit testing più granulare

Page 26: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

Mapper PatternsMapper Patterns

Data Mapper Table Data Gateway Row Data Gateway

Page 27: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

Data mapperData mapper

L’oggetto contiene i dati che il comportamento di business

I metodi di persistenza stanno in una classe esterna ma si riferiscono sempre alla classe da mappare

PersonaMapperCrea()

Modifica()

Cancella()

Leggi()

PersonaCodice

Nome

Cognome

DataDiNascita

CodiceFiscale

Page 28: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

Table Data GatewayTable Data Gateway

La TDG contiene tuttale la logica di accesso al DB

Solitamente è senza stato Può mappare più entità di business

PersonaGatewayCrea(Codice, Nome, …)

Modifica(Codice, Nome,…)

Cancella(Codice)

Leggi(Codice)

PersonaCodice

Nome

Cognome

DataDiNascita

CodiceFiscale

Page 29: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

Row Data GatewayRow Data Gateway

E’ un’estensione del Data Table Gateway Da usare quando la logica di ricerca

diventa complessa

PersonaGatewayCrea(Codice, Nome, …)

Modifica(Codice, Nome,…)

Cancella(Codice)

PersonaCodice

Nome

Cognome

DataDiNascita

CodiceFiscale

PersonaFinderLeggi(Codice)

LeggiPerCodiceFiscale(CF)

LeggiPerCognome(Cognome)

Page 30: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

Classi DB helperClassi DB helper

Sono classi general purpose personalizzate per la persistenza Factory di connessioni Conversioni DB type - .NET type Factory di comandi

Page 31: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

Data Access Application BlockData Access Application Block

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html/daab.asp

Page 32: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

ConsiderazioniConsiderazioni

Il DataSet tipizzato è ottimale per il pattern Active Record

Il DataSet è ottimale nelle architetture statefull o nelle lookup in cache

Le classi fortemente tipizzate sono molto flessibili e meno soggette ad errori di utilizzo

Page 33: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

Domande ?Domande ?

Page 34: Real World data access layers DataSet vs. Custom entities Pierre Greborio Software Architect – PEWay SrL Microsoft MVP – Solutions Architect.

RiferimentiRiferimenti

MSDN Data Patternshttp://msdn.microsoft.com/architecture/patterns/default.aspx?pull=/library/en-us/dnpatterns/html/dp.asp

MSDN Data Access Architecture Guidehttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/daag.asp

MSDN Data Access Application Blockhttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html/daab.asp

Patterns of Enterprise Application Architecture