Navigation

Search

Categories

On this page

Using NHibernate Profiler with database integration tests.
Behaviour Driven Database Development (BDDD)
Scaffolding with ASP.Net
SQL Dependency Tracker
Working with the Object Data Source
Timers and synchronization
SQL Server 2005 Upgrade Advisor
Writing Provider-Independent Data Access Code with ADO.NET 2.0

Archive

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 149
This Year: 2
This Month: 0
This Week: 0
Comments: 43

Sign In
Pick a theme:

# Wednesday, June 10, 2009
Wednesday, June 10, 2009 6:28:07 PM (GMT Daylight Time, UTC+01:00) ( .Net Data | Agile | NHibernate )

The NHibernate Profiler is a pretty cool tool. If you are using NHibernate and want to see how your application/website uses NHibernate this is what you need.

But, why wait till you use the whole application to start using the profiler. Why not use it when writing  writing tests , while building your mappings and when building your NHibernate queries. It’s really easy to do this.

First, get the NHibernate profiler from www.nhprof.com. Extract the contents to a location. From your test project, add a reference to HibernatingRhinos.NHibernate.Profiler.Appender.dll in the NHProf package.

This is my small test class.

 [TestFixture]
    public class EpisodeTests : BaseTest
    {
        [SetUp]
        public override void SetUp()
        {
            base.SetUp();
            ActiveRecordStarter.Initialize(typeof(Episode).Assembly, new XmlConfigurationSource("activerecord.xml"));
        }

        [Test]
        public void CanPersistTitle()
        {
            Episode episode = new Episode();
            string expectedTitle = "Dr Who and the Daleks";
            episode.Title = expectedTitle;
            episode.Save();
           
            Guid savedEpisodeId = episode.Id;
            episode = ActiveRecordBase<Episode>.Find(savedEpisodeId);

            Assert.That(episode.Title, Is.EqualTo(expectedTitle));

        }
    }

 

I’m using Castle ActiveRecord (which uses NHibernate behind the scenes) to demonstrate this. To enable NHProf to profile the test, add a static constructor to the super class , BaseTest. In the static constructor, add the following code.

HibernatingRhinos.NHibernate.Profiler.Appender.NHibernateProfiler.Initialize();

Go to where you extracted NHProf and run the profiler exe. Run the test and watch the magic.

image

NHProf is now profiling your integration tests. You can leave this running in the background while you are working on the tests.

You get immediate feedback NHibernate best practice violations, and you can fix them while writing tests. You don’t have to wait till deployment to profile your code. Although this doesn’t give the whole picture of how your application is using NHibernate , you still can fix many things early. I highly recommend running NHProf while running automated acceptance test scenarios.

Technorati Tags: ,

 

Comments [0] | | # 
# Saturday, December 27, 2008
Saturday, December 27, 2008 11:20:26 AM (GMT Standard Time, UTC+00:00) ( .Net Data | Agile )

Pramod Sadalage, has written an article on Behaviour Driven DB development. http://www.methodsandtools.com/archive/archive.php?id=78.

He writes about adding behaviour to the domain model and how that behaviour is translates into database objects, with tests. In the project I'm working on, which driven by a Web UI I use the UI behaviour to build the presenter and  through this add behaviour to the domain model. It's at the last stage that I add the persistance, through Nhibernate.

Comments [0] | | # 
# Thursday, January 24, 2008
Thursday, January 24, 2008 9:53:00 PM (GMT Standard Time, UTC+00:00) ( .Net Data | .Net Web )

Many web applications have a back end "admin" pages to manage tables in the database.  These pages exist solely to perform CRUD operations against a single table. Sometimes with a bit of validation.

It is tedious work if you have to create and write the same code  for each table and to maintain them. To make this task somewhat easier, there are solutions that look at the database structure, and generate the data access code and the asp.net pages.

The latest and greatest of them is ASP .Net Dynamic data. This uses LINQ to SQL for data access. Its quite easy to get a site up and running using this.

Mono Rail has an ActiveRecord scaffold.  This uses Castle ActiveRecord and Nhibernate to generate the scaffold. However, this doesn't generate the database to object model mapping automatically.  Classes that inherent from an ActiveRecord base have to be created first. Fields and relationships have to be defined in code. When this is done its all a breeze. Its a good way to get to grips with MonoRail.

 

Subsonic which I'm using on a project now, has two methods of operation. It has a set of scaffold controls which you can use on any asp.net pages to get CRUD functionality, and it also has the option to generate the asp.net pages and the data classes. The data classes generated by subsonic use the active record pattern too. The code generated by subsonic can be modified if you need to do so. After the code is generated these are normal asp.net pages to which you can add validation, combine with master pages etc..

Subsonic and Asp.Net dynamic data offer the quickest way to get up and running.  Subsonic and MonoRail offer the possibility to extend the application when things get more complex. The data classes generated can be reused when the application grows beyond a simple prototype or admin section.

Comments [0] | | # 
# Tuesday, June 26, 2007
Tuesday, June 26, 2007 10:31:11 AM (GMT Daylight Time, UTC+01:00) ( .Net Data )

I was playing with Redgate's SQL Dependency tracker yesterday. The balloon tree view sort of shows how modular the db design is. I'm working on a project where I'm implementing "pluggable" modules of functionality.

