Showing posts with label NHibernate. Show all posts
Showing posts with label NHibernate. Show all posts

Sunday, October 4, 2009

NH Profiler – Using Filters to find the needle in the haystack

The allows a developer to analyze the behaviors between the domain and data layer more effectively. If you are using NHibernate I think this piece of software should be in your arsenal of tools. Oh, and for you Java folks it works against Hibernate. image

Session Filtering

Before I start the analysis notice to the left we have an unfiltered view of our current sessions. If you look a bit closer, you’ll notice that the recent statements list contains some added noise. To remedy this we will apply a filter to all session statements containing a url path for image,css and JavaScript files.

Locate the Filter component, in the upper right window of NH Profiler. imageBy default filtering is Inactive. So lets define one custom to our session!

  1. Click on the filter icon and in the dropdown list of the Edit Filter window choose filtering for  “Sessions by URL.
  2. Click the Add button. image
  3. Choose the not containing operator.
  4. Enter the text for filtering. In my example I want to filter URL’s being sourced with the text '/Assets/’. 
  5. Click apply.

 

And voila! we now have a more concise session list.image

When the analysis is complex try to make it simpler with filters! In a future discussion we will look into how NH Prof can diff sessions and what insight we can gain from doing this.

Sunday, August 30, 2009

S#arp Architecture – Part 1: Implementing the M:M mapping override

User Story

Should be able to add a user to more than one role. Should be able to remove the user from specific roles.

Convention over Configuration

This topic is garnering quite a bit of interest of late in the .Net community. James Kovacs recently appeared on an episode of rocks discussing Convention over Configuration. He spent a fair amount of time discussing the move from XML configuration for NHibernate by using the conventions of Fluent NHibernate. By default S#arp Architecture implements the auto-mapping convention. This means that you only need to alter the convention for various edge cases, one of those being M:M associations. Knowing in advance that you will need to perform the override is handy hence the reason for today’s discussion.

Review of the Model

I have the following two entities forming a many-to-many relationship.

Mapping Override using Class Auto Mapping

I created a sub-folder within my data assembly naming it NHibernateMaps and then proceeded to add the following two classes within it. I added using statements for Fluent NHibernate’s AutoMap and AutoMap Alteration namespaces. The key mapping attributes below for both sides of the relationship are the WithParentKeyColumn and WithChildKeyColumn values.

public class AppUserMap : IAutoMappingOverride<AppUser>
{
public void Override(AutoMap<AppUser> mapping)
{
mapping.Id(x => x.Id, "AppUserID")
.WithUnsavedValue(0)
.GeneratedBy.Identity();

mapping.WithTable("AppUsers");
mapping.SetAttribute("lazy", "false");
mapping.Map(x => x.LoginName).WithLengthOf(50);
mapping.Map(x => x.Password).WithLengthOf(255);
mapping.HasManyToMany(x => x.Roles)
.WithTableName("AppUserRoles")
.WithParentKeyColumn("AppUserID")
.WithChildKeyColumn("RoleID")
.AsBag();
}
}
public class RoleMap : IAutoMappingOverride<Role>
{
public void Override(AutoMap<Role> mapping)
{
mapping.Id(x => x.Id, "RoleID")
.WithUnsavedValue(0)
.GeneratedBy.Identity();

mapping.Map(x => x.Name, "RoleName");
mapping.SetAttribute("lazy","false");
mapping.HasManyToMany<AppUser>(x => x.AppUsers)
.WithTableName("AppUserRoles")
.Inverse()
.WithParentKeyColumn("RoleID")
.WithChildKeyColumn("AppUserID")
.AsBag();
}
}

The choice of using a Bag, List, Set, Map or array structure to handle the transient collection in memory depends on the context of the requirement at hand. In our domain model above we are representing the User and Role associations as an IList structure that is equivalent to NHibernate’s IBag object and therefore we set the mapping to use a Bag. who blogs over at CodeBetter.com discusses this point in the Relationships section in Part 6 of a great series of articles that formed his book called .

In the next article I hope to implement the same override but by using the Conventional approach. Therefore there will not be any need to use any mapping classes. I will wrap up the series by indicating the additional changes needed in the UI and controller layers.

