I'm building a ASP.NET MVC app and would like some feedback for my way to query/execute statements to the ORM.
I'm using Fluent NHibernate, and have mixed the UoW provided by NHibernate with a Command pattern.
My code:
// ICommand.cs (Core layer)
using NHibernate;
namespace MyDemon.Core.Commands
{
public interface ICommand<out TResult>
{
TResult Execute(ISession session);
}
}
// ISessionExtensions.cs (Core layer)
using NHibernate;
namespace MyDemon.Core.Commands
{
public static class ISessionExtensions
{
public static TResult Execute<TResult>(this ISession session, ICommand<TResult> unitOfWork)
{
return unitOfWork.Execute(session);
}
}
}
// GetUserById.cs (Core layer)
using NHibernate;
namespace MyDemon.Core.Commands.User
{
using Entities;
public class GetUserById : ICommand<User>
{
public int UserId { get; set; }
public GetUserById(int userId)
{
UserId = userId;
}
#region Implementation of IUnitOfWork<out User>
public User Execute(ISession session)
{
return session.Get<User>(UserId);
}
#endregion
}
}
// AccountController.cs (Web layer)
[AjaxOnly]
[Authorize]
public ActionResult Details(int id)
{
User userToGet = _session.Execute(new GetUserById(id));
if (userToGet == null)
{
return PartialView("Partials/UserNotFound");
}
DetailsUserViewModel userToViewModel = Mapper.Map<User, DetailsUserViewModel>(userToGet);
return PartialView("Partials/Details", userToViewModel);
}
What do you think? A clever design, or just another "too-much code" approach?