Paging with NHibernate 3.0 using the new query API QueryOver

NHibernate 3.0 will come with some really nice features. One of them is the new query API QueryOver. You can now create type safe queries. So no magical and error-prone strings are required.

If you ask yourself how to use QueryOver for paging just read the following:

I defined an extension method that docks on every object that implements

IQueryOvery<T,T>
    public static class QueryOverExtensions
        public static PagedList<T> ToPagedList<T>(this IQueryOver<T, T> queryOver, int pageIndex, int pageSize)
        {
            var rowCountQuery = queryOver.ToRowCountQuery();
            IEnumerable<T> list = queryOver.Take(pageSize).Skip((pageIndex - 1) * pageSize).Future();
            int totalCount = rowCountQuery.FutureValue<int>().Value;

            return new PagedList<T>(list, pageIndex, pageSize, totalCount);
        }
    }

You could use this extension in a repository as follows:

    public class TeamEmployeeRepository : Repository<TeamEmployee>, ITeamEmployeeRepository
    {
        public PagedList<TeamEmployee> GetPagedEmployees(int pageIndex, int pageSize)
        {
            return GetSession().QueryOver<TeamEmployee>()
                .Fetch(x => x.Employee).Eager
                .Fetch(x => x.Team).Eager
                .ToPagedList(pageIndex, pageSize);
        }
    }

This code says, create a QueryOver object for the object TeamEmployee and fetch all Teams and Employees by using the eager fetching strategy. The extension method takes the result and pages it.

I really like this ;) It is type safe and very easy to read!

Posted in Development | Tagged , , | 19 Comments

Using AutoMapper in ASP.NET MVC – Implementing a MappingService

Das ist ein Test C# Code.

    public class MappingService : IMappingService
    {
        public PagedList<TDestinationElement> MapToViewModelPagedList<TSourceElement, TDestinationElement>(PagedList<TSourceElement> model)
        {
            var mappedList = MapPagedListElements<TSourceElement, TDestinationElement>(model);
            var index = model.PagerInfo.PageIndex;
            var pageSize = model.PagerInfo.PageSize;
            var totalCount = model.PagerInfo.TotalCount;

            return new PagedList<TDestinationElement>(mappedList, index, pageSize, totalCount);
        }

        public object Map<TSource, TDestination>(TSource model)
        {
            return Mapper<TSource, TDestination>(model);
        }

        private static IEnumerable<TDestinationElement> MapPagedListElements<TSourceElement, TDestinationElement>(IEnumerable<TSourceElement> model)
        {
            return model.Select(element => Mapper.Map<TSourceElement, TDestinationElement>(element))
                .OfType<TDestinationElement>();
        }
    }
Posted in Development | Tagged , , , | 1 Comment

#NETOS .NET Open Space 2010 in Leipzig


.NET Open Space vom .NET Open Space im Oktober 2010 in Leipzig

Die .NET Open Space 2010 Entwickler-Unkonferenz findet am 16-17.10.2010 in Leipzig statt. Die Anmeldung erfolgt nächste Woche.

Mehr unter http://netopenspace.de/2010

Posted in Events | Tagged , | 3 Comments

First blog entry

Well, this is my first blog entry on my new page at www.rookian.com.

Have fun!!!

Posted in Events | Tagged | 2 Comments