SlideShare a Scribd company logo
LINQ2NHRicci Gian MariaGiorgetti Alessandro
Nhibernate permits you to query database with different techniquesNHibernate support for QueryHQLICRITERIASQLNHIBERNATE
The LINQ query expressed by code gets translated into an “Expression Tree”An Expression Tree is a representation of the query with a tree like this oneWhat is needed to build a LINQ provider 3 + (5 + 9) * 2
If an arithmetic expression is simple to visualize as a tree a LINQ query could be more complex.Luckily enough, we do not need to know this to use LINQ2NH, but is useful to understand the processExpression Tree in real world
We already have a LINQ2NH provider, is it not good?We have LINQ2NH in 2.1.. So? LINQ2NHHQLICRITERIASQLNHIBERNATE
2.1 LINQ provider is limited by features offered by ICriteriaThe query gets translated from ExpressionTree to ICriteria then from ICriteria to the real queryICriteria misses some features like Having clauseIt is difficult to create complex query with ICriteria so it is difficult to support full LINQ syntaxCurrent situation
First of all there was a rewrite of the HQL parser using an AST (Abstract Syntax Tree) based on ANTLRSteve Strong started from the Java ANTLR parser and converted for C#The resulting HQL engine now has four steps, the hard work is between 2nd and 3rd stepsA new idea – use HQL
Why rewrite HQL parser is needed for LINQThe big idea is to create a provider that translate an Expression Tree to the same Parse Tree of HQLSTRING(HQL)LINQThe complex part, is translating the Parse treeTranslation of parse tree is the point where mappings are consulted to generate the SQL treeReusing the same code for HQL and LINQ is the key for success and performancePARSE TREEICRITERIASQLNHIBERNATE
Is LINQ2NH mature enough?Actually the provider supports many features, but since parsing an Expression Tree is not so simple we still have something unsupportedWe will see this in detail in the second part, you should simple be aware that not full LINQ syntax is supported
Whylinq 2 NHIf we already have HQL, ICriteria and SQL, we really need LINQ?
LINQ is strongly typed, so you have full intellisense power and compile time checkingRefactoring capabilities, if you rename a property of a mapped class, you automatically have your LINQ queries updatedLINQ is standard in .NET, maybe you already know it, and you can use to query XML, object in memory, LDAP tree, Entity Framework  and so on.Differencebetween HQL and LINQ
Deferred execution – a key concept of LINQSome of the LINQ operator are called: “deferred” operatorA deferred operator is evaluated only during the iteration of the content, or if you call a “not deferred” operator (like ToList() )
Deferred execution – even in LINQ2NHThe very same happens for LINQ2NH queries.Nothing get executed here, you are only defining a query like you do with ICriteriaSince Count is a “non deferred” operator, a  query to the database is issued.
LINQ query is an expression tree that can be manipulated by code Like ICriteria is simple to add and remove condition, projections to an existing query.You can pass a LINQ query between layers, each layer can manipulate the query in a very simple way.Differencebetween HQL and LINQ
Very easy projectionYou can have one layer that manage the where part and another one that use projectionYou can project into Dto (Data Transfer Object) with great easy, respect HQLYou can leverage Anonymous Type instead of DTO.Difference between HQL and LINQ
You can build on the fly complex anonymous DTO, with calculation in it and let the provider do the workLINQ is expressiveSince you want to use the year part of the BirthDate property, the provider is using datepart SQL functionProvider is smart, and select only what is needed and not unnecessary properties
Linq to Nh – Part 2
A thing you need to be aware of…Some things that actually ‘do not work’How to extend the provider to overcome issues and add features.Part 2 - Agenda
Nhibernate best practice: use explicit Transations!Deferred execution might cause troubles when coupled with Transaction Management policies.It’s very easy to execute code outside the scope of a transaction, and you will hardly spot it at first.If your repository/dao or whateever data access strategy you have, exposes IQueriable<T> interfaces you need to be careful and call the functions that actually ‘trigger’ the Linq query evaluation inside the scope of your transation (i.e.: GetEnumerator(), Count(), ToList(), Single(), etc…).A Thing to be Aware of…Linq2Nh and Transactions
Some Functions/Operators not supported – for example: Equals() is supported only for strings at the moment, for all other types you can use the ==operator.Problems when using Ilists<T> while trying to create expressions that use ‘contains’ to realize ‘Select..from…where..in’ queries. The parser goes crazy and try to compare an IList<T> to T entities.Custom IUserType(s) not supported yet. (It’s a bug and should be corrected soon..remember it’s an alpha version).Problems with some Joins and Nested Queries, you need to rework your code if you want to use Linq to perform these kind of queries.During everyday usage of the provider you can encounter the following problems:Some things that actually ‘do not work’
Fix some of the holes in the provider – the missing equals for example.Add new features and functionalities.Extending the provider is very easy and it allows you to:Extending Linq To NHibernate
Given your Linq Query a parser examines it.Each segment of the expression that forms the query is translated from a Linq Expression to an Hql Expression (The old provider was based on the ICriteria API).Your operators are replaced with the predefined corresponding ones (or the one you defined/customized).The result is the internal representation of an HQL query that is later on converted to a real SQL query.Putting it simple the new provider works this way:Extending Linq To NHibernate
Create a ‘Method Generator’ – a class that basically tells Nhibernate how to build an Hql Expression that represent your Linq function.Register the Method Generator in the internal methods repository – inherit from DefaultLinqToHqlGeneratorsRegistry to add you custom methods.Tell Nhibernate to use your custom method registry intead of the default one:To customize the provider you have to:Extending Linq To NHibernate
Inherit from the base class BaseHqlGeneratorForMethodProvide the list of supported methods (a sort of call signatures)Override the BuildHql() function that will provide the HqlExpression that represent your operator/functionImplement the IRuntimeMethodHqlGeneratorinterface (usefull when you do not know the types involved ‘at design time’).you do not provide a list of function calls to matchImplement the  bool SupportsMethod(MethodInfo method) – states if the method can be represented by the generator.Implement the IHqlGeneratorForMethod GetMethodGenerator(MethodInfo method) – returns a method generator that will be used to generate the HqlExpressionTo create a Method Generator you can:Extending Linq To NHibernate
DemoExtending the provider
Thank you for attending!

