Tuesday, June 9, 2009

Master Pages with NVelocity and ASP.NET MVC

Not too long ago I was digging around for information on how to use the NVelocity ViewEngine for ASP.NET MVC. The MVCContrib project on CodePlex currently does not have any documentation, and it took some digging around in the source code and samples to find the solution. Master pages with NVelocity for ASP.NET MVC are not quite as fully featured as with standard .aspx views, however they are possible in a basic form.

The NVelocity view engine for ASP.NET MVC supports a #parse() command which may be used to render any other NVelocity view. You can explicitly state the view you wish to render, such as “shared/header.vm”, or specify the variable $childContent to render the child page. Unlike .aspx views, you can not create multiple content place holders in the master page, each of which can be populated in your child page. But for the most part, that is ok, because with the #parse() command and partial view rendering you can pretty much take care of any composability needs in your NVelocity views anyway.

<head>
  <title>My Page with Master Page</title>
  <link href="$Url.Content('~/Views/Common/Site.css')" rel="stylesheet" type="text/css" />
</head>
<body>
  <div id="header">
    #parse("shared/header.vm")
  </div>
  <form>
  <div id="content">
    #parse($childContent)
  </div>
  <div id="footer">
    #parse("shared/footer.vm");
  </div>
  </form>
</body>

Saturday, May 23, 2009

Stackoverflow

I’ve been encountering Stackoverflow.com quite a lot lately when I search for answers to programming questions on the internet. Today, I finally signed up for a membership to the site, and I must say, its one of the nicest community sites for programmers I have ever encountered.

Stackoverflow is a refreshing blast of modern web site and community design, and quite enjoyable to use. It’s also an intriguing weave of forum, wiki, blog and something akin to digg. For those of you who are interested in sharing your knowledge, Stackoverflow offers a bunch of rewards and incentive to share. The more you help, rate, and answer questions, the more badges (rewards for achievements) and reputation you earn. The more reputation you have, the more power you have in the overall Stackoverflow community.

Which leads into another interesting aspect of Stackoverflow. It is a site for programmers, by programmers, and managed by you. There is a staff who develops and maintains the infrastructure and software for Stackoverflow, but its the community members who administer it. Those with the highest rep are granted the rights of moderators, able to edit, close, delete, and moderate the questions and posts on the site.

While I can’t say that Stackoverflow has displaced CodeProject as my primary programmers resource, it has definitely taken second place on my list of favorite developer community sites.

Give it a whirl: www.stackoverflow.com

Sunday, May 17, 2009

New Water Cooled Computer

Near the beginning of the year, my old motherboard up and died on me. As a result, I ended up doing a full upgrade with a new motherboard, cpu, and ram. In addition to upgrading, I also went all out and built a custom water cooling system with Koolance parts. Here are some pics and the system specs:

Motherboard Asus Rampage II Extreme
CPU Core i7 920 2.66Ghz @ 4.3Ghz
Memory 12Gb OCZ DDR3-1600 7-7-7-24
GPU EVGA GeForce GTX 280
Sound Creative X-Fi Motherboard Edition
Screen Apple CinemaDisplay 30”
Drive Space 2Tb 4 7200Rpm Western Digital
Case Antec P190 1200Watt Dual PSU

The water cooling system is built entirely from Koolance parts. It includes a custom waterblock for the Asus Rampage II Extreme that covers both the north and south bridge, a waterblock for the CPU, as well as two small waterblocks for the voltage regulators around the CPU.

CPU http://www.koolance.com/water-cooling/product_info.php?product_id=755
MB http://www.koolance.com/water-cooling/product_info.php?product_id=772
VReg1 http://www.koolance.com/water-cooling/product_info.php?product_id=736
VReg2 http://www.koolance.com/water-cooling/product_info.php?product_id=660
Nozzles http://www.koolance.com/water-cooling/product_info.php?product_id=397
Tubing http://www.koolance.com/water-cooling/product_info.php?product_id=171
Exchanger http://www.koolance.com/water-cooling/product_info.php?product_id=634
Pump/Res http://www.koolance.com/water-cooling/product_info.php?product_id=280

