<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Billy McCafferty : MVC.NET, Spring.NET</title><link>http://devlicio.us/blogs/billy_mccafferty/archive/tags/MVC.NET/Spring.NET/default.aspx</link><description>Tags: MVC.NET, Spring.NET</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>S#arp Architecture upgraded to ASP.NET MVC Preview 3</title><link>http://devlicio.us/blogs/billy_mccafferty/archive/2008/06/09/s-arp-architecture-upgraded-to-asp-net-mvc-preview-3.aspx</link><pubDate>Tue, 10 Jun 2008 05:34:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:40932</guid><dc:creator>Billy McCafferty</dc:creator><slash:comments>7</slash:comments><description>&lt;p&gt;S#arp Architecture has been upgraded for compatibility with ASP.NET MVC Preview 3 and Spring.NET 1.1.2..&amp;nbsp; A big thanks goes out to Adam Cooper and Frank Laub for assisting with this effort.&amp;nbsp; S#arp Architecture, an architectural framework for developing applications with ASP.NET MVC, Spring.NET and NHibernate, may be found at &lt;a href="http://www.codeplex.com/SharpArchitecture"&gt;http://www.codeplex.com/SharpArchitecture&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Billy McCafferty&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=40932" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/billy_mccafferty/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://devlicio.us/blogs/billy_mccafferty/archive/tags/NHibernate/default.aspx">NHibernate</category><category domain="http://devlicio.us/blogs/billy_mccafferty/archive/tags/MVC.NET/default.aspx">MVC.NET</category><category domain="http://devlicio.us/blogs/billy_mccafferty/archive/tags/Spring.NET/default.aspx">Spring.NET</category></item><item><title>S#arp Architecture:  ASP.NET MVC with NHibernate</title><link>http://devlicio.us/blogs/billy_mccafferty/archive/2008/04/21/asp-net-mvc-best-practices-with-nhibernate-and-spring-net.aspx</link><pubDate>Mon, 21 Apr 2008 22:44:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:40135</guid><dc:creator>Billy McCafferty</dc:creator><slash:comments>54</slash:comments><description>&lt;p&gt;To skip to the chase...&amp;nbsp; &lt;a href="http://code.google.com/p/sharp-architecture/"&gt;http://code.google.com/p/sharp-architecture/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It wasn&amp;#39;t too many years ago that I used to state quietly that I was a Microsoft web developer.&amp;nbsp; Inevitably, a Java developer would hear my whisper and begin laughing hysterically as I cowered away from defending Active Server Pages.&amp;nbsp; Not long after, I felt a bit more confident defending Microsoft web development after the introduction of ASP.NET but have yet to this day been able to accurately describe the page life cycle&amp;nbsp;or defend&amp;nbsp;why the *%$@ it was ever a good idea.&amp;nbsp;&amp;nbsp;While the .NET language continued to progress into a&amp;nbsp;world class&amp;nbsp;language with generics and&amp;nbsp;LINQ, I often wondered when the presentation layer would get the attention it deserved.&amp;nbsp; To add insult to injury, the Rails crew was quick to join in the&amp;nbsp;hysterics that the Java community had long been enjoying.&amp;nbsp; To make up for the deficiencies of developing Microsoft web applications, many of us would come up with near ridiculous schemes to facilitate even a partial amount of test driven development and properly tiered layers.&amp;nbsp; With ASP.NET MVC, we now easily stand shoulder to shoulder with the clean separation of concerns enjoyed by other technologies.&amp;nbsp; But with any new technology, there is&amp;nbsp;a dire and immediate need for communications and guidance concerning how to develop a solid, testable web application using time tested patterns for rapid development and long term maintainability.&amp;nbsp; With this post, I hope to do just for ASP.NET MVC.&lt;/p&gt;
&lt;p&gt;Pronounced &amp;quot;Sharp Architecture,&amp;quot; this is a solid architectural foundation for rapidly building maintainable web applications leveraging the ASP.NET MVC framework with NHibernate 1.2 and Spring.NET 1.1.1.&amp;nbsp; 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.&amp;nbsp; 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.&amp;nbsp; Accordingly, S#arp Architecture adheres to the following key principles:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Focused on Domain Driven Design&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;This architecture places emphasis on the domain as being the core element of focus.&amp;nbsp; Accordingly, the domain layer has no direct dependencies on any data loading mechanisms or other resource dependent services.&amp;nbsp; This facilitates a test driven approach to developing the domain without the need for external resources, such as a database, to be available during domain development.&amp;nbsp; (Alternatively, some developers prefer a model driven approach to application development.&amp;nbsp; A solid framework for supporting this alternative style is the ADO.NET Entity Framework.)&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Loosely Coupled&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;All layers within S#arp Architecture depend only on the core domain layer and framework utilities.&amp;nbsp; In many applications, the presentation tier depends on a service tier which depends on a domain tier which depends on a data access tier.&amp;nbsp; A frequent problem with this is that every layer &amp;quot;up the chain&amp;quot; inherently depends on every tier below it.&amp;nbsp; This tight coupling makes it increasingly more difficult, as development on a project proceeds, to make modifications to lower tiers without having an adverse affect on the tiers above it.&amp;nbsp; Alternatively, this architecture keeps the tiers loosely coupled from each other; e.g., the controller and domain logic layers have no direct dependency on the data tier.&amp;nbsp; To facilitate this, the core domin layer, in addition to defining the domain model and logic, also define service interfaces which are needed by other layers.&amp;nbsp; (Examples of a &amp;quot;service&amp;quot; include a data access object, a ftp utility, or a web service.)&amp;nbsp; In this way, the controller logic only has knowledge of data access interfaces, for which implementation details can be injected at runtime.&amp;nbsp; This loosely coupled design further faciliates test driven development and allows dependencies to be more easily modified, if not completely replaced, with no affect on &amp;quot;higher&amp;quot; tiers.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Preconfigured Infrastructure&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Developers often spend much of their time maintaining data access and other infrastructure related layers.&amp;nbsp; Furthermore, infrastructure to facilitate test driven development which bypasses live data access code is often complicated and error prone to configure.&amp;nbsp; By leveraging NHibernate 1.2 for data access, along with the benefits of .NET generics, it is rarely the case, using this described approach, that even a single line of code need be written to have full data access capabilities for a given domain object.&amp;nbsp; Spring.NET 1.1 is then leveraged to provide dependency injection of concreate data access objects into the controllers layer.&amp;nbsp; (Alternatively, the data access objects could be injected into a service layer when appropriate.)&amp;nbsp; To reduce the risk of introducing bugs into XML configuration files, this framework includes a Spring.NET XML configuration generator to handle defining data access objects (DAOs), controllers, and DAO injection into controller constructors.&amp;nbsp; The only time it is necessary to configure Spring.NET XML is when custom DAOs are required.&amp;nbsp; Note that this is the only code generation in the entire framework.&amp;nbsp; There is no bazillion lines of data access code generated upon every compilation.&amp;nbsp; There are no magically created partial classes to facilitate data access.&amp;nbsp; This makes for a much more understandable and maintainable application.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Open Ended Presentation&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;There are a plethora of great alternatives available for building the presentation layer of an application.&amp;nbsp; Consequently, S#arp Architecture mandates few, if any, constraints on how the presentation layer should be designed and developed.&amp;nbsp; In fact, the only constraint is that ASP.NET MVC be leveraged; other than that, any presentation option is available for use.&amp;nbsp; To illustrate, this framework shows no preferences towards any particular presentation solution such as jQuery, Yahoo UI widgets, or the Microsoft AJAX utilities.&amp;nbsp; As long as your preferred mechanism works with MVC, this framework will accommodate it.&lt;br /&gt;&lt;br /&gt;To better facilitate community involvement, to provide releases for later versions of ASP.NET MVC and other framework dependencies, and to manage documentation (with a comprehensive walk through lab), S#arp Architecture is being managed at &lt;a href="http://code.google.com/p/sharp-architecture/"&gt;http://code.google.com/p/sharp-architecture/&lt;/a&gt;.&amp;nbsp; The corresponding discussion group may be found at &lt;a href="http://groups.google.com/group/sharp-architecture"&gt;http://groups.google.com/group/sharp-architecture&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;I sincerely hope that this architecture will serve as a key element in enabling you to create high quality applications which both you and your clients can take pride in.&amp;nbsp; This work reflects the knowledge of industry experts, the feedback garnered from past experiences, and the help of many people who have lent a hand or provided guidance and/or feedback when needed.&amp;nbsp; Particularly, I&amp;#39;d like to thank Damon Carr, Ben Scheirman, Alan Northam, Mark Pollack and Erich Eichinger of the Spring.NET team, Wayde Gilliam and the hundreds of developers who helped me hone my ideas by providing great feedback over the past couple of years.&amp;nbsp; A special thanks also goes to Roy Bradley who made it possible for me to spend as much time as I did on this effort.&lt;/p&gt;
&lt;p&gt;Some&amp;nbsp;&lt;span style="text-decoration:line-through;"&gt;useful&lt;/span&gt; essential background material for this architecture may be found at &lt;a href="http://www.codeproject.com/KB/architecture/NHibernateBestPractices.aspx"&gt;http://www.codeproject.com/KB/architecture/NHibernateBestPractices.aspx&lt;/a&gt;.&amp;nbsp; Although there are major changes in the infrastructure of the new architecture, the overall structure is similar to that presented in this previous article.&amp;nbsp; So it&amp;#39;s a great starting point for getting up to speed with the knowledge areas included.&lt;/p&gt;
&lt;p&gt;The overall goal of this architecture is to allow developers to worry less about application &amp;quot;plumbing&amp;quot; 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.&amp;nbsp; Enjoy and keep the feedback coming!&lt;/p&gt;
&lt;p&gt;Billy McCafferty&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=40135" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/billy_mccafferty/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://devlicio.us/blogs/billy_mccafferty/archive/tags/NHibernate/default.aspx">NHibernate</category><category domain="http://devlicio.us/blogs/billy_mccafferty/archive/tags/Featured/default.aspx">Featured</category><category domain="http://devlicio.us/blogs/billy_mccafferty/archive/tags/MVC.NET/default.aspx">MVC.NET</category><category domain="http://devlicio.us/blogs/billy_mccafferty/archive/tags/Spring.NET/default.aspx">Spring.NET</category></item><item><title>Using Spring.NET and NHibernate with ASP.NET MVC</title><link>http://devlicio.us/blogs/billy_mccafferty/archive/2008/04/04/using-spring-net-and-nhibernate-with-asp-net-mvc.aspx</link><pubDate>Fri, 04 Apr 2008 22:38:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:39937</guid><dc:creator>Billy McCafferty</dc:creator><slash:comments>46</slash:comments><description>&lt;p&gt;[Update 6/11/08 - To see an example of this in action, read the introduction of &lt;a href="http://devlicious.com/blogs/billy_mccafferty/archive/2008/04/21/asp-net-mvc-best-practices-with-nhibernate-and-spring-net.aspx"&gt;S#arp Architecture&lt;/a&gt;.]&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve been quiet lately as I&amp;#39;ve been a very busy bee preparing the inner workings of the sequel to my NHibernate Best Practices article.&amp;nbsp; In the next installment, I&amp;#39;ll be discussing in detail how to leverage the best of Spring.NET for dependency injection and transaction management, the best of NHibernate for a nearly touch-free data access layer and non-intrusive custom collections, and ASP.NET MVC with the MvcContrib project for a bomber presentation tier.&lt;/p&gt;
&lt;p&gt;The biggest challenge I&amp;#39;ve faced in one sitting is getting NHibernate lazy loading to work in symbiosis with Spring.NET dependency injection for controllers.&amp;nbsp;&amp;nbsp;The key is to leverage Spring&amp;#39;s WebApplicationContext instead of trying to create and&amp;nbsp;configure your own within Application_Start in Global.asax.cs.&amp;nbsp; The steps to realize this are as follows:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;div&gt;Configure Spring.NET as you normally would&amp;nbsp;for an ASP.NET application, but leave out the PageHandlerFactory setting.&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;
&amp;lt;configuration&amp;gt;
  &amp;lt;configSections&amp;gt;
    &amp;lt;sectionGroup name=&amp;quot;spring&amp;quot;&amp;gt;
      &amp;lt;section name=&amp;quot;context&amp;quot; type=&amp;quot;Spring.Context.Support.WebContextHandler, Spring.Web&amp;quot;&amp;gt;
      ...
    &amp;lt;/sectionGroup&amp;gt;
  &amp;lt;/configSections&amp;gt;
  ...
  &amp;lt;spring&amp;gt;
    &amp;lt;context&amp;gt;
      &amp;lt;resource uri=&amp;quot;~/SpringConfig/Spring.xml&amp;quot;&amp;gt;
    &amp;lt;/context&amp;gt;
  &amp;lt;/spring&amp;gt;
  &amp;lt;system.web&amp;gt;
    &amp;lt;httpModules&amp;gt;
      &amp;lt;add name=&amp;quot;OpenSessionInView&amp;quot; type=&amp;quot;Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate12&amp;quot;&amp;gt;
      &amp;lt;add name=&amp;quot;Spring&amp;quot; type=&amp;quot;Spring.Context.Support.WebSupportModule, Spring.Web&amp;quot;&amp;gt;
      ...
    &amp;lt;/httpModules&amp;gt;
  &amp;lt;/system.web&amp;gt;