More Related Content

Viewers also liked

Nicholas' Performance Talk at Google
Nicholas' Performance Talk at GoogleNicholas' Performance Talk at Google
Nicholas' Performance Talk at Google
Nicholas Zakas
 
Introduction to Application insights
Introduction to Application insightsIntroduction to Application insights
Introduction to Application insights
Gian Maria Ricci
 
Git branching model
Git branching modelGit branching model
Git branching model
Gian Maria Ricci
 
YUI Test The Next Generation (YUIConf 2010)
YUI Test The Next Generation (YUIConf 2010)YUI Test The Next Generation (YUIConf 2010)
YUI Test The Next Generation (YUIConf 2010)
Nicholas Zakas
 
Progressive Enhancement 2.0 (jQuery Conference SF Bay Area 2011)
Progressive Enhancement 2.0 (jQuery Conference SF Bay Area 2011)Progressive Enhancement 2.0 (jQuery Conference SF Bay Area 2011)
Progressive Enhancement 2.0 (jQuery Conference SF Bay Area 2011)
Nicholas Zakas
 
High Performance JavaScript - jQuery Conference SF Bay Area 2010
High Performance JavaScript - jQuery Conference SF Bay Area 2010High Performance JavaScript - jQuery Conference SF Bay Area 2010
High Performance JavaScript - jQuery Conference SF Bay Area 2010
Nicholas Zakas
 

Viewers also liked (6)

Nicholas' Performance Talk at Google
Nicholas' Performance Talk at GoogleNicholas' Performance Talk at Google
Nicholas' Performance Talk at Google
 
Introduction to Application insights
Introduction to Application insightsIntroduction to Application insights
Introduction to Application insights
 
Git branching model
Git branching modelGit branching model
Git branching model
 
YUI Test The Next Generation (YUIConf 2010)
YUI Test The Next Generation (YUIConf 2010)YUI Test The Next Generation (YUIConf 2010)
YUI Test The Next Generation (YUIConf 2010)
 
Progressive Enhancement 2.0 (jQuery Conference SF Bay Area 2011)
Progressive Enhancement 2.0 (jQuery Conference SF Bay Area 2011)Progressive Enhancement 2.0 (jQuery Conference SF Bay Area 2011)
Progressive Enhancement 2.0 (jQuery Conference SF Bay Area 2011)
 
High Performance JavaScript - jQuery Conference SF Bay Area 2010
High Performance JavaScript - jQuery Conference SF Bay Area 2010High Performance JavaScript - jQuery Conference SF Bay Area 2010
High Performance JavaScript - jQuery Conference SF Bay Area 2010
 

Similar to NHDay Introduction to LINQ2NH

Link quries
Link quriesLink quries
Link quries
ulfat mushtaq
 
Iterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design patternIterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design pattern
Nitin Bhide
 
Linq in C#
Linq in C#Linq in C#
Linq in C#
Umar Farooq
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_iv
Nico Ludwig
 