And here are some pictures of the system (I apologize for the quality…they were taken on my iPhone):

Motherboard with waterblocks Closeup of Motherboard Waterblock

Installed Cooling System (Tubing in Normal Light)\ Installed Cooling System (Tubing in UV)Pump/Reservoir/CtrlSys

Case with installed cooling system and UV lighting

Case with installed cooling system and UV lighting

Friday, November 28, 2008

The Disappointment of Entity Framework

I just hit the EF Design Blog again, for the first time in a few weeks. I was quite dismayed by the latest post, which was addressing one of the most important aspects of EF: change tracking. To put it simply, I’m disgusted by the approach the EF team is taking. They are not solving the problem…they are adding an API so we, their customers, can solve the problem for them. They are taking the easy way out, shortcutting the process, and ignoring the needs of their customers…by giving them more work.

I believe Microsoft has lost sight of what the real world need is for products like Entity Framework: Object Relational Mapping…bridging the gap between the relational world of tables and foreign keys, and the object world of types and references and hierarchies. I think they have become too wrapped up in trying to create a pervasive, all encompassing “data framework” that they have overstepped their bounds, forgotten how to solve their customers problems, and are now just building another bloated platform. Which is absolutely NOT what we need.

I was very excited for EF before it was released. It boasted advanced mapping capabilities, a provider model, a much richer visual designer than LINQ to SQL, and an n-tier story. In actuality, mapping is a nightmare mired in useless rules and restrictions, the visual designer is as touchy as an inmate in an psych ward eagerly awaiting the chance to wipe away manual changes, and the n-tier story is generally absent due to missing change tracking. The problem we were left with was a half-wit visual designer that needed “developer support” through manual editing of SSDL/CSDL/MSL and a cesspool of ad-hoc one-off change tracking solutions to fill in the gaps that Microsoft, conveniently enough for themselves, left gaping to meet a release date.

An here we are, eagerly awaiting news about the solutions hiding around the corner in EF v2…and the story hasn’t changed. We won’t be given solutions…we will be given the means to create a thousand solutions ourselves. Each one requiring an investment of either time or money…or quite possibly both…depending on whether we solve the problem ourselves, or wait to buy the solutions of others, or buy a solution that doesn’t solve the problem adequately enough requiring still further solutions. Either way, the real problem isn’t solved, and EF v2 is shaping up to be another wonderful round of EF v1 with a few new API’s and a fancy WF code generator.

Whoop, Whoop.

Thursday, October 2, 2008

Data for Display vs. Data for Modification

I have long been a fan of object-oriented programming. From the first day I borrowed a book on C++ from the local library when I was about 10 or 11 years old, I was hooked. Objects were so natural, convenient, and kept code organized. Objects could be related to other objects, objects could contain lists of other objects, and object graphs were born. Back then, and for quite some time, objects seemed to be king, and were the fundamental component of software for a very long time. Even today, objects are still a very fundamental aspect of software development. Objects and object graphs solve a lot of problems.

Object graphs don’t solve every problem, however. Sometimes, the use of a rich, hierarchical object model with rich relationships between the various participants can get in the way. Simple needs can become hugely complex when object graphs are involved. Resources such as CPU, memory, and bandwidth can become needlessly consumed when object graphs are created and passed around when only a subset or aggregation of the information contained within the graph is actually required. With the complexity of modern applications, and the need to develop them cheaply with minimal developer resources…keeping things as simple and efficient as possible is becoming ever more important.

Data for Modification

When it comes to editing data, objects are still king. Creation, modification, or deletion of data is a fundamental part of just about every application. Such operations require detailed knowledge of the “true” structure of the data being edited. Knowing the hierarchical and relational associations between the entities in a graph are critical to safely and successfully creating, modifying, or deleting the data those entities represent while maintaining the integrity of that data. Rich object graphs are the ideal mechanism to store and represent data that needs to be modified.

Data for Display

