Just sharing some of my inconsequential lunch conversations with you... RSS  

Monday, August 06, 2007

Interception NHibernate activity

We intercept NHibernate activity for the following reasons:

  • logging
  • authorization validation
  • generic validation
  • trigger events
The intercepting mechanism is a generic one, where we deliver each of the life cycle triggers to the super class all domain model classes derive from. On this implementation we've just derived this parent of all classes from NHibernate.IInterceptor as virtual methods with empty implementations - other than the generic logging. For example:


public bool OnFlushDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, NHibernate.Type.IType[] types)
{
Model.EntityObject entityObject = entity as EntityObject;

if (entityObject == null)
{
return false;
}

return entityObject.OnFlushDirty(entity, id, currentState, previousState, propertyNames, types);
}



The way we hooked generic logging is quiet simple: we've intercepted OnFlushDirty and are just logging object[] currentState.

The entity update upon trigger is not immediate. Other than just setting properties on parameter entity, we have to set currentstate:

currentState[(int)propertyNames["MyProperty"]] = ...;

There goes the strongly typed advantage...

Sunday, August 05, 2007

Entity Life-Cycle in NHibernate: IInterceptor Interface

Here's a great little article about NHibernate Entity Life-Cycle.

[update] Here's another one about session management in general.

Paradox: are closed options the best options?

I'm currently in the process of shutting down my personal datacenter. It has served me well, but it is too costly on power and ecologically unacceptable. Oh, and it also a bit noisy.

I had a bunch of servers with a bunch of virtual machines serving a bunch of my personal sites, through an expensive domain I was paying to NetworkSolutions served by ZoneEdit dynamic DNS free service. I'm moving all of this to GoDaddy Deluxe hosting. Not as fun to administer nor flexible, but dam cheaper.

One of the decisions I had to make was for the OS. I chose Windows, and let me explain why.