LINQ 2 SQL Presentation To Palmchip And Trg, Technology Resource Group
LINQ 2 SQL Presentation To Palmchip  And Trg, Technology Resource GroupLINQ 2 SQL Presentation To Palmchip  And Trg, Technology Resource Group
LINQ 2 SQL Presentation To Palmchip And Trg, Technology Resource Group
Shahzad
 
Spark sql meetup
Spark sql meetupSpark sql meetup
Spark sql meetup
Michael Zhang
 
Linq
LinqLinq
Linq in C# 3.0: An Overview
Linq in C# 3.0: An OverviewLinq in C# 3.0: An Overview
Linq in C# 3.0: An Overview
pradeepkothiyal
 
LINQ Inside
LINQ InsideLINQ Inside
LINQ Inside
jeffz
 
Understanding LINQ in C#
Understanding LINQ in C# Understanding LINQ in C#
Understanding LINQ in C#
MD. Shohag Mia
 
LINQ PPT.pptx
LINQ PPT.pptxLINQ PPT.pptx
LINQ PPT.pptx
09AnkitkumarJhariya
 
SQL ttrain wrwrwrw wwrw wwrrrwrwrwrwwrwr.pptx
SQL ttrain wrwrwrw wwrw wwrrrwrwrwrwwrwr.pptxSQL ttrain wrwrwrw wwrw wwrrrwrwrwrwwrwr.pptx
SQL ttrain wrwrwrw wwrw wwrrrwrwrwrwwrwr.pptx
antony194610
 
C# advanced topics and future - C#5
C# advanced topics and future - C#5C# advanced topics and future - C#5
C# advanced topics and future - C#5
Peter Gfader
 
Android meetup
Android meetupAndroid meetup
Android meetup
Vitali Pekelis
 
The LINQ Between XML and Database
The LINQ Between XML and DatabaseThe LINQ Between XML and Database
The LINQ Between XML and Database
IRJET Journal
 
C#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New FeaturesC#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New Features
techfreak
 
C# 3.0 and LINQ Tech Talk
C# 3.0 and LINQ Tech TalkC# 3.0 and LINQ Tech Talk
C# 3.0 and LINQ Tech Talk
Michael Heydt
 
Linq
LinqLinq
Linq
ClickExpo
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language Analysis
Nicole Gomez
 
Beginning linq
Beginning linqBeginning linq
Beginning linq
Shikha Gupta
 

Similar to NHDay Introduction to LINQ2NH (20)

Link quries
Link quriesLink quries
Link quries
 
Iterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design patternIterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design pattern
 
Linq in C#
Linq in C#Linq in C#
Linq in C#
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_iv
 
LINQ 2 SQL Presentation To Palmchip And Trg, Technology Resource Group
LINQ 2 SQL Presentation To Palmchip  And Trg, Technology Resource GroupLINQ 2 SQL Presentation To Palmchip  And Trg, Technology Resource Group
LINQ 2 SQL Presentation To Palmchip And Trg, Technology Resource Group
 
Spark sql meetup
Spark sql meetupSpark sql meetup
Spark sql meetup
 
Linq
LinqLinq
Linq
 
Linq in C# 3.0: An Overview
Linq in C# 3.0: An OverviewLinq in C# 3.0: An Overview
Linq in C# 3.0: An Overview
 
LINQ Inside
LINQ InsideLINQ Inside
LINQ Inside
 
Understanding LINQ in C#
Understanding LINQ in C# Understanding LINQ in C#
Understanding LINQ in C#
 
LINQ PPT.pptx
LINQ PPT.pptxLINQ PPT.pptx
LINQ PPT.pptx
 
SQL ttrain wrwrwrw wwrw wwrrrwrwrwrwwrwr.pptx
SQL ttrain wrwrwrw wwrw wwrrrwrwrwrwwrwr.pptxSQL ttrain wrwrwrw wwrw wwrrrwrwrwrwwrwr.pptx
SQL ttrain wrwrwrw wwrw wwrrrwrwrwrwwrwr.pptx
 
C# advanced topics and future - C#5
C# advanced topics and future - C#5C# advanced topics and future - C#5
C# advanced topics and future - C#5
 
Android meetup
Android meetupAndroid meetup
Android meetup
 
The LINQ Between XML and Database
The LINQ Between XML and DatabaseThe LINQ Between XML and Database
The LINQ Between XML and Database
 
C#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New FeaturesC#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New Features
 
C# 3.0 and LINQ Tech Talk
C# 3.0 and LINQ Tech TalkC# 3.0 and LINQ Tech Talk
C# 3.0 and LINQ Tech Talk
 