Despite the value of objects for modification, the richness and detail of an object graph does not effectively meet the bar when data simply needs to be displayed. Data is most often stored in a relational structure: tables, keys, and references to keys of other tables. Pieces of information are isolated into discreet locations, aggregated into related sets, and stored in the most efficient manner possible to save space and maximize retrieval speed.

When we display data, however, we tend to display it in ways that make the most sense to humans, and those ways don’t often mirror the ways we store data or edit data. Data from multiple tables are joined, aggregated, and reduced into a flat set that represents exactly what humans are interested in. These flat result sets are best represented, not with a structured, composite object graph, but with a simple table, or a collection of simple non-related, non-structural objects. Simple, flat structures with minimal relationships are compact, efficient structures that may be quickly transferred from one tier to another in an application.

Separating Display and Modification Concerns

The differences in the needs of data display vs. data modification dictate that they be approached differently when developing software. In a modern n-Tier application, the same data will often be transferred across a wire several times. Database to Business Layer, Business Layer to Client, Service to Service, etc. Transferring a collection of rich object graphs when only pieces of information from that graph need to be displayed is wasteful of resources across all the tiers of an application. Transferring a rich object graph to a view for editing that information is essential, and therefor not necessarily wasteful, since all the details of the information being edited are required.

Older software development paradigms, such as ADO.NET with Data Sets, natively support this separation of concerns. A simple DataTable may be retrieved with exactly the information that needs to be displayed. A DataSet containing the full relational set of information required for editing may be retrieved for edit. A DataSet will also track changes and make bulk persistence of all changes simple when it is sent back to the business layer. However, DataTables and DataSets are weakly typed, simple structures that don’t fully represent the domain of an application.

Display vs. Modification w/ LINQ to SQL & Entity Framework

Modern software development paradigms involve rich domain models that accurately reflect a problem domain. Product objects represent products, Category objects represent categories, Categories contain collections of products, etc. Object graphs in a domain layer can be quite rich and complex. Things aren’t just sets of data, they are functional entities, replete behavior, continuity, integrity and relationships. The complexity of these rich object graphs has given rise to the use of O/R Mappers, which serve as intelligent data access layers capable of dynamically generating SQL statements and materializing object graphs for you, with minimal effort on the part of the developer.

LINQ to SQL is a basic, SQL Server only O/R mapper from Microsoft. While it does not provide super rich modeling capabilities, cross database support, or broad support for service-oriented applications, it does provide support for retrieving flat, non-structured data sets. L2S offers great support for “custom projections”, where specific pieces of information from various related entities in a domain model may be selected into an anonymous result set, or into a strongly type result set using a custom class.

Entity Framework is a rich, fully featured, cross-database O/R mapper from Microsoft with broad support for service-oriented applications. Entity Framework supports a wide range of entity to table mappings, giving it a degree of mapping flexibility that L2S just can’t approach. Unlike L2S, however, Entity Framework does not efficiently support custom projections. The current SQL statement generation pipeline makes the assumption that you are always querying for full entities or graphs of entities. The SQL generated for custom projections by EF can be quite inefficient, and at times, it can be incorrect and cause massive amounts of data to be processed by the database engine.

Right Tool for the Job

If you are attempting to simplify your projects, reduce overhead, and improve efficiency for your applications, make sure you pick the right tool for the job. Use object graphs when they provide useful benefit, and use simple, flat result sets when you don’t need the full richness of an object graph. Find and use tools that will support these patterns in an efficient way. LINQ to SQL may not offer rich mapping like Entity Framework, but it does support flexible querying and allows developers to retrieve the information they need with minimal resources. Methodologies like DDD encapsulate and isolate data access from the rest of an application through the use of Repositories. Some of the additional benefits of EF could be spoofed internally in Repository classes, allowing L2S to be used in the interim until EF matures more and offers better support for data display concerns.

Monday, September 22, 2008

Domain Driven Design

Not too long ago, I encountered a failure in an architecture I had designed, and was trying to implement. The general design of the architecture was fairly good. It followed strict coding guidelines, implemented standard patterns, introduced some new patterns to solve common business problems, and, due to the very patterned nature of all the classes, was very code-generation friendly. This architecture allowed my team and I to move beyond the existing code base of random, chaotic code that solved individual, daily technical problems (or, as it more often was, provided a workaround instead of actually solving the problem). We established good coding practices, became object oriented, and came to a full understanding of the applications we were developing.

