HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

NHibernate (UoW) + cCommand pattern

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
uowpatternnhibernateccommand

Problem

I'm building an 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.

ICommand.cs (Core layer)

using NHibernate;

namespace MyDemon.Core.Commands
{
    public interface ICommand
    {
        TResult Execute(ISession session);
    }
}


ISessionExtensions.cs (Core layer)

using NHibernate;

namespace MyDemon.Core.Commands
{
    public static class ISessionExtensions
    {
        public static TResult Execute(this ISession session, ICommand unitOfWork)
        {
            return unitOfWork.Execute(session);
        }
    }
}


GetUserById.cs (Core layer)

using NHibernate;

namespace MyDemon.Core.Commands.User
{
    using Entities;

    public class GetUserById : ICommand
    {
        public int UserId { get; set; }

        public GetUserById(int userId)
        {
            UserId = userId;
        }

        #region Implementation of IUnitOfWork

        public User Execute(ISession session)
        {
            return session.Get(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(userToGet);

    return PartialView("Partials/Details", userToViewModel);
}


What do you think? A clever design, or just another "too-much code" approach?

Solution

This seems to be a lot of code to say the same as this:

[AjaxOnly]
[Authorize]
public ActionResult Details(int id)
{
    User userToGet = _session.Get(id);

    if (userToGet == null)
    {
        return PartialView("Partials/UserNotFound");
    }

    DetailsUserViewModel userToViewModel = Mapper.Map(userToGet);

    return PartialView("Partials/Details", userToViewModel);
}


What problem are you trying to solve here?

(or am I missing something?)

Code Snippets

[AjaxOnly]
[Authorize]
public ActionResult Details(int id)
{
    User userToGet = _session.Get<User>(id);

    if (userToGet == null)
    {
        return PartialView("Partials/UserNotFound");
    }

    DetailsUserViewModel userToViewModel = Mapper.Map<User, DetailsUserViewModel>(userToGet);

    return PartialView("Partials/Details", userToViewModel);
}

Context

StackExchange Code Review Q#522, answer score: 3

Revisions (0)

No revisions yet.