patterncsharpMinor
NHibernate (UoW) + cCommand pattern
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)
ISessionExtensions.cs (Core layer)
GetUserById.cs (Core layer)
AccountController.cs (Web layer)
What do you think? A clever design, or just another "too-much code" approach?
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:
What problem are you trying to solve here?
(or am I missing something?)
[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.