nhibernate from inside

19
NHibernate from inside by Andriy Buday Kiev ALT.NET

Upload: andriy-buday

Post on 23-Jun-2015

2.401 views

Category:

Technology


0 download

DESCRIPTION

I have been working with NHibernate for manner of couple of years and I never seriously thought about how it is built from inside, what is the architecture of NH, how much it depends on ADO.NET, how they made reflection to work fast, how high code quality is, which people worked on it and what was the history of it. The other day, I had chance to start mine own small ORM project and all of this questions raised for me. Thus I will do my best to share everything I discovered in NHibernate surgery.

TRANSCRIPT

Page 1: NHibernate from inside

NHibernate from inside

by Andriy Buday

Kiev ALT.NET

Page 2: NHibernate from inside

NHibernate guts. Why?Because… it is fun… interesting…

http://andriybuday.com | http://kievalt.net

Page 3: NHibernate from inside

NHibernate guts. Why?And not so scary as you could have thought

http://andriybuday.com | http://kievalt.net

Page 4: NHibernate from inside

Outline Q1: If build similar ORM ourselves? Q2: How does it look like at glance?

History Community Code base quality

Q3: Dive deeper? Main interaction classes DEMO 1: Debugging fetch logic DEMO 2: Debugging save logic

http://andriybuday.com | http://kievalt.net

Let see… if guts could be interesting o_O

Page 5: NHibernate from inside

Q1: If build similar ORM ourselves?Lets draw…

http://andriybuday.com | http://kievalt.net

Page 6: NHibernate from inside

Q1: If build similar ORM ourselves?This might be the solution we came up with…

http://andriybuday.com | http://kievalt.net

Even uglier than real cave drawing

Page 7: NHibernate from inside

Q2: How does it look like at glance?History

http://andriybuday.com | http://kievalt.net

Page 8: NHibernate from inside

Q2: How does it look like at glance?Community

http://andriybuday.com | http://kievalt.net

Page 9: NHibernate from inside

Q2: How does it look like at glance?NDepend analysis

http://andriybuday.com | http://kievalt.net

Page 10: NHibernate from inside

Q2: How does it look like at glance?NDepend analysis: Application Metrics

http://andriybuday.com | http://kievalt.net

L. of code : 62,914L. of comment: 37,113Assemblies : 3Namespaces : 90Types : 2,211Methods : 18,335

Fields : 7,205C# source files : 1,437IL instruction : 437,982Classes : 1,866UT coverage: 75.93%

Page 11: NHibernate from inside

Q2: How does it look like at glance?Architecture: entity states

http://andriybuday.com | http://kievalt.net

Page 12: NHibernate from inside

Q2: How does it look like at glance?Architecture: more

http://andriybuday.com | http://kievalt.net

Page 13: NHibernate from inside

Q2: How does it look like at glance?Personal opinion: really well-commented

http://andriybuday.com | http://kievalt.net

/// <summary>/// Get the alias of the entity encapsulated by this criteria instance./// </summary>/// <value>The alias for the encapsulated entity.</value>string Alias { get; }

/// <summary>/// Was the read-only mode explicitly initialized?/// </summary>/// <returns><c>true</c> if the read-only mode was explicitly initialized, otherwise <c>false</c>.</returns>/// <seealso cref="ICriteria.SetReadOnly(bool)" />/// <seealso cref="ICriteria.IsReadOnly" />///bool IsReadOnlyInitialized { get; }

/// <summary>/// Will entities (and proxies) loaded by this Criteria be put in read-only mode?/// </summary>/// <remarks>/// <para>/// If the read-only setting was not initialized, then the value of the session's/// <see cref="ISession.DefaultReadOnly" /> property is returned instead./// </para>/// <para>/// The read-only setting has no impact on entities or proxies returned by the/// Criteria that existed in the session before the Criteria was executed./// </para>/// </remarks>/// <returns>/// <c>true</c> if entities and proxies loaded by the criteria will be put in read-only mode,/// otherwise <c>false</c>./// </returns>/// <seealso cref="ICriteria.SetReadOnly(bool)" />/// <seealso cref="ICriteria.IsReadOnlyInitialized" />bool IsReadOnly { get; }

/// <summary>/// Used to specify that the query results will be a projection (scalar in/// nature). Implicitly specifies the projection result transformer./// </summary>/// <param name="projection">The projection representing the overall "shape" of the/// query results.</param>/// <returns>This instance (for method chaining)</returns>/// <remarks>/// <para>/// The individual components contained within the given <see cref="IProjection"/>/// determines the overall "shape" of the query result./// </para>/// </remarks>ICriteria SetProjection(params IProjection[] projection);

Don’t read code – see how green it is

Page 14: NHibernate from inside

Q2: How does it look like at glance?Personal opinion: a lot of noise in code

http://andriybuday.com | http://kievalt.net

if (cmd.Connection != null){ // make sure the commands connection is the same as the Sessions connection // these can be different when the session is disconnected and then reconnected if (cmd.Connection != sessionConnection) { cmd.Connection = sessionConnection; }}else{ cmd.Connection = sessionConnection;}

Now read code!Don’t you think it can be replaced?cmd.Connection = sessionConnection;

Page 15: NHibernate from inside

Q3: Dive deeper?Lets have some fun with code & debugging…

http://andriybuday.com | http://kievalt.net

Page 16: NHibernate from inside

Q3: Dive deeper?Lets have some fun with code & debugging…

http://andriybuday.com | http://kievalt.net

OverviewDEMO 1: Debugging fetchDEMO 2: Debugging save

Page 17: NHibernate from inside

Questions?

http://andriybuday.com | http://kievalt.net

Ask me something! But easy!

Page 18: NHibernate from inside

Read blog! Follow me! Visit KIEV ALT.NET!

http://andriybuday.com | http://kievalt.net

@andriybuday

http://andriybuday.com/

http://kievalt.net/

[email protected]

Page 19: NHibernate from inside

Links

http://andriybuday.com | http://kievalt.net

My blog: http://andriybuday.com

Kiev ALT.NET: http://kievalt.net/

NHibernate: Home Page: http://nhforge.org/ Source Code: https://github.com/nhibernate/nhibernate-core Screencasts: http://www.summerofnhibernate.com/ Reference: http://www.nhforge.org/doc/nh/en/index.html NDepend analysis:

http://www.ndepend.com/SampleReports/OnNHibernate/NDependReport.html So what’s NH development look like:

http://elegantcode.com/2008/12/07/so-whats-nhibernate-development-like/ Mapping by code: http://fabiomaulo.blogspot.com/2011/04/nhibernate-32-mapping-by-code.html