The two circular clusters at the top left corner are the db objects for the module I'm working on. The view shows how the cluster relates to the ASP .Net membership provider tables. The clusters which go diagonally across the view are the aspnet db objects.

image

Comments [0] | | # 
# Saturday, November 25, 2006
Saturday, November 25, 2006 1:33:32 PM (GMT Standard Time, UTC+00:00) ( .Net Data | .Net Web )

The Object Data Source (ODS) helps you expose your business objects, to provide data to databound controls such as the GridView, DetailsView and many other data bound controls.

Compared to the other data sources like the DataTable and DataSet, the ODS does require some work to achieve the same functionality. The main advantage of the ODS is that it allows you to maintain your layers, without polluting your presentation layer with data code.

To start off,  drag and drop the ODS control to a web form. Set the TypeName property to the business object you want to work with. For example, if we have a BO named Employees in the TimeTracker namespace, this will be TimeTracker.Employee.

Selecting
 A common scenario is to display the a list of records. Say we want to display the list of all employees in a GridView. To retrieve the records, the Employee object needs a GetEmployees static method that will retrieve  all the employees from the DB.  Set the SelectMethod property of the ODS to GetEmployees. Now bind the ODS to the GridView just like any other data source.

At runtime the ODS uses reflection to find the GetEmployees method and invokes it.

Paging and filtering

Filtering Employee objects is as easy as adding a new method to your Employee object. If we want to select Employees by department, the Employee object should have a static method

GetEmployees(string department)

In the properties window of the ODS, add the department parameter to the SelectParameters collection. You can set a default value to it, and this is where things get real easy. You can bind the parameter to a control on your web form ( like a DropDownList) , a Session variable and even a query string parameter.  Adding  filtering functionality is simple as that. You can do the same programmatically by adding Parameters to the SelectParameters collection. Be sure to clear the collection before you add a parameter( or check if the parameter exists).

Paging is where working with the ODS gets a bit complicated. To enable paging ( in conjunction with the GridView) set the EnablePaging property to true. The Employee object too, needs to have methods to support paging.

When paging is enabled, the ODS calls the GetEmployee method with two extra parameters. by default the parameters are startRowIndex and maximumRows. These can be changed by setting the StartRowIndexParameterName and MaximumRowsParameterName properties.

Now we need a GetEmployees method with the signature like
GetEmployees(int startRowIndex,int maxiumRows)
To support filtering it will have to be GetEmployees(string department ,int startRowIndex,int maxiumRows)

The maximumRows parameter, will have the value of how many rows to display per page. This will have the value of the PageSize property of the GridView. The startRowIndex will contain the the index of the current page. This can be the starting row index of the current page of records. See here for more on how to pass these on to an SQL query.

In addition to this the ODS needs another method that is set by the SelectCountMethod property. This is invoked to find out the total number of rows available. So we need another static method in our Employee object. GetEmployeesCount() .If the total number of rows is 100, and you have a page size of 20. The GetEmployees count method should return 100, so that the ODS can tell the GridView, how many pager links to display. I usually return this as an out parameter from the stored procedure that retrieves the paged results.

Inserting, Editing and Deleting

Inserting, editing and deleting is pretty easy. All you will again is to have the appropriate static methods in the Employee object.
AddEmployee(Employee e)
UpdateEmployee(Employee e)
DeleteEmployee(Emplyee e).

Set the InsertMethod , UpdateMethod and DeleteMethod properties accordingly. All these method can be configured with parameters like the SelectMethod. However, setting the DataObjectTypeName property to the Employee object will reduce a lot of the hassle by passing Employee objects to the Add,Update and DeleteMethods.

Comments [0] | | # 
# Wednesday, December 28, 2005
Wednesday, December 28, 2005 10:36:32 PM (GMT Standard Time, UTC+00:00) ( .Net Data | .Net UI )

While working with the System.Timers.Timer class last week, I found that the timer_elapsed method is not synchronized, i.e the event is fired without waiting for the completion of the previous invocation.

Say for example you have a method that pulls up the latest records from a table and do some processing. The timer event can fire while you are doing this processing and have still not marked the records as processed. The safe way is to wrap the code in the timer_elapsed event inside a lock statement.

Comments [0] | | # 
# Thursday, July 28, 2005
Thursday, July 28, 2005 9:01:04 AM (GMT Daylight Time, UTC+01:00) ( .Net Data )

The first step to migrate your SQL Server

Download here

"The Microsoft SQL Server 2005 Upgrade Advisor is a tool used by Database developers and administrators to analyze SQL Server 7.0 and SQL Server 2000 database servers in preparation for upgrading to SQL Server 2005. The Upgrade Advisor will allow users to analyze the configuration of their existing database services and database applications. As a result of this analysis, Upgrade Advisor will provide reports that identify deprecated features and necessary configuration changes that will impact their Database upgrade process. Upgrade Advisor will also provide links to documentation that describe these changes and necessary steps to complete the process."

 

Comments [0] | | # 
# Tuesday, March 22, 2005
Tuesday, March 22, 2005 11:15:57 PM (GMT Standard Time, UTC+00:00) ( .Net Data )

http://www.devx.com/dotnet/Article/27297

Now if only they would release it !!.

The release date has slipped to the end of this year. I wish they would just release the runtime sooner or even backport some of the enhancements and bug fixes.

Comments [0] | | #