Talk of CodeGeneration at Corrientes

The past wednesday with a couple of friends we had the oportunity to listen in a talk to our dear Angel “Java” Lopez at Corrientes Capital City.

The talk was pretty good, Angel spoke about the software reality, a little about the System’s evolution, and the complexity that we must to afront the Software Developers at this days. To afront this issue, we can use code generation tool and make our life a little easier. The material of the talk you can download here (in spanish). Angel also could leave without show us her baby ;)

This is a image from presentation, and show a reality of our systems. Exists some code that we can generate (orange blocks) and another code that we need to write (green blocks), maybe this code could be encapsulted within some framework for our good … and the person who read the code :)

Title: Pragmatic Solution

  • Capa Presentación = Presentation Layer
  • Capa de Negocio = Business Layer
  • Capa de Datos = Data Layer

Maybe the code generation doesn’t be “the solution” but make us the life a little easier and it deal with the boring code that we don’t want to write.

Look at your code and sure you gonna find some code that you can deal it with codegeneration. This is the first step to software factory.

3 layer Example of NHibernate + Spring.Net + AOP

Sometime ago I wrote this template project using Spring.Net and NHibernateprimarily. I use NHibernate for the data access and Spring.Netfor the IoC and Aop. With the Aop support, the transaction managmente it’s very easy to implement.

This it’s the CustomerService class:

    public class CustomerService : BaseService, ICustomerService
    {
	/// <summary>
        /// This field is inyected by IoC through the property.
        /// </summary>

        private ICustomerDao customerDao;        
 	public ICustomerDao CustomerDao {
            get { return customerDao; }
          set { customerDao = value; }
        }

#region ICustomerService Members

	public int CreateCustomer(string Name, string LastName) {

        	Customer customer = new Customer();

        	customer.FirstName = "Dario";

	        customer.LastName = "Quintana";

		CustomerDao.Save(customer);

		return customer.Id;

        }

	public void DeleteCustomer(int Id) {

            	Customer customer = CustomerDao.GetById(Id);

		CustomerDao.Delete(customer);

        }

#endregion

    }

As you can see, the CreateCustomer or DeleteCustomer is not envolved at code by anyone Transactional or UnitOfWork code, nor the method Save(customer) at CustomerDao class.

Then… where it’s the transactional support ? Spring + AOP is the answer. Spring envolves al Service methods between transaccions.

When you instantiate the class CustomerService, a proxy is instantiate insted. With the proxy Spring.Net can envolve the method configurated into transactions. Cool uh?

Another thing that you can appreciate at example it’s the using of uNhAddIns at Repository stuff. With this library you can use Hql/Sql queries detached with the class DetachedQuery. With DetachedQuery you can use NamedQuery and this a good practice to adopt.

Download the Example

ActiveRecord moving to NHibernate 2

Aparently the ActiveRecord guys want to move ActiveRecord to NHibernate 2.0, for now, this means, move to NHibernate trunk (just for now).

This will bring more capabilities to ActiveRecord, for example, here I show you how to use DetachedQuery with ActiveRecord. Moving to NHibernate 2.0, I will able to commit a patch with this support without need uNHAddIns.ActiveRecord.dll.

At this thread you can see the guys of developer list of Castle voting for do this play.

Another key feature to take advantages is Event Listeners. At chat with Fabio Maulo:

  • Me: “I can’t see the advantages using Event Listeners with ActiveRecord at Validation, AR validate before Save and do this work
  • Fabio: “How you can intercept the validation process untill make the Save?
  • Me: “You can’t
  • Fabio: “You just answered yourself

Fluent Interfaces on NHibernate

Ben Scheirman proposed at this entry a interesting theme for apply to NHibernate:Fluent Interfaces.

The main idea it’s create the NHibernate mappgins programmatically instead of using XML, but not only that, but also using fluent interfaces too.

There are many ways to create fluent interfaces for specific domain, and exists several advantages of this approach at the time to consume the code, but above all to read the code. The code must be very easy to read and this make it interesting.

Write fluent interfaces it’s a more complicated  and required practice.

Resources:

ActiveRecord and support for DetachedQuery

This weekend I wrote the classes necessaries to support IDetachedQuery with ActiveRecord. DetachedQuery it’s not available yet at any version of NHibernate, you can find it only in uNHAddIns and at the SVN of NHibernate.

I add a project to uNHAddIns: uNHAddIns.ActiveRecord. This consist in the classical classes for ActiveRecord with support for IDetachedQuery.

If we want support for IDetachedQuery using ActiveRecord you must this clases:

  • ActiveRecordBase
  • ActiveRecordBase<T>
  • ActiveRecordValidationBase
  • and ActiveRecordValidationBase<T>

Now you will able to do somethings like this:

Customer[] customers = Customer.FindAll(new DetachedQuery("from Customer Order By Name"));

The new methods for ActiveRecordBase and ActiveRecordValidationBase are:

  • public static bool Exists(Type targetType, IDetachedQuery detachedQuery);
  • public static Array FindAll(Type targetType, IDetachedQuery detachedQuery);
  • public static object FindFirst(Type targetType, IDetachedQuery detachedQuery);
  • public static object FindOne(Type targetType, IDetachedQuery detachedQuery);
  • public static Array SlicedFindAll(Type targetType, int firstResult, int maxResults, IDetachedQuery detachedQuery);

And the new methods for ActiveRecordBase<T> and ActiveRecordValidationBase<T> are:

  • public static bool Exists(IDetachedQuery detachedQuery);
  • public static T[] FindAll(IDetachedQuery detachedQuery);
  • public static T FindFirst(IDetachedQuery detachedQuery);
  • public static T FindOne(IDetachedQuery detachedQuery);
  • public static T[] SlicedFindAll(int firstResult, int maxResults, IDetachedQuery detachedQuery);

This implementation no hide any feature of ActiveRecord base classes, it’s a extension for IDetachedQuery support.

Requeriments

This classes are provided in the assembly uNHAddIns.ActiveRecord.dll. And this assembly require the main assembly of the project: uNHAddIns.dll.

Downloads here

Wiki entry

Validation on NHibernate

The open door that the EventListeners port let for us, it’s the possibility to create some classes in order to validate objects before them were saved (and before were deleted too).

An option is port Hibernate.Validator, but… will be used by anyone? I don’t think so. Looking it implementation and what it do, do not offer much more that you can find of .Net side with some existent libraries. Maybe some day Hibernate.Validator will be ported, by now, I think that it’s so much important finish the port of the features of Hibernate 3.2.5 GA.

Another valid option, it’s create bridges between NHibernate (EventListeners) and somes known framework for .Net for input validation:

The implementation, either Hibernate.Validator (I don’t think so) or the bridges for the citated, will be at uNHAddIns.

An interesting point, will be the extensibility option for home made validatores.