I'd like to keep my old Mambo/Joomla sites, and a bunch of little projects I've done when there was no match for PHP to an old C++ programmer, but still wanted to experiment serving personal ASP.NET sites (ASP.NET sites are my way of living, but I've never did it with my personal sites for lack of licenses).

So the obvious answer was Windows, because of Microsoft's closed model. I can run PHP and mySQL on Windows, but I can't run (decent) ASP.NET on Linux.

It isn't the only example of closed option as the best one. Look at the Apple closed model: we can run Windows and OS X on Apple hardware, but cannot run OS X on non Apple machines. Again the closed model has the upper hand.

I know, this isn't fair, but that the way it is.

[update] I've just found out that the Windows account at GoDaddy doesn't support PHP - only mySQL. Due to the Metropolis low Microsoft support, and because I travel with some project baggage, I'm now trying to change my account back to Linux. The world may not be as unfair to Linux as I supposed... Only unfair to those who refuse to choose to stay only on one side of the fence...

Thursday, August 02, 2007

CodeSmith 4.1 is now available

I've been a long time user of CodeSmith - where I chose to port my old generaters to, and I'm still using it with NetTiers and with my NHibernate mappers. The latest version supports:

* Microsoft Visual Studio 2008 "Orcas" Support - CodeSmith has been updated to support the latest version of Visual Studio codenamed "Orcas"

* Linq to Sql templates - Generates LINQ classes as well as manager classes to make it easier to execute common queries, manage validation, and add business rules. It's "SQL Metal" on steroids.

Some considerations:
  • Good: CodeSmith is very active and responsive to market changes;
  • Bad: pricing is not what it used to be;
  • Undefined: does CodeSmith still makes sense for new projects? Targeting SQLMetal doesn't seem enough for surviving... The LINQ to SQL and LINQ to Entities designer makes SQLMetal too poor to use other than in demos.
And now that we are taking about it, what about GAT/GAX implementations other than the P&P ones? Where are them?

Drop-down list versus combo box

from wikipedia:

A combo box is a user interface control GUI element. It is a combination of a drop-down list or list box and a single-line textbox, allowing the user either to type a value directly into the control or choose from the list of existing options. An example of this use is the address bar of graphical web browsers.

But:

The term "combo box" is sometimes used to mean the same thing as "drop-down list," owing to the variability of terms. This usage is likely the result of Visual Basic, and later the Visual Studio suite using the term "combo box" for both types of control.

Uau, this is crazy! When Microsoft consolidated input text and textarea on ASP:TextBox, this was a good thing. Merging DropDownList and ComboBox was also a good thing if DropDownList was the choosen naming...

So I am with those who believe:

This usage is sometimes made more precise as "non-editable combo box" (or something similar). Nevertheless, there are those who consider this usage incorrect, and apply the term "combo box" only to controls that allow edits.





Query Composition using Functional Programming Techniques in C# 3.0

Here's a great post about query Composition using Functional Programming Techniques in C# 3.0. I can relate to the conclusion:

In the end, I believe that programming in the FP style is useful and powerful. I think that in some circumstances, imperative code is more readable than FP code, so I am not an advocate of doing everything only in the FP style. As my learning evolves, I'll certainly expose my learning process publically.


Here are the links:


1. Introduction to the FP Tutorial

Introduces functional programming, mentions some of the core ideas, and the main hurdle (lazy evaluation) that users of LINQ will have to jump.

2. What this Tutorial Covers

Gives a quick overview of what the tutorial covers.

3. Quick Intro to Query Expressions

Capsule summary of query expressions and explicit notation.

4. Lambda Expressions

Introduction to lambda expressions, their syntax, and their semantics. Lambda expressions are one of the key tools that you will use when doing functional programming.

5. Extension Methods

Introduction to extension methods, and their applicability to functional programming.

6. Local Variable Type Inference

Anonymous types are key to FP, and in certain circumstances, to use anonymous types, you must use local variable type inference.

7. Object and Collection Initializers

To use anonymous types, you must use object and collection initializers. This introduces them.

8. Tuples and Anonymous Types

Tuples are important for creating intermediate values (and sometimes final results) of queries. This introduces tuples as implemented via anonymous types.

9. The Yield Contextual Keyword

Yield blocks are the foundation on which lazy evaluation is based. Yield blocks were introduced to C# in version 2.0.

10. Lazy Evaluation

This introduces and discusses what may be the most difficult hurdle that OO programmers must get over when using LINQ.

11. Aggregation

Aggregation is a core tool that you will use to develop results when using FP.

12. Programming in a Functional Style

Introduces the idea of declarative programming (vs. imperative programming).

13. Procedural Analogs

Presents an analog to the switch procedural construct.

14. Pure Functions

Refactoring in FP consists of creation of pure functions. This defines pure functions and tells why you want to use them.

15. Parsing WordML

This introduces our main example that we'll use in this tutorial. This example pulls together all of the previously introduced topics, including lambda expressions, extension methods, tuples (including type inference and object initializers), lazy evaluation, aggregation, and pure functions.

15.1 The Word Document to Parse

This topic contains the Word document that we'll parse using FP techniques.

15.2 Retrieving the Paragraphs

Develops a query that returns all of the paragraphs in a Word document. Creates an intermediate result using an anonymous type.

15.3 Refactoring using a Pure Function

Refactors the previously introduced query using a pure function. The resulting query is easier to read.

15.4 Retrieving the Text of the Paragraphs

Modifies the query to retrieve the text of each of the paragraphs. Adds the text to the tuple.

15.5 Separating Out the Code and Comments

Modifies the query to determine whether a paragraph is code or comment, or anything else.

15.6 Retrieving the Two Code/Comment Groups

Introduces a new query operator, GroupOnChange, implemented as an extension method on IEnumerable.

15.7 Filtering Out the Groups that we Don't Want

Modifies the query to retrieve just the groups that we want.

15.8 The Final Results

Modifies the query to retrieve exactly what we want. This is the end target of this example.

15.9 Complete Listing of ParseWordML

Contains the complete listing of our example.

16. Overview of a Functional Transform

Introduces the idea of doing transformations in a functional programming style. Contains the code for the transformation.

16.1 PurchaseOrders.xml

Contains the source XML document for our transformation.

17. Conclusion

Final thoughts about functional programming.



How to stop a development team for over an hour

Yesterday all of our development environments just stopped working over some sub-set of our model. As we had been doing a lot of refactoring, we started rolling back the changes (HBM + Model) until it worked. Yes, it's true, if something fails, we first look at NHibernate.

Another team was having their share of problems. They complained about some problems with NHibernate - it wasn't mapping correctly some data from the database. After some debugging I found the problem: this team didn't commit the changes they were doing at the database! So NHibernate was innocent.

The first team finally come to me with good and bad news:

  • after the rolling back, the application started working;
  • after unrolling it back, the application resumed working!
Boy, were we in trouble. We confirmed the version control to me sane, and then it hit me: this team was sufering from the table locking from the first team!

The second team was using TOAD to edit some data over the database. Apparently TOAD got a table lock when editing over the datagrid, so all of the inserts the first team tried just blocked. As NHibernate was the new kid on the block, he got the blame.

Good for you, NHibernate. Bad for you, TOAD.

BDD - Behavior-driven development

Scott Bellware [MVP] has just posted about BDD, where he says:


"What it is" is the model. "What it does" is the behavior.

We prefer to talk about the behavior of the system because we communicate more stuff with fewer efforts than talking about the model.

Wednesday, August 01, 2007

Mapping SQL Server Errors to .NET Exceptions (the fun way!)

Tom Hollander has just posted about mapping SQL Server Errors to .NET Exceptions, where he ends up with the following code and statement:

 try
{
Database db = DatabaseFactory.CreateDatabase("MyDatabase");
db.ExecuteNonQuery("spMySproc", param1, param2);
}

catch (Exception ex)
{

if (ExceptionPolicy.HandleException(ex, "Data Access Policy"))

throw;

}

This is a lot nicer, but it still requires that the boilerplate exception logic is included in every data access method. But with the magic of the Policy Injection Application Block, you don't even need to do this. By configuring a policy in the PIAB configuration, or decorating your data access class or individual methods with the [ExceptionCallHandler] attribute, you can get the desired behavior with no exception handling logic in the code at all!


LINQ to SQL Debug Visualizer

Scott Gutrie has just posted about how to enable SQL debuger visualizer on LINQ - we no longer need to read the log file :)

Dam! I've just finish a LINQ internal demo with the log files - I'll have to update it...

Generating Word Documents on the server

Here are 2 clean links from mszCool on how to generate Word documents on the server:


DinnerNow.net

I've been catching up ARCast, and finally decided to post about DinnerNow after earing Ron Jacobs's ARCast DinnerNow series. From the DinnerNow.net site:

DinnerNow is a fictious marketplace where customers can order food from local restaurants for delivery to their home or office. This sample is designed to demonstrate how you can develop a connected application using several new Microsoft technologies.

The demo utilizes several technologies including: IIS7, ASP.NET Ajax Extensions, Linq, Windows Communication Foundation, Windows Workflow Foundation, Windows Presentation Foundation, Windows Powershell, and the .NET Compact Framework.

The DinnerNow sample application is now available for download. You can download the entire DinnerNow sample code from CodePlex.


Is it all you can eat from .NET3? Is it the swiss army knife of .NET3! No, it's DinnerNow!

Development Catharsis :: Copyright 2006 Mário Romano