&amp;lt;/configuration&amp;gt; &lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;Configure Spring.NET for your controllers.&amp;nbsp; In the sample below, a data access object is being passed using constructor injection to the controller.&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;
&amp;lt;objects xmlns=&amp;quot;http://www.springframework.net&amp;quot;&amp;gt;
  &amp;lt;description&amp;gt;
    MVC Controller declarations.
  &amp;lt;/description&amp;gt;
  &amp;lt;object id=&amp;quot;HomeController&amp;quot; type=&amp;quot;Northwind.Controllers.HomeController, Northwind.Controllers&amp;quot;&amp;gt;
  &amp;lt;object id=&amp;quot;CustomersController&amp;quot; type=&amp;quot;Northwind.Controllers.CustomersController, Northwind.Controllers&amp;quot;&amp;gt;
    &amp;lt;constructor-arg ref=&amp;quot;CustomerDao&amp;quot;&amp;gt;
  &amp;lt;/object&amp;gt;
&amp;lt;/objects&amp;gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;Instruct Global.asax.cs to leverage the loaded Spring.NET context when initializing the MvcContrib project&amp;#39;s Spring.NET IoC controller factory.&amp;nbsp; The crux of the problem here is that you can&amp;#39;t get a handle on the web application context in Application_Start as it&amp;#39;s only available after all of the HttpModules (e.g., Spring.NET&amp;#39;s WebSupportModule) have been loaded into memory.&amp;nbsp; Therefore, you must wait until HttpApplication.Init() to get a hold of the context loaded via the web.config file. The following code, which leverages MvcContrib&amp;#39;s Spring.NET controller factory, demonstrates how to do this.&lt;br /&gt;public class GlobalApplication : System.Web.HttpApplication
{
    protected void Application_Start(object sender, EventArgs e) {
        RegisterRoutes(RouteTable.Routes);
    }
    public void RegisterRoutes(RouteCollection routes) {
        ...
    }
    public override void Init() {
        base.Init();
        ConfigureIoC();
    }
    private void ConfigureIoC() {
        WebApplicationContext webApplicationContext = 
            ContextRegistry.GetContext() as WebApplicationContext;
        DependencyResolver.InitializeWith(
            new SpringDependencyResolver(webApplicationContext.ObjectFactory));
        ControllerBuilder.Current.SetControllerFactory(typeof(IoCControllerFactory));
    }
}&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;With the above in place, you can now leverage Spring.NET&amp;#39;s dependency injection, transaction management, and the open-session-in-view pattern for NHibernate session management and lazy loading within your views.&amp;nbsp; There are obviously details left out, but if you&amp;#39;ve been working on this problem,&amp;nbsp;this is the concise solution.&amp;nbsp; (This post uses Spring.NET 1.1, NHibernate 1.2, ASP.NET MVC Preview 2, and MvcContrib 0.0.1.96 Beta)&lt;/p&gt;
&lt;p&gt;Billy McCafferty&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=39937" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/billy_mccafferty/archive/tags/NHibernate/default.aspx">NHibernate</category><category domain="http://devlicio.us/blogs/billy_mccafferty/archive/tags/MVC.NET/default.aspx">MVC.NET</category><category domain="http://devlicio.us/blogs/billy_mccafferty/archive/tags/Spring.NET/default.aspx">Spring.NET</category></item></channel></rss>