Saturday, August 29, 2009

S#arp Architecture - Architectural review

Overview

For the past 6 months or so I have been using and following closely the framework’s evolution from beta to release. During this time I have found the project, community and reference documentation excellent! and the team of contributors have done a professional job assembling this framework into a key source of guidance on how to assemble an enterprise architecture that embraces (from its Project main page on Google code) :

  • Loose coupling leveraging Microsoft’s ASP.Net MVC
  • Persistence ignorance with NHibernate
  • Domain Driven Design
  • Pre-configured infrastructure

S#arp Architecture includes S#arp Scaffolding that greatly speeds up the process of adding CRUD functionality for your entities through the use of T4 templates implemented with the T4 Template toolkit. The “out-of-the box” templates can be extended adding support for using the JavaScript library. It also includes a Visual Studio project template to build out your Solution tree. Billy has a good on the topic of extending the T4 template to embrace the EXT-JS library.He frequently responds to questions that come into the Google group discussion forum linked at the bottom of this post. has done several videos on getting started and extending the architecture. Others of note who contribute with insightful comments and blog entries that have helped my understanding are .

What follows will be a series of blog posts documenting the ability to override the default generated templates and code to produce the desired result of managing the roles for various users accessing an MVC website in the context of varying controller actions.

The posts will be as follows:

Introducing the model and overriding Fluent NHibernate’s auto mapping strategy within S#arp Architecture

Links

Wednesday, July 22, 2009

VAN: Lessons learned building NH Profiler with Ayende Rahien, Christopher Bennage and Rob Eisenberg Oct 21 and Oct 28, 2009

Topic

A three way conversation with the main collaborators who created NH Profiler. This tool enables developers to gain a deeper insight into profiling their applications communication from NHibernate(.Net) and Hibernate(Java) through to the database.

Who they are

contributed efforts on the back-end development.

contributed their efforts to the front-end.

Time and location of the meetings

Times below are Central Daylight Time
Start Time: Oct 21 and 28, Each week 8:00 PM UTC/GMT -5 hours
End Time: Oct 21 and 28, Each week 10:00 PM UTC/GMT -5 hours
Attendee URL: Attend the meeting (Live Meeting)

Sunday, March 15, 2009

Solving slow parameterized query plans with SQL Server

This morning I was listening to Stack Overflow podcast#45 in which Jeff Atwood indicated he had uncovered situations involving poor performing parametrized query's. The solution involves optimizing for 'UNKNOWN' as an optional hint when dealing with parametrized queries.

Example:

@p1=1, @p2=9998,

Select * from t where col > @p1 or col2 > @p2 order by col1

option (OPTIMIZE FOR (@p1 UNKNOWN, @p2 UNKNOWN))

This optional optimization is available for SQL 2008 only. The option forces the query optimizer to look at all available statistical data to come up with a more intelligent deterministic view of what values the local variables used to generate the query plan should equate to rather than using the parameters being passed in by the application. The workaround alternatives do not really offer any good alternatives to solve this issue most notably when dealing with dynamic parameters.

Workarounds

  1. Recompile every time the query is executed using the RECOMPILE hint - This can be very CPU intensive and effectively eliminates the benefits of caching query plans. ex. option(RECOMPILE)

  2. Un-parametrize the query – Not a viable option in most cases due to SQL injection risk.

  3. Hint with specific parameters using the OPTIMIZE FOR hint (However, what value(s) should the app developer use?) This is a great option if the values in the rows are static, that is; not growing in number, etc. – However in my case the rows were not static.

  4. Forcing the use of a specific index

  5. Use a plan guide – Using any of the recommendations above.

Implications with NHibernate or other Object Relational Mappers's

I am a user of NHibernate. At present NHibernate does not provide support for the SQL 2008 dialect and recommends using the SQL 2005 dialect configuration option to deal with SQL 2008 data sources. I am wondering if anyone in the NHibernate community has come across this issue with slow parametrized SQL? Is this an issue that an ORM needs to be aware of when supporting a given database dialect? My view is yes. However I am still somewhat of a newb with NHibernate.