Archive for category uNHAddIns
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 and support for DetachedQuery
Posted by Dario in ActiveRecord, NHibernate, uNHAddIns on November 11th, 2007
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
Posted by Dario in NHibernate, uNHAddIns on November 1st, 2007
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.
uNHAddIns new release available – Revision 64
Posted by Dario in NHibernate, uNHAddIns on October 18th, 2007
By internal policies uNHAddins not have yet a release number o release version name, by now we are using the revision number at SVN for this purpose. Probably will come a branch when NHibernate 2.0 release appear, and that will be the time.
Features of new version:
- Added UserType: UpperString. Allow save/get strings against the database in upper case.
- Few features at pagination.
- Documentation enhanced.
- Cloner helper for make objects clone via binary serialization.
- Build files for compiler using NAnt.
For more resources visit:
uNHAddins: UnOfficial NHibernate AddIns
Posted by Dario in NHibernate, uNHAddIns on September 6th, 2007
Fabio Maulo, with who I got the honor of moderate NHibernate-Hispano, put to our disposition this new project calls uNHAddIns. UnOfficial NHibernate Add-Ins provide resources to interactuate with NHibernate, it’s like Contrib package. For now, and just for now, we can use this cool features:
- DetachedCriteria,
- DetachedQuery
- DetachedDynQuery
- Paginator.
DetachedQuery it’s the brother of DetachedCriteria, and doesn’t exists at NHibernate yet. DetachedCriteria it’s at NHibernate long ago, but there was not something similar for HQL. DetachedQuery/DetachedCriteria, it’s usefull to use in places where we don’t have a Session. It’s a good method to retardate until the last moment the association with the query and this help to play a lot at Generics DAOs implementations and their inheritances. Resources:
- Project Site
- Wiki
- Discussion group of uNHAddIns
- LesTroisMousquetaires:DetachedCriteria,DetachedQuery y DetachedDynQuery
- Downloads
Forward I gonna make some post about uNHAddIns, for questions visit the group.