NPersistence - .NET Persistence API

.NET Persistence API

The .NET Persistence API, is a persistence and Object Relational Mapping (ORM) specification for the .NET framework.

The specification contains the classes and interfaces that define the contracts between client applications which require persistence and ORM services and the vendors which provide them.

.NET Persistence API is the .NET equivalent for the java JPA framework and is based on it. It offers the same standard for managing relational data in applications using the .NET framework written in C#, Visual Basic and other .NET languages.

 

Features


.NET Persisrence API offers a veriaty of features that covers every aspect of ORM and persistence. The interface is very easy to understand and maintain. Some of the features that are offered are:

Object-Relational mapping using annotations

The persistent classes are POCO classes which are annotated by custom NPersistence annotations providing the information required for mapping the entity. An example mapped class:

using NPersistence;
[Entity]
public class Employee
{
    [Id]
    public virtual string ID { get; set; }
    [Basic]
    public virtual string FirstName { get; set; }
    [Basic]
    public virtual string LastName { get; set}
}

.NET Persistence API supports for the following persistence asspects:

  • Transaction management
  • Saving entities
  • Retrieving entities
  • Deleting entities

All these services are provided by the ‘EntityManager’ interface which holds APIs for these purposes.

configuration

A central configuration location using an XML file called 'persistence.xml' that can be placed anywhere that is reachable in the classpath. An example configuration file:

  <persistence-unit name="testPersistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>NPersistence.NHibernate.NHibernatePersistence</provider>
    <class>Employee</class>
    <properties>
      <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
      <property name="connection.connection_string">server=localhost; user id=user1; password=pass1; database=test2</property>
      <property name="dialect">NHibernate.Dialect.MySQLDialect</property>
      <property name="connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
    </properties>
  </persistence-unit>

Background


Mapping between classes and DB tables has always been a necessity for applications which manipulate and persist data. In the past, each application created its own ORM layer in which the application's specific classes where mapped to specific tables in the DB using custom SQL statements.

Object-relational mapping (ORM) The next step in the evolution of ORM was the creation of middleware layer applications that can dynamically map classes to relational databases given a description of the classes (entities) and relationships between them. These applications dynamically generate SQL for loading and storing the described objects. Such applications are: • EJB entity beans • Hibernate • Microsoft ADO Hibernate is a JAVA framework which was ported to C# and used in the .NET platform under the name 'NHibernate'.

In addition, other applications bypass the ORM problem completely and offer persisting entities into object oriented databases avoiding the overhead of the relational DB. Such an application is ObjectDB which is an object oriented DB that also implements the JPA spec, therefore providing a complete persistence solution without the need of combining a database with an ORM provider.

JAVA Persistence API (JPA) At this stage, when multiple vendors and technologies offered the same purpose of ORM middleware, a portability problem arises.

Since each vendor has his own set of API’s, an application that uses a particular vendor is now tightly linked to him. Changing the ORM layer results in a lot of coding work for the application, a hard work which results is tight linkage to another ORM vendor.

In order to untie the link between the applications and ORM vendors, the ORM technology had to be standardized – resulting in JPA. JPA offers a set of standard APIs with which an application can meet all its persistence needs.

Since JPA is just a set of interfaces, it requires an implementation by ORM vendors.

JPA implementers

Many of the different ORM vendors implemented JPA with their own technologies. Now, applications using JPA can easily switch JPA implementations with zero effort. Some of the vendors that offer implementations for JPA are: • Hibernate • OpenJPA • Toplink Essentials • ObjectDB

JPA and all its implementations belong to the java world. No equivalent for JPA existed in .NET world until NPersistence was released.

 

.NET Persistence API implementations

The first implementation of the .NET Persistence API is the NHibernate-NPA. It is an implementatation of the well known NHibernate application. Work on other implementations is on going and will be released shortly.