Due to the success of our architecture, we were finally able to move past mundane bug fixes that continually plagued us. We were now able to write new code to implement new features. Everything was great…until the database schema changed. This was where the weakness of our architecture became apparent. In the process of trying to make our application more structured, layered, and object oriented, I had failed to account for the need for fully independent adaptability in both our business objects and database schema. Our business objects were not persistence-ignorant…rather, in my efforts to build a system that would perform very well, our business objects became tightly bound to their knowledge of the database schema. Suddenly the nice, clean, simple world I had created exploded, and the daily grind became a process of updating our database to support a new feature, then the grueling task of updating all the dependant code to match. We had code generation in place, via CodeSmith, but that only alleviated some of the pain (and also created some of its own). Ultimately, the only conclusion I could come to was that I had failed. I achieved a structured, object oriented architecture, but still failed to provide a platform upon which we could solve business problems.

I spent the next several months trying to figure out ways to solve the problem, during which time we upgraded to Visual Studio 2008. I immediately fell in love with LINQ, and dove into planning for a new architecture built around this wonderful new technology. It was during my research on LINQ to SQL, and early information about Entity Framework, that I came across the various discussions on the concept of Persistence Ignorance, and the need for it to be supported in any decent O/R mapper. My explorations of PI lead to explorations of modern architectures, which eventually lead to my discovery of a most wonderful book: Domain-Driven Design: Tackling Complexity in the Heart of Software, by Eric Evans. I can say nothing less than that this book has completely changed the way I look at software development. In my attempts to refactor our applications and processes to follow the guidelines of DDD, I have learned more about both the business of developing software, and the business of the company I work for, than any other time in my career.

There is a lot of buzz these days about the various software development methodologies or pseudo-methodologies, and how they can help speed up software development and improve the quality of code. You always hear about testability, adaptability, quality improvement, distribution, all those common-sense things that can help solve the technical problems of a technical profession. The thing that intrigues me about DDD is that the emphasis is not so much on solving technical problems encountered by developers (however it does address them), as it is on connecting with business (domain) experts, learning how to communicate with those domain experts, and collaborating with them on the construction of a cohesive model and common language that reflects the problem domain and the business processes involved. Because that’s ultimately what we as developers are all about…solving business problems.

The bulk of my efforts recently have been put into learning Entity Framework, and applying that new-found knowledge towards creating a new architecture for our projects. The goals this time are focused less on solving technical problems, and more on solving business problems. In addition to providing a framework of collaboration between developers and domain experts in the form of domain modeling, DDD provides a core set of patterns and guidelines to help implement that model in code, and write software that solves the problems at hand. This new approach to software development has been quite refreshing, and when DDD practices are applied, I have found it easier to write simple, concise code that directly solves the problem at hand without any extraneous fluff. The software patterns advocated in DDD: Tackling Complexity have also helped me to develop code that is unit-test friendly, even when using LINQ to SQL or LINQ to Entities.

For anyone who is interested in simplifying their life as a developer, and getting to the heart of the problem, I highly recommend Domain Driven Design. It is a refreshing approach to software architecture and design, and offers some great solutions to common problems encountered in any software project. DDD isn’t bound to any specific software development methodology, either, so regardless of whether your Agile, XP, RUP, or a home-grown methodology, DDD still has a lot to offer.

Alongside my discussions on Entity Framework and other Microsoft technologies, I hope to document some of my experiences with DDD and its use with those technologies.

http://www.domaindrivendesign.org/index.htm

Tuesday, September 16, 2008

Having navigation properties and keeping your basic properties too!

When LINQ to SQL first hit the scene, software development on the Microsoft platform changed forever. We all finally had a nice, albeit simple, O/R mapper that allowed us to flexibly query an object model in a wide variety of ways. Then Entity Framework appeared on the scene, promising better mapping capabilities, a better designer, and a much richer framework for O/R mapping overall than L2S would ever provide. Well, so we hoped. EF isn’t without its quirks and limitations.

