Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the NHibernate structure in C #

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge about "what is the NHibernate structure in C#". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

1. NHibernate structure

Let's start with a picture for our friends:

This is the overall structure diagram of NHibernate. NHibernate establishes a connection to the database through ADO. NET and encapsulates a Transaction factory and a Session factory. At each operation, the corresponding Session/Transaction sample operation data object is obtained through two factories.

ISessionFactory - NHibernate.ISessionFactory:

A compiled map cache based on a single database that is persistent and thread-safe. Is a factory class that provides ISession and is also a client for IConnectionProvider. You can set up a secondary cache at the process level or cluster level between transactions.

ISession - NHibernate.ISession:

A single-threaded, short-life object that represents a connection between slave applications and data persistence. An ADO. NET connection wrapper that provides ITransaction factories. Provides a first-level cache for retrieving objects through primary keys and navigating links while querying objects. This is the navigation attribute in EF Core.

Persistent Objects and Collections:

Single-threaded, short-life objects that contain persistent state and business methods. They may just be ordinary POCOs, simply associated with ISession. As long as ISession is turned off, these objects can be detached and used anywhere in the application layer.

Temporary Objects and Collections:

Represents temporary persistent objects that are not hosted by ISession and are temporarily created by the application layer but will not be persisted until ISession is closed.

ITransaction - NHibernate.ITransaction:

This is optional. Represents a single-threaded, short-life object used by an application to constrain an atomic unit of work, based on ADO. NET's Transaction abstraction. An ISession may open multiple transactions, and Transaction scopes may be used instead.

IConnectionProvider - NHibernate.Connection.IConnectionProvider:

Also optional, is a factory for creating ADO. NET Connections and Commands. Based on DbConnection and DbCommand implementations, it is not directly exposed to applications, but can be extended or implemented by developers.

IDriver -NHibernate.Driver.IDriver:

Optional, driver interface to encapsulate differences between different ADO. NET data providers. For example: parameterization, etc.

ITransactionFactory - NHibernate.Transaction.ITransactionFactory:

Optional, transaction implementation factory, not exposed to applications, but developers can extend or implement it.

2. instance state

In NHibernate, a persistent object has three different states depending on its relationship to the persistence context, where ISession is a persistence context. There are three states:

transient Temporary objects in this state have not been captured by the persistence context, simply they have just been created, and the primary key information has not been obtained from the database/persistence context.

persistent The persistent object representation of this state has been correctly captured by the context, and the persistent context can monitor changes to the object. The persistence context holds a reference to the object. This state is usually obtained from the database or newly created data appended to the context.

detached The object in this state is detached from its context, has a database primary key, and has or still has a database record corresponding to it. This could be because the context is closed; the object is persisted in another context, and it is free for the current context.

3. Introduction to configuration items

Common configurations in NHibernate

dialect

Database dialect, indicating what the database connected by NHibernate is, and which format should be used to parse the relationship mapped to the database SQL statement

default_schema

Default schema, used to set the default schema of the database connected by the connection string.

connection.provider

Provider for database connections, default is NHibernate.Connection.DriverConnectionProvider. Fill in implementation classes inherited from IConnectionProvider

connection.connection_string

database connection string

connection.connection_string_name

The name of the database connection string that is configured for the connectionStrings node in the program's configuration file.

max_fetch_depth

Maximum recursion depth, indicating the depth of navigation attributes loaded directly in a query. The default is not to load navigation properties directly, based on the logic of delayed loading, and load data from the database only when actually used.

show_sql

Whether to print the converted SQL statement in the console is generally set to true during debugging to confirm whether the generated SQL is correct.

hbm2ddl.auto

This value indicates whether DDL statements are automatically generated and submitted to the database for execution each time ISessionFactory is created. The default is blank, which means the database will not be forced to update. There are several candidate values: create or create-drop, update, etc. where create means only new ones are created each time;create-drop means tables are created each time ISessionFactory is created and deleted when ISessionFactory is closed; update means DDL SQL is updated to the database each time. (I remember there was update, but there was no option in the documentation)

"What is the NHibernate structure in C#" is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report