S#arp Architecture: ASP.NET MVC Best Practices with NHibernate

by Jacobus Meintjes August 31, 2009 08:47

Found at S#harp architecture

 

Pronounced "Sharp Architecture," this is a solid architectural foundation for rapidly building maintainable web applications leveraging the ASP.NET MVC framework with NHibernate. The primary advantage to be sought in using any architectural framework is to decrease the code one has to write while increasing the quality of the end product. A framework should enable developers to spend little time on infrastructure details while allowing them to focus their attentions on the domain and user experience. Accordingly, S#arp Architecture adheres to the following key principles:

  • Focused on Domain Driven Design
  • Loosely Coupled
  • Preconfigured Infrastructure
  • Open Ended Presentation

The overall goal of this is to allow developers to worry less about application "plumbing" and to spend most of their time on adding value for the client by focusing on the business logic and developing a rich user experience.

Good background material reading includes http://www.codeproject.com/KB/architecture/NHibernateBestPractices.aspx. Although there are major infrastructural changes from this article in the current architecture, the general structure is very similar and the background reading is very helpful in understanding many of the ideas behind this architecture. S#arp Architecture documentation is included along side the current release.

S#arp Architecture and the preceding article have been referenced in NHibernate in Action by Pierre Henri Kuate, Windows Developer Power Tools by James Avery and Pro LINQ Object Relational Mapping in C# 2008 by Vijay P. Mehta.

Billy McCafferty

Tags:

ASP.Net MVC

NHibernate - NonUnique object exception

by Jacobus Meintjes July 26, 2009 16:33

Using ASP.Net MVC and NHibernate has added some tricky parts to the way I have developed for a while now.

As I was busy working  with the Create/View view for my entity, I suddenly started getting the following error:


a different object with the same identifier value was already associated with the session: 38, of entity: Model.Entities.Muppet

After doing some searching on google I found the error, a small piece of code to check the state of a specific object.

I was trying to access the same Entity object from the database as was currently being used.

Muppet current = muppetRepository.Muppets.FirstOrDefault(m => m.Id == muppet.Id);

By adding a hidden field to the view with the previous value that I required and comparing that, the exception was cleared.

Tags:

ASP.Net MVC | NHibernate

Adding menu items with valid routes to ASP.Net MVC

by Jacobus Meintjes July 14, 2009 09:54

While working on my MVC project I found that adding a menu item can be tricky if you don't do it correctly. Instead of

just using a string for the URL, use the RouteValueDictionary class. 

 

Menu Item:

   1:      public class MenuItem

   2:      {

   3:          public string Text { get; set; }

   4:          public RouteValueDictionary RouteValues { get; set; }

   5:      }

MenuItemRepository:

   1:   public class FakeMenuRepository

   2:      {

   3:          private IList<MenuItem> items = new List<MenuItem>();

   4:   

   5:          public FakeMenuRepository()

   6:          {

   7:              AddMenuItem("Home", "Home", "Index", 1);

   8:              AddMenuItem("Trusts", "Trusts", "List", 1);

   9:              AddMenuItem("Trusts", "Trusts", "List", 1);

  10:          }

  11:   

  12:          public IList<MenuItem> MenuItems

  13:          {

  14:              get

  15:              {

  16:                  return items;

  17:              }

  18:          }

  19:   

  20:          public void AddMenuItem(string text, string _controller, string _action, int _page)

  21:          {

  22:              MenuItem m = new MenuItem();

  23:              m.Text = text;

  24:              m.RouteValues = new RouteValueDictionary(new

  25:                                                           {

  26:                                                               controller = _controller,

  27:                                                               action = _action,

  28:                                                               page = _page

  29:                                                           });

  30:             

  31:              items.Add(m);

  32:          }

  33:      }

 

 

Displaying the menu items:

<%foreach(var item in Model){%>

    <a href="<%= Url.RouteUrl(item.RouteValues) %>">

        <%= item.Text %>

    </a>

<%} %>

 

Url.RouteUrl - Gets or sets the URL pattern for the route. Basically it will create the URL for you and, test that the route you have added is valid against the route table.

Tags:

ASP.Net MVC

ASP.Net MVC Routes

by Jacobus Meintjes July 08, 2009 08:36

This had me stumped for a while.

I had the following code in my List view:

   1:  <%= Html.ActionLink("Edit", "Edit", new {id=item.Id} %>

When I tried to redirect from the list view to the edit view, nothing happened. Even the html that was rendered to the page showed a empty href value.

<a href="">Edit</a>

After looking through a demo application's code, I got it.

Just add a generic route to the route collection:

   1:  routes.MapRoute(null, "{controller}/{action}");

Moral of the story

Always make sure the route is configured correctly.

Tags:

ASP.Net MVC

Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen | Modified by Mooglegiant

About Me

Jacobus Meintjes
 
C# developer working with ASP.Net and/or Windows Forms.
Email Me

Cumulus

This will be shown to users with no Flash or Javascript.

Widget Twitter not found.

Root element is missing.X