One of the limitations of Entity Framework is its inability to have a single schema-level property mapped to more than one conceptual-level property in the same entity. In L2S, you could easily have had a basic property for a foreign key, while also having a reference property (or navigation property in EF-speak). This allowed a very wide variety of queries, and made it easy to update single entities and submit those changes to the database without needing related entities to be loaded as well. For the most part, EF still supports all that, but in a much more round-about way, involving the creation EntityKey instances and the like.

Currently, if you wish to set a foreign key value on an entity, you need to do something akin to the following:

   1: using (ObjectContext db = new ObjectContext())
   2: {
   3:     EntityWithFK entity = db.EntitiesWithFK.First();
   4:  
   5:     // Create an entity key to represent the foreign key
   6:     EntityKey key = new EntityKey("Entities.Foreign", "KeyColumn", 1);
   7:  
   8:     // Change the foreign key without loading the associated object
   9:     entity.ForeignReference.EntityKey = key;
  10:     
  11:     db.SaveChanges();
  12: }

Simple, or so it seems. If you are working with an entity that already had a related entity set for that key, then you would also need to clear any existing entity before setting the new key. You can imagine what happens when you have a composite foreign key to an entity with a composite primary key. Wouldn’t it be nice to just set the key properties directly, like you used to be able to in L2S? Well, its not too difficult with partial classes and a little bit of encapsulation. A single-column key is easy:

   1: public int ForeignKey
   2: {
   3:     get
   4:     {
   5:         // Get the first value of the entity key values, its a single-column key
   6:         EntityKey key = ForeignReference.EntityKey;
   7:         int keyValue = (int)key.EntityKeyValues[0].Value;
   8:         return keyValue;
   9:     }
  10:     set
  11:     {
  12:         // Clear the reference of any existing value before changing the key
  13:         if (ForeignReference.Value != null)
  14:         {
  15:             ForeignReference.Value = null;
  16:         }
  17:  
  18:         // Create new entity key and replace the reference one
  19:         EntityKey key = new EntityKey("Entities.Foreign", "Key", value);
  20:         ForeignReference.EntityKey = key;
  21:     }
  22: }

If you have a composite key, things are just a tiny bit trickier. You can’t create an entity key in pieces, you have to create it all at once. Just make some read-only properties to get the individual key values, and create a method to replace the whole key at once:

   1: public int ForeignKeyA
   2: {
   3:     get
   4:     {
   5:         EntityKey key = ForeignReference.EntityKey;
   6:         int keyValue = (int)key.EntityKeyValues[0].Value;
   7:         return keyValue;
   8:     }
   9: }
  10:  
  11: public char ForeignKeyB
  12: {
  13:     get
  14:     {
  15:         EntityKey key = ForeignReference.EntityKey;
  16:         char keyValue = (char)key.EntityKeyValues[1].Value;
  17:         return keyValue;
  18:     }
  19: }
  20:  
  21: public EntityKey SetForeignKey(int keyA, char keyB)
  22: {
  23:     // Clear any currently referenced entity for the foreign key
  24:     if (ForeignReference.Value != null)
  25:     {
  26:         ForeignReference.Value = null;
  27:     }
  28:  
  29:     // Create a new entity key for a composite key
  30:     EntityKey key = new EntityKey("Entities.Foreign",
  31:         new[] {
  32:             new EntityKeyMember("KeyA", keyA),
  33:             new EntityKeyMember("KeyB", keyB)
  34:     });
  35:  
  36:     ForeignReference.EntityKey = key;
  37:  
  38:     // Return the key so other code may use it
  39:     return key;
  40: }

There you go, a nice, clean, simple, encapsulated way to manage your foreign keys when you don’t have an instance of the entity for the other end on hand. (All we need now is for it to be automatically generated…perhaps in another article). The great thing about Microsoft frameworks…if they didn’t get it perfect themselves, at least we have the flexibility to perfect it ourselves.