NhiBernate Source Code Analysis 2: Session Factory

zhaozj2021-02-16  92

The session factory is a key class in NHibernate. It interacts with database connections, database transactions, etc., also stores persistent objects associated with all persistent object types, and the persistence class is the key to persistence, it implements basic CRUD operations.

When the user needs persistent operation, create a session by the session factory for persistent operation.

1. Creation of the session factory

The session factory is implemented by the iSessionFactory interface, created by the CONFIGURATION's buildsessionFactory method, and the session plant should use the Singleton mode.

If you want to access multiple databases, you should build multiple session factories.

// *** configuration.cs ***

Public ISessionFactory BuildSessionFactory () {

// ...

Hashtable Copy = new hashtable ();

Foreach (DictionaryEntry de in proties) {

Copy.Add (de.key, de.value);

}

Return New SessionFactoryImpl (this, copy, interceptor);

}

Where sessionFactoryIMPL is class that implements ISessionFactory, this class is modified to Internal.

2. Creation of persistence classes

Persistence class is used to persist in persistent objects, each of which has a persistent object associated with it.

The persistence class inherits from the ICLASSPERSISTER interface, which defines the CRUD operation for persistent objects.

// *** sessionFactoryImpl ***

Private iDictionary classpersisters;

Persistent object collection, key is the type of persistent object;

Private iDictionary classpersistersbyname;

Persistent object collection, key is the class name of lasting objects;

Public sessionFactoryImpl (Configuration CFG, IDictionary Properties, IINTERCEPTOR Interceptor) {

// ...

Foreach (PersistentClass Model In CFG.CLASSMAPPINGS) {

System.Type PersisterClass = Model.Persister;

IclassPersister CP;

// Todo: H2.0.3 CREATED a PERSISTERFACTORY

IF (persisterclass == null || persisterclass == typeof (entityPersister)) {

CP = New EntityPersister (Model, this);

} else if (personisterclass == typeof (normalizedentitypersister) {

CP = New NormalizedentityPersister (Model, this);

} else {

CP = InstantiatePersister (PersisterClass, Model);

}

ClassPersisters [Model.PersistentClazz] = CP;

ClassPersiName [model.name] = CP;

}

// ...

}

Traverse all persistent class mapping information in the constructor, then establish a persistent object according to the persistent type of persistent class, and add this object to the collection.

InstantiatePersister is used to create a custom persistent object, specified by the Persister property of the Class node in the mapping file, the custom persistence class must implement the IClassPersister interface. 3. Connection provider

The connection provider is implemented by the IConnectionProvider interface, and the session factory interacts with a persistent mechanism (database, etc.) through the connection provider, such as obtaining a database connection.

// *** sessionFactoryImpl.cs ***

Public sessionFactoryImpl (Configuration CFG, IDictionary Properties, IINTERCEPTOR Interceptor) {

// ...

ConnectionProvider = ConnectionProviderFactory.NewConnectionProvider (Properties);

// ...

}

Or in the constructor, the connection provider is created by the connection provider plant based on the configuration attribute.

// *** ConnectionProviderFactory ***

Public Static iconnectionProvider NewConnectionProvider (iDictionary settings) {

IConnectionProvider Connections = NULL;

String providerclass = settings [cfg.environment.ConnectionProvider] as string;

IF (providerclass! = null) {

Try {

Connections = (iConnectionProvider) Activator.createInstance (System.Type.gettype (ProviderClass);

}

Catch (Exception E) {

Throw New HibernateException ("COULD NOT Instantiate Connection Provider:" Providerclass);

}

}

Else {

Throw New NotimplementException ("We Have Not Implement User Supplied Connections Yet.");

}

Connections.configure (settings);

Return Connections;

}

The NewConnectionProvider method creates a connection provider by configuring the value of ConnectionProvider. The current version (V0.0.5) uniquely available providers have only DriverConnectionProvider classes.

// Part of content, etc.

转载请注明原文地址:https://www.9cbs.com/read-12880.html

New Post(0)