Linq
LinqLinq
Linq
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language Analysis
 
Beginning linq
Beginning linqBeginning linq
Beginning linq
 

More from Gian Maria Ricci

Se non sviluppo codice non sto lavorando
Se non sviluppo codice non sto lavorandoSe non sviluppo codice non sto lavorando
Se non sviluppo codice non sto lavorando
Gian Maria Ricci
 
Gestire la qualità del codice con Visual Studio, SonarQube ed Azure Devops
Gestire la qualità del codice con Visual Studio, SonarQube ed Azure DevopsGestire la qualità del codice con Visual Studio, SonarQube ed Azure Devops
Gestire la qualità del codice con Visual Studio, SonarQube ed Azure Devops
Gian Maria Ricci
 
Migrare da un VCS centralizzato a Git
Migrare da un VCS centralizzato a GitMigrare da un VCS centralizzato a Git
Migrare da un VCS centralizzato a Git
Gian Maria Ricci
 
Real World Build + Release automation in Azure DevOps
Real World Build + Release automation in Azure DevOpsReal World Build + Release automation in Azure DevOps
Real World Build + Release automation in Azure DevOps
Gian Maria Ricci
 
Gestire i rilasci automatici con azure devops
Gestire i rilasci automatici con azure devopsGestire i rilasci automatici con azure devops
Gestire i rilasci automatici con azure devops
Gian Maria Ricci
 
Build and release in code with azure devops pipelines
Build and release in code with azure devops pipelinesBuild and release in code with azure devops pipelines
Build and release in code with azure devops pipelines
Gian Maria Ricci
 
Azure Pipeline in salsa yaml
Azure Pipeline in salsa yamlAzure Pipeline in salsa yaml
Azure Pipeline in salsa yaml
Gian Maria Ricci
 
Git gitflow pull requests in devops focused teams
Git gitflow pull requests in devops focused teamsGit gitflow pull requests in devops focused teams
Git gitflow pull requests in devops focused teams
Gian Maria Ricci
 
Distribute your code with NUget and build vNext
Distribute your code with NUget and build vNextDistribute your code with NUget and build vNext
Distribute your code with NUget and build vNext
Gian Maria Ricci
 
Manage your environment with DSC
Manage your environment with DSCManage your environment with DSC
Manage your environment with DSC
Gian Maria Ricci
 
Deploy applications with TFS Build
Deploy applications with TFS BuildDeploy applications with TFS Build
Deploy applications with TFS Build
Gian Maria Ricci
 
TFS - Quale source control
TFS - Quale source controlTFS - Quale source control
TFS - Quale source control
Gian Maria Ricci
 
Branch model in Git
Branch model in GitBranch model in Git
Branch model in Git
Gian Maria Ricci
 
Introduction to Visual Studio Online
Introduction to Visual Studio OnlineIntroduction to Visual Studio Online
Introduction to Visual Studio Online
Gian Maria Ricci
 
Git si o Git No
Git si o Git NoGit si o Git No
Git si o Git No
Gian Maria Ricci
 
Testing
TestingTesting
Come Organizzare il proprio Team Project
Come Organizzare il proprio Team ProjectCome Organizzare il proprio Team Project
Come Organizzare il proprio Team Project
Gian Maria Ricci
 
Git Perchè Usarlo
Git Perchè UsarloGit Perchè Usarlo
Git Perchè Usarlo
Gian Maria Ricci
 
Automatically deployment to Azure Web Sites
Automatically deployment to Azure Web SitesAutomatically deployment to Azure Web Sites
Automatically deployment to Azure Web Sites
Gian Maria Ricci
 
Porfolio Management in TFS 2013
Porfolio Management in TFS 2013Porfolio Management in TFS 2013
Porfolio Management in TFS 2013
Gian Maria Ricci
 

More from Gian Maria Ricci (20)

Se non sviluppo codice non sto lavorando
Se non sviluppo codice non sto lavorandoSe non sviluppo codice non sto lavorando
Se non sviluppo codice non sto lavorando
 
Gestire la qualità del codice con Visual Studio, SonarQube ed Azure Devops
Gestire la qualità del codice con Visual Studio, SonarQube ed Azure DevopsGestire la qualità del codice con Visual Studio, SonarQube ed Azure Devops
Gestire la qualità del codice con Visual Studio, SonarQube ed Azure Devops
 
Migrare da un VCS centralizzato a Git
Migrare da un VCS centralizzato a GitMigrare da un VCS centralizzato a Git
Migrare da un VCS centralizzato a Git
 
