Open Source Frameworks
Download
Report
Transcript Open Source Frameworks
.NET Database Technologies:
Open-Source Frameworks
Looking beyond Microsoft
• Many third-party ORM tools and frameworks are available
for .NET and for other languages/platforms
• High level of maturity in the Java world in particular
• Open-source projects and commercial products
• We will look at two (closely-related) open-source
frameworks which implement different patterns
Castle ActiveRecord
NHibernate
Active Record
• An object that wraps a row in a database table or view,
encapsulates the database access, and adds domain logic
on that data
• Implemented by Castle ActiveRecord
• Part of Castle Project, which also includes Windsor,
Dynamic Proxy, etc.
• Built on top of NHibernate
ActiveRecordBase
• Active record pattern
instance properties representing a record in the database
instance methods acting on that specific record
static methods acting on all records
• ActiveRecord implements this through ActiveRecordBase
class
• All persistent classes inherit from it
• ActiveRecordBase implements a basic set of storage and
retrieval operations, e.g. Create, FindAll, Save, Update
• Persistent classes can implement additional operations
ActiveRecord mapping
• Active record pattern assumes a fairly direct
correspondence between objects and database
• “an object that wraps a row in a database table”
• Castle ActiveRecord is a bit more flexible than that basic
description of pattern implies
• Makes use of capabilities of Nhibernate
• Offers a subset of NHibernate mapping functionality,
including inheritance mapping
• Mapping is done using class, fields and property level
attributes
• ActiveRecord is able to infer table and column names if
you omit them
Convention over configuration
ActiveRecord queries
• Persistent classes can retrieve data and populate objects
in the following ways:
Calling methods of base class
Calling methods of base class and using NHibernate criteria
Using SimpleQuery class to execute HQL queries (Hibernate
Query Language)
Native SQL queries (through NHibernate)
LINQ(?)
Another pattern – Query Object
• An object that represents a database query.
• A Query Object is a structure of objects, including
criteria, that can form itself into a SQL query
• You can create this query by referring to classes and fields
rather than tables and columns
• In this way those who write the queries can do so
independently of the database schema and changes to the
schema can be localized in a single place
ActiveRecord and PI
• Not a chance!
• ActiveRecord is intended to be simple to use, without the
need to learn NHibernate mapping schema
• Favours convention over configuration (this is not
specifically a property of the Active Record pattern)
• Suitable for Rapid Application Development rather than
Domain Driven Development
Not just .NET - Ruby on Rails
• Probably the best known Active Record implementation
• Works with the Ruby language
• Persistent classes inherit from ActiveRecord::Base class
• Rails also implements MVC and favours convention over
configuration throughout
• Other implementations exist for PHP, Java, etc.
Data Mapper/Metadata Mapping - NHibernate
• The Data Mapper is a layer of software that separates the
in-memory objects from the database
• A Metadata Mapping allows developers to define the
mappings in a simple tabular form, which can then be
processed by generic code to carry out the details of
reading, inserting, and updating the data
• NHibernate is an ORM which implements these patterns
• Relatively mature technology, ported from Hibernate
which is long established for Java
NHibernate Unit of Work
• Session is roughly equivalent to EF’s ObjectContext
• Implements Unit of Work and Identity Map patterns
• Session instance is created by a SessionFactory object
which is built by reading configuration file
• Session opens a Transaction in which to do the work
• Common practice to wrap Session in a custom Unit of
Work at a higher level of abstraction
NHibernate mapping
• Mappings are defined in XML files or by annotations
• Very powerful and flexible mapping capabilities
• Detailed control of loading strategies, etc.
• Can map select, update, etc to stored queries
• Configuration (database connection, etc) also defined in
XML files
NHibernate queries
• NHibernate can retrieve data and populate objects in the
following ways (among others):
HQL queries
Criteria queries
Native SQL queries
LINQ
Named queries (HQL or SQL) defined in mapping
NHibernate and PI
• NHibernate persistent classes are POCOs
• Good adherence to PI principle
• Favoured by adherents of DDD
Fluent NHibernate
• Uses Fluent API to define NHibernate configurations and
mappings in code
• Type-safe, no XML
• Similar to EF Code-First mappings (actually, it’s the other
way round)
• Favours convention over configuration
Fluent APIs
• Term first coined by Evans and Fowler
• A fluent interface is normally implemented by using
method chaining to relay the instruction context of a
subsequent call
• The API is primarily designed to be readable and to flow
• Can be detrimental to debugging, as a fluent chain
constitutes a single statement in which debuggers may not
allow setting up intermediate breakpoints
• Becoming common for applications such as defining
mappings, configurations and query objects (e.g. LINQ
extension methods syntax)
ORMs and performance
• Using any ORM can be expected to have an impact on
performance compared to ADO.NET
Overhead of extra layer on top of ADO.NET
Select N+1 problem
Sub-optimal SQL may be generated
• Hard to find reliable data to base decisions on
Some academic research (van Zyl, Cvetkovic)
Benchmarks
•
ORMeter (http://ometer.net)
•
PolePosition (http://www.polepos.org)
Opinions and experience
• Weigh benefits against performance hit
Optimising ORM performance
• SQL Profiling
• Careful consideration of fetching/loading strategy within
the application
• Careful design of mapping where possible
• Using stored procedures where appropriate
• Compiled queries and query-plan caching (LINQ)
• Pre-Build Store Views (EF)
• Tracking vs. No Tracking (EF)
• 2nd level caching (NHibernate)
• etc...