Real World Build + Release automation in Azure DevOps
Real World Build + Release automation in Azure DevOpsReal World Build + Release automation in Azure DevOps
Real World Build + Release automation in Azure DevOps
 
Gestire i rilasci automatici con azure devops
Gestire i rilasci automatici con azure devopsGestire i rilasci automatici con azure devops
Gestire i rilasci automatici con azure devops
 
Build and release in code with azure devops pipelines
Build and release in code with azure devops pipelinesBuild and release in code with azure devops pipelines
Build and release in code with azure devops pipelines
 
Azure Pipeline in salsa yaml
Azure Pipeline in salsa yamlAzure Pipeline in salsa yaml
Azure Pipeline in salsa yaml
 
Git gitflow pull requests in devops focused teams
Git gitflow pull requests in devops focused teamsGit gitflow pull requests in devops focused teams
Git gitflow pull requests in devops focused teams
 
Distribute your code with NUget and build vNext
Distribute your code with NUget and build vNextDistribute your code with NUget and build vNext
Distribute your code with NUget and build vNext
 
Manage your environment with DSC
Manage your environment with DSCManage your environment with DSC
Manage your environment with DSC
 
Deploy applications with TFS Build
Deploy applications with TFS BuildDeploy applications with TFS Build
Deploy applications with TFS Build
 
TFS - Quale source control
TFS - Quale source controlTFS - Quale source control
TFS - Quale source control
 
Branch model in Git
Branch model in GitBranch model in Git
Branch model in Git
 
Introduction to Visual Studio Online
Introduction to Visual Studio OnlineIntroduction to Visual Studio Online
Introduction to Visual Studio Online
 
Git si o Git No
Git si o Git NoGit si o Git No
Git si o Git No
 
Testing
TestingTesting
Testing
 
Come Organizzare il proprio Team Project
Come Organizzare il proprio Team ProjectCome Organizzare il proprio Team Project
Come Organizzare il proprio Team Project
 
Git Perchè Usarlo
Git Perchè UsarloGit Perchè Usarlo
Git Perchè Usarlo
 
Automatically deployment to Azure Web Sites
Automatically deployment to Azure Web SitesAutomatically deployment to Azure Web Sites
Automatically deployment to Azure Web Sites
 
Porfolio Management in TFS 2013
Porfolio Management in TFS 2013Porfolio Management in TFS 2013
Porfolio Management in TFS 2013
 

Recently uploaded

WhatsApp Spy Online Trackers and Monitoring Apps
WhatsApp Spy Online Trackers and Monitoring AppsWhatsApp Spy Online Trackers and Monitoring Apps
WhatsApp Spy Online Trackers and Monitoring Apps
HackersList
 
Dublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptx
Dublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptxDublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptx
Dublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptx
Kunal Gupta
 
Integrating Kafka with MuleSoft 4 and usecase
Integrating Kafka with MuleSoft 4 and usecaseIntegrating Kafka with MuleSoft 4 and usecase
Integrating Kafka with MuleSoft 4 and usecase
shyamraj55
 
Tailored CRM Software Development for Enhanced Customer Insights
Tailored CRM Software Development for Enhanced Customer InsightsTailored CRM Software Development for Enhanced Customer Insights
Tailored CRM Software Development for Enhanced Customer Insights
SynapseIndia
 
BLOCKCHAIN TECHNOLOGY - Advantages and Disadvantages
BLOCKCHAIN TECHNOLOGY - Advantages and DisadvantagesBLOCKCHAIN TECHNOLOGY - Advantages and Disadvantages
BLOCKCHAIN TECHNOLOGY - Advantages and Disadvantages
SAI KAILASH R
 
(CISOPlatform Summit & SACON 2024) Orientation by CISO Platform_ Using CISO P...
(CISOPlatform Summit & SACON 2024) Orientation by CISO Platform_ Using CISO P...(CISOPlatform Summit & SACON 2024) Orientation by CISO Platform_ Using CISO P...
(CISOPlatform Summit & SACON 2024) Orientation by CISO Platform_ Using CISO P...
Priyanka Aash
 
Types of Weaving loom machine & it's technology
Types of Weaving loom machine & it's technologyTypes of Weaving loom machine & it's technology
Types of Weaving loom machine & it's technology
ldtexsolbl
 
(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...
(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...
(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...
Priyanka Aash
 
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptxUse Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
SynapseIndia
 
Sonkoloniya documentation - ONEprojukti.pdf
Sonkoloniya documentation - ONEprojukti.pdfSonkoloniya documentation - ONEprojukti.pdf
Sonkoloniya documentation - ONEprojukti.pdf
SubhamMandal40
 
Introduction-to-the-IAM-Platform-Implementation-Plan.pptx
Introduction-to-the-IAM-Platform-Implementation-Plan.pptxIntroduction-to-the-IAM-Platform-Implementation-Plan.pptx
Introduction-to-the-IAM-Platform-Implementation-Plan.pptx
313mohammedarshad
 
The importance of Quality Assurance for ICT Standardization
The importance of Quality Assurance for ICT StandardizationThe importance of Quality Assurance for ICT Standardization
The importance of Quality Assurance for ICT Standardization
Axel Rennoch
 
Google I/O Extended Harare Merged Slides
Google I/O Extended Harare Merged SlidesGoogle I/O Extended Harare Merged Slides
Google I/O Extended Harare Merged Slides
Google Developer Group - Harare
 
IPLOOK Remote-Sensing Satellite Solution
IPLOOK Remote-Sensing Satellite SolutionIPLOOK Remote-Sensing Satellite Solution
IPLOOK Remote-Sensing Satellite Solution
IPLOOK Networks
 
Data Integration Basics: Merging & Joining Data
Data Integration Basics: Merging & Joining DataData Integration Basics: Merging & Joining Data
Data Integration Basics: Merging & Joining Data
Safe Software
 
Feature sql server terbaru performance.pptx
Feature sql server terbaru performance.pptxFeature sql server terbaru performance.pptx
Feature sql server terbaru performance.pptx
ssuser1915fe1
 
leewayhertz.com-AI agents for healthcare Applications benefits and implementa...
leewayhertz.com-AI agents for healthcare Applications benefits and implementa...leewayhertz.com-AI agents for healthcare Applications benefits and implementa...
leewayhertz.com-AI agents for healthcare Applications benefits and implementa...
alexjohnson7307
 
Best Practices for Effectively Running dbt in Airflow.pdf
Best Practices for Effectively Running dbt in Airflow.pdfBest Practices for Effectively Running dbt in Airflow.pdf
Best Practices for Effectively Running dbt in Airflow.pdf
Tatiana Al-Chueyr
 
Semantic-Aware Code Model: Elevating the Future of Software Development
Semantic-Aware Code Model: Elevating the Future of Software DevelopmentSemantic-Aware Code Model: Elevating the Future of Software Development
Semantic-Aware Code Model: Elevating the Future of Software Development
Baishakhi Ray
 
Acumatica vs. Sage Intacct _Construction_July (1).pptx
Acumatica vs. Sage Intacct _Construction_July (1).pptxAcumatica vs. Sage Intacct _Construction_July (1).pptx
Acumatica vs. Sage Intacct _Construction_July (1).pptx
BrainSell Technologies
 

Recently uploaded (20)

WhatsApp Spy Online Trackers and Monitoring Apps
WhatsApp Spy Online Trackers and Monitoring AppsWhatsApp Spy Online Trackers and Monitoring Apps
WhatsApp Spy Online Trackers and Monitoring Apps
 
Dublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptx
Dublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptxDublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptx
Dublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptx
 
Integrating Kafka with MuleSoft 4 and usecase
Integrating Kafka with MuleSoft 4 and usecaseIntegrating Kafka with MuleSoft 4 and usecase
Integrating Kafka with MuleSoft 4 and usecase
 
Tailored CRM Software Development for Enhanced Customer Insights
Tailored CRM Software Development for Enhanced Customer InsightsTailored CRM Software Development for Enhanced Customer Insights
Tailored CRM Software Development for Enhanced Customer Insights
 
BLOCKCHAIN TECHNOLOGY - Advantages and Disadvantages
BLOCKCHAIN TECHNOLOGY - Advantages and DisadvantagesBLOCKCHAIN TECHNOLOGY - Advantages and Disadvantages
BLOCKCHAIN TECHNOLOGY - Advantages and Disadvantages
 
(CISOPlatform Summit & SACON 2024) Orientation by CISO Platform_ Using CISO P...
(CISOPlatform Summit & SACON 2024) Orientation by CISO Platform_ Using CISO P...(CISOPlatform Summit & SACON 2024) Orientation by CISO Platform_ Using CISO P...
(CISOPlatform Summit & SACON 2024) Orientation by CISO Platform_ Using CISO P...
 
Types of Weaving loom machine & it's technology
Types of Weaving loom machine & it's technologyTypes of Weaving loom machine & it's technology
Types of Weaving loom machine & it's technology
 
(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...
(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...
(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...
 
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptxUse Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
 
Sonkoloniya documentation - ONEprojukti.pdf
Sonkoloniya documentation - ONEprojukti.pdfSonkoloniya documentation - ONEprojukti.pdf
Sonkoloniya documentation - ONEprojukti.pdf
 
Introduction-to-the-IAM-Platform-Implementation-Plan.pptx
Introduction-to-the-IAM-Platform-Implementation-Plan.pptxIntroduction-to-the-IAM-Platform-Implementation-Plan.pptx
Introduction-to-the-IAM-Platform-Implementation-Plan.pptx
 
The importance of Quality Assurance for ICT Standardization
The importance of Quality Assurance for ICT StandardizationThe importance of Quality Assurance for ICT Standardization
The importance of Quality Assurance for ICT Standardization
 
Google I/O Extended Harare Merged Slides
Google I/O Extended Harare Merged SlidesGoogle I/O Extended Harare Merged Slides
Google I/O Extended Harare Merged Slides
 
IPLOOK Remote-Sensing Satellite Solution
IPLOOK Remote-Sensing Satellite SolutionIPLOOK Remote-Sensing Satellite Solution
IPLOOK Remote-Sensing Satellite Solution
 
Data Integration Basics: Merging & Joining Data
Data Integration Basics: Merging & Joining DataData Integration Basics: Merging & Joining Data
Data Integration Basics: Merging & Joining Data
 
Feature sql server terbaru performance.pptx
Feature sql server terbaru performance.pptxFeature sql server terbaru performance.pptx
Feature sql server terbaru performance.pptx
 
leewayhertz.com-AI agents for healthcare Applications benefits and implementa...
leewayhertz.com-AI agents for healthcare Applications benefits and implementa...leewayhertz.com-AI agents for healthcare Applications benefits and implementa...
leewayhertz.com-AI agents for healthcare Applications benefits and implementa...
 
Best Practices for Effectively Running dbt in Airflow.pdf
Best Practices for Effectively Running dbt in Airflow.pdfBest Practices for Effectively Running dbt in Airflow.pdf
Best Practices for Effectively Running dbt in Airflow.pdf
 
Semantic-Aware Code Model: Elevating the Future of Software Development
Semantic-Aware Code Model: Elevating the Future of Software DevelopmentSemantic-Aware Code Model: Elevating the Future of Software Development
Semantic-Aware Code Model: Elevating the Future of Software Development
 
Acumatica vs. Sage Intacct _Construction_July (1).pptx
Acumatica vs. Sage Intacct _Construction_July (1).pptxAcumatica vs. Sage Intacct _Construction_July (1).pptx
Acumatica vs. Sage Intacct _Construction_July (1).pptx
 

NHDay Introduction to LINQ2NH

  • 2. Nhibernate permits you to query database with different techniquesNHibernate support for QueryHQLICRITERIASQLNHIBERNATE
  • 3. The LINQ query expressed by code gets translated into an “Expression Tree”An Expression Tree is a representation of the query with a tree like this oneWhat is needed to build a LINQ provider 3 + (5 + 9) * 2
  • 4. If an arithmetic expression is simple to visualize as a tree a LINQ query could be more complex.Luckily enough, we do not need to know this to use LINQ2NH, but is useful to understand the processExpression Tree in real world
  • 5. We already have a LINQ2NH provider, is it not good?We have LINQ2NH in 2.1.. So? LINQ2NHHQLICRITERIASQLNHIBERNATE
  • 6. 2.1 LINQ provider is limited by features offered by ICriteriaThe query gets translated from ExpressionTree to ICriteria then from ICriteria to the real queryICriteria misses some features like Having clauseIt is difficult to create complex query with ICriteria so it is difficult to support full LINQ syntaxCurrent situation
  • 7. First of all there was a rewrite of the HQL parser using an AST (Abstract Syntax Tree) based on ANTLRSteve Strong started from the Java ANTLR parser and converted for C#The resulting HQL engine now has four steps, the hard work is between 2nd and 3rd stepsA new idea – use HQL
  • 8. Why rewrite HQL parser is needed for LINQThe big idea is to create a provider that translate an Expression Tree to the same Parse Tree of HQLSTRING(HQL)LINQThe complex part, is translating the Parse treeTranslation of parse tree is the point where mappings are consulted to generate the SQL treeReusing the same code for HQL and LINQ is the key for success and performancePARSE TREEICRITERIASQLNHIBERNATE
  • 9. Is LINQ2NH mature enough?Actually the provider supports many features, but since parsing an Expression Tree is not so simple we still have something unsupportedWe will see this in detail in the second part, you should simple be aware that not full LINQ syntax is supported
  • 10. Whylinq 2 NHIf we already have HQL, ICriteria and SQL, we really need LINQ?
  • 11. LINQ is strongly typed, so you have full intellisense power and compile time checkingRefactoring capabilities, if you rename a property of a mapped class, you automatically have your LINQ queries updatedLINQ is standard in .NET, maybe you already know it, and you can use to query XML, object in memory, LDAP tree, Entity Framework  and so on.Differencebetween HQL and LINQ
  • 12. Deferred execution – a key concept of LINQSome of the LINQ operator are called: “deferred” operatorA deferred operator is evaluated only during the iteration of the content, or if you call a “not deferred” operator (like ToList() )
  • 13. Deferred execution – even in LINQ2NHThe very same happens for LINQ2NH queries.Nothing get executed here, you are only defining a query like you do with ICriteriaSince Count is a “non deferred” operator, a query to the database is issued.
  • 14. LINQ query is an expression tree that can be manipulated by code Like ICriteria is simple to add and remove condition, projections to an existing query.You can pass a LINQ query between layers, each layer can manipulate the query in a very simple way.Differencebetween HQL and LINQ
  • 15. Very easy projectionYou can have one layer that manage the where part and another one that use projectionYou can project into Dto (Data Transfer Object) with great easy, respect HQLYou can leverage Anonymous Type instead of DTO.Difference between HQL and LINQ
  • 16. You can build on the fly complex anonymous DTO, with calculation in it and let the provider do the workLINQ is expressiveSince you want to use the year part of the BirthDate property, the provider is using datepart SQL functionProvider is smart, and select only what is needed and not unnecessary properties
  • 17. Linq to Nh – Part 2
  • 18. A thing you need to be aware of…Some things that actually ‘do not work’How to extend the provider to overcome issues and add features.Part 2 - Agenda
  • 19. Nhibernate best practice: use explicit Transations!Deferred execution might cause troubles when coupled with Transaction Management policies.It’s very easy to execute code outside the scope of a transaction, and you will hardly spot it at first.If your repository/dao or whateever data access strategy you have, exposes IQueriable<T> interfaces you need to be careful and call the functions that actually ‘trigger’ the Linq query evaluation inside the scope of your transation (i.e.: GetEnumerator(), Count(), ToList(), Single(), etc…).A Thing to be Aware of…Linq2Nh and Transactions
  • 20. Some Functions/Operators not supported – for example: Equals() is supported only for strings at the moment, for all other types you can use the ==operator.Problems when using Ilists<T> while trying to create expressions that use ‘contains’ to realize ‘Select..from…where..in’ queries. The parser goes crazy and try to compare an IList<T> to T entities.Custom IUserType(s) not supported yet. (It’s a bug and should be corrected soon..remember it’s an alpha version).Problems with some Joins and Nested Queries, you need to rework your code if you want to use Linq to perform these kind of queries.During everyday usage of the provider you can encounter the following problems:Some things that actually ‘do not work’
  • 21. Fix some of the holes in the provider – the missing equals for example.Add new features and functionalities.Extending the provider is very easy and it allows you to:Extending Linq To NHibernate
  • 22. Given your Linq Query a parser examines it.Each segment of the expression that forms the query is translated from a Linq Expression to an Hql Expression (The old provider was based on the ICriteria API).Your operators are replaced with the predefined corresponding ones (or the one you defined/customized).The result is the internal representation of an HQL query that is later on converted to a real SQL query.Putting it simple the new provider works this way:Extending Linq To NHibernate
  • 23. Create a ‘Method Generator’ – a class that basically tells Nhibernate how to build an Hql Expression that represent your Linq function.Register the Method Generator in the internal methods repository – inherit from DefaultLinqToHqlGeneratorsRegistry to add you custom methods.Tell Nhibernate to use your custom method registry intead of the default one:To customize the provider you have to:Extending Linq To NHibernate
  • 24. Inherit from the base class BaseHqlGeneratorForMethodProvide the list of supported methods (a sort of call signatures)Override the BuildHql() function that will provide the HqlExpression that represent your operator/functionImplement the IRuntimeMethodHqlGeneratorinterface (usefull when you do not know the types involved ‘at design time’).you do not provide a list of function calls to matchImplement the  bool SupportsMethod(MethodInfo method) – states if the method can be represented by the generator.Implement the IHqlGeneratorForMethod GetMethodGenerator(MethodInfo method) – returns a method generator that will be used to generate the HqlExpressionTo create a Method Generator you can:Extending Linq To NHibernate
  • 26. Thank you for attending!