Programming

Built-in “yes_no” type for Hibernate

Posted in Programming on October 21st, 2008 by Chris Stewart – Be the first to comment

Today I was working on some code and ran across an interesting situation while trying to create a Hibernate mapping file for an entity. My entity has a property called isAdministrator that has the type boolean. The corresponding column in the database table represents true/false with the character Y or N. At runtime, Hibernate would toss casting exceptions as I’d attempt to query the database for this field.

I quickly realized where my problem was and thought I’d need to write some ugly code to make this work. After a few quick Google searches I discovered the “yes_no” type. By adding this attribute to my XML configuration for the mapping, Hibernate automatically takes care of the translation process. Neat, huh? In case you’re wondering, there’s a “true_false” type in there as well.

<property name="isAdmin" column="isAdmin" type="yes_no" />

You should be using Dependency Injection

Posted in Programming on October 6th, 2008 by Chris Stewart – Be the first to comment

Over the last few months I’ve been getting myself reacquainted to the Java world. I built a reasonably simple JavaServer Faces application. I tend to instinctively follow Domain-Driven Design and separate levels of abstraction reasonably well. Towards the end of the v1 release, I was informed that we’d need a build for another environment of ours. The front end code remains unchanged for the most part. What would be radically different was the service layer code I wrote that had an implementation specific to our environment.

Time was a concern, as it always is, and I nearly forked the code base to begin on a version specifically for that second environment. Before I did, I took a crack at Dependency Injection with Google Guice to see if that could be a potential solution. With my original implementation, I had a set of controllers that were marked as JSF beans. Each controller had instances of service classes as needed. Those service classes interacted with the entities and performed persistence by way of Hibernate (Yeah, I know, I should have split those). Anyway, as you can see, there was a direct dependency on the service classes and my controllers. Everything is tightly coupled in this situation.

What I did was create a set of interfaces that described the functionality in my service classes. Essentially a one-to-one, an interface for each service class. I refactored my controllers to use the interfaces in place of the concrete service classes themselves. Next, I created an “environment two” set of service classes that implemented the newly created interfaces. So, with the help of Google Guice and some refactoring, I had a single code base that adjusted its implementation based on the environment it was in and a code base that’s fully ready for unit testing.

Dependency Injection is important. In the most simplest of terms, it gives you loosely coupled software components. Having that gives you flexibility. Flexibility in the sense of what I described, or testability, or an architecture that can be reused without rewriting a lot of code. You should be using it, in every project, if for nothing more than a simplistic approach to software engineering.

Is Agile right for every project?

Posted in Programming on October 3rd, 2008 by Chris Stewart – Be the first to comment

With “every” in the question, you should immediately be suspicious. :)

My company has a software product that is in the neighborhood of 12 years old. I’m sure it has gone through a number of revisions, some of which were probably major. FishEye is telling me that our repository has several million lines of code in the trunk. The product is used by hundreds, maybe thousands, of people every day for critical tasks. Should we follow agile practices? Maybe. Fact is, we don’t, and it’s not a bad thing.

Many developers swear up and down over agile methodologies for developing software. In many respects, I agree with that mindset. Obviously there are always benefits to both sides. In my opinion, a large portion of the reasoning in which methodology to adopt lies in the industry you serve. Take the financial business for instance; does it make sense for weekly iterations with online banking software? Again, maybe, but I lean towards no because of the risk involved. What about mission critical health care systems? Same idea.

Now, you may not be agile in your deployment cycle in these industries and that’s understandable. But, you can certainly follow agile practices in your internal development cycles. Instead of pushing weekly iterations to production, push them to your QA or staging environment. You can still follow the processes of continuously integrating your work with your peers. You may not push to production every week, but your stakeholders might need to be involved in the process so you don’t get too far down the wrong path. In both of the industry examples above, do you think the teams could benefit from weekly iterations to a testing environment? I hate to say it again, but, maybe.

I believe that having daily meetings, pushing for progress in small steps, and developing features in isolation lead to a more refined development team. Doing these things reduces the amount of wasted cycles, keeps developers pushing forward, and helps them see progress with small steps of success.

My takeaway from this is to evaluate your needs, project environment, and industry. Every situation is different and a single methodology doesn’t work for each instance. Find ways to boost team morale and keep the project moving forward.

Back to Java, With a dash of JSF

Posted in Programming on June 2nd, 2008 by Chris Stewart – 3 Comments

My first exposure to real programming was with the Java platform. I spent my first two years in technology learning Java and that platform’s web-specific frameworks for developing applications. At that time JavaServer Pages (JSP) was a relatively new technology and Servlets had been the real work horse. Of course now the landscape has changed and web frameworks have dramatically improved. It’s laughable to think of presentation code being generated by Servlets these days, legacy applications excluded, of course.

The hot new framework at the time was Struts. The idea of MVC was not exactly new but bringing it to the web was a very interesting idea. Struts was pretty much the greatest thing since sliced bread to every Java developer doing web programming. Personally, I just couldn’t grasp it. I was too new to programming in general and that framework was simply over my head. I stuck with my JSPs and Servlets.

Shortly after my time ended at that company and I started working at VCU, I was exposed to Microsoft’s .NET framework. This was roughly in the spring of 2003. Even though my bachelors program was focused around Java, I spent a lot of time devoted to .NET. The Imagine Cup project consumed a year of my life and was completely in .NET. I think at one point we had counted nearly 100,000 lines of code in that project. One of my higher level courses was somewhat language independent and I ended up using .NET there as well. I essentially had moved on to what I considered greener pastures and had no interest in looking back.

After school, which was winter of 2005, I had a number of jobs that were all specific to .NET. I worked with GIS technology, built some very interesting AJAX/C# applications, and rebuild Richmond.com. Simply put, I know the platform very well and had really invested myself it in.

Everything changed one morning last September. I was sitting in a room with my buddy Matt and the CEO of his company. Originally I was there to chat with the CEO and find out if there were any areas I could help the company in, on a consulting basis. By the end of our conversation, I had a job offer on the table which I happily accepted. The kicker was that the company was a Java shop and it had been a number of years since I had done any real Java work.

Since the start of my new job I was actually doing everything in C#. I built a new company web site, created an automated way to transfer and validate data, and a few other miscellaneous things. It wasn’t until a month or two ago that I actually picked up Java again for something real.

What I quickly noticed is how much had changed since I left Java. Nothing! Struts is still around, only with a new version number, JSP and Servlets don’t appear to have changed at all, and now there are dozens of web frameworks out there trying to fill a void I can’t seem to find. The only “Sun sponsored” framework I’ve seen is JavaServer Faces (JSF).

In my search for a framework I looked at Spring, Struts 2, and JSF. I decided to go with JSF. Where JSF stands out in comparison to the others is in the simplicity of getting started and the ease of use. Struts has been and will always be a monster to set up and code with. Spring is a monster of a framework that pretty much does everything you could want. That’s all well and good, and a lot of people swear by it, but for what I wanted it was just too much to take in. I just wanted to get started on a web application. I wasn’t interested in AOP, Dependency Injection, ORM tools, and the other things Spring includes.

JSF provides a fresh and simplistic approach to MVC. Frankly, a framework that didn’t follow MVC wasn’t an option for me. Anyone who knows me knows how nutty I am over architecture, design patterns, and elegant solutions. JSF is a great solution for MVC, while maintaining an environment where I can understand how everything works. The lack of “automagic” is a good thing and I appreciate JSF for it. Working with JavaBeans in JSF is a dream and a fundamental aspect of the framework. The abstractions of Models and Controllers aren’t enforced by JSF, but rather implicit by design. It’s my call on how I structure my Java code and I simply inform JSF by using the faces-config.xml file. JSF gives me an easy environment to implement a “domain driven” model, my interpretation of it at least. I’ve stuck with that programming model for this project and it has worked out incredibly well.

What’s really shocking to me is that JSF has not caught on in the Java community as I would have expected. It really goes to show you how important the community is in Java. Had this been in the Microsoft camp, it would have been devoured and sword by immediately. In that world, Microsoft is your dictator and what they say goes. Sure, there are successful open source projects, but it’s nothing like the Java community. There are so many options it can almost become a problem but that’s another subject I guess.

I’ve enjoyed JSF, a lot. It has it’s problems. For me, most notably, the lack of URL importance. I think a lot of people have not bothered to give JSF the proper look it deserves. Many pass it off as being complicated. I’m very surprised by that as I’ve found it extremely easy and open to work with. If you’re starting a new web project and want a fresh look at web development, take a peek at JSF, you might like what you see. I sure have.

Tell IE8 to render like IE7

Posted in Programming on March 10th, 2008 by Chris Stewart – 1 Comment

With the upcoming release of Internet Explorer 8 (IE8) and the recent flip-flop on web standards support, a lot of web developers will likely have some upgrade pains once IE8 does hit the mass market. I have a few spacing issues I need to address but am simply swamped with other things at the moment. Until I can get to those issues I wanted a solution to tell IE8 to render the site as if it were being viewed in IE7. After some searching around, I came across this meta tag that will do the trick:

<meta http-equiv=”X-UA-Compatible” content=”IE=7″ />

Resource for ASP.NET

Posted in Programming on January 5th, 2008 by Chris Stewart – Be the first to comment

I want to create a community resource for ASP.NET knowledge and help. I’ve been writing web applications in ASP.NET since 2002 and have learned a bit about the technology along the way. What I’d like to do is either branch out this blog into something for ASP.NET related content only, or perhaps something different altogether, like a public wiki, screen cast series, or some kind of web site that would help enable new ASP.NET developers in their learning experience. So as you can tell, I’m very open as to what I want this to turn into, but I know I want to create a helpful utility from my experiences.

Speaking of ASP.NET, I ran across CodeCampServer today and immediately subscribed to the mailing list. CCS is an open source effort to build a web application for code camp management using some of the cutting edge ASP.NET technology, such as the ASP.NET MVC framework.

Oh, and one last ASP.NET note, we deployed our new company website yesterday. Check it out at www.xperts.com.

ASP.NET MVC Framework

Posted in Programming on December 31st, 2007 by Chris Stewart – Be the first to comment

Recently announced is a MVC framework for ASP.NET. The MVC architecture is what I enjoyed most in Ruby on Rails, so I’m very excited to see it exist as a supported feature of a future .NET Framework version. Scott’s blog post explains what this is and means far better than I could so check it out if you’re a geek. :D

LINQ in an N-Tier architecture

Posted in Programming on December 27th, 2007 by Chris Stewart – 3 Comments

LINQ is a new addition in C# 3.0. The technology allows for a language-integrated query mechanism, hence LINQ. Which essentially means you can query a data store using built-in language features. No longer is it necessary to write elementary SQL statements to access your database. With LINQ to SQL, you can create an object-relational mapping using the designer built in to Visual Studio 2008. The end result is a set of classes that map to tables represented in your database. The DataContext is the other main pillar to LINQ. Its purpose is to facilitate the communication between the entity classes generated by the O/R designer and your physical database.

I’ve been toying around with LINQ and thought it would be a useful technology for me in the future. It removes the need to build out all of the fundamental SQL queries for data bound objects. That alone makes it a worthwhile topic to read up on. What I noticed while cruising around the web is that every example on LINQ is very simplistic and not representative of a real world scenario. I’m sure it’s because the focus is on LINQ and nothing else, but eventually you need to see how it fits into a real enterprise class system.

With planning underway in a new project, I thought it was the perfect time to investigate LINQ further. I just finished up a small project where I used LINQ in a minor and trivial way. It was about as complex as the examples you see online. But now I needed to design an architecture in the more traditional n-tier sense, something that would meet the needs of a complex web application.

Using the traditional layers, I drafted up my architecture and tried to find LINQ’s place. Remembering the entities and DataContext generated by the O/R designer, it was easy to see that the entities generated fit in what I call the Entity Layer. These skeleton objects simply represent database tables and are used by the business and data layers. Think of them as transitional objects between the various layers. The DataContext stood out though. It’s data-centric nature dictates that it should exist in the data layer, not the entity layer. However, since these two classes of objects are generated by the O/R designer, I can’t easily separate them without redoing it each time the model is generated. We’re essentially collapsing the entity and data layers, or at the very least, letting the DataContext slip under the radar. But remember, all of the data access in LINQ is using that DataContext class. So your data layer may be forming the LINQ queries but the entity layer ends up executing them since the DataContext lives there. Not exactly the separation of concerns I was architecting.

I’ve yet to find an elegant solution for this problem. I’m very happy with the architecture I have minus this one key element. If anyone out there has been in this situation before, of trying to find a solid design that includes LINQ, please post and give me your thoughts. I’ll be sure to post my solution once I come up with something worthwhile. :D

Microsoft .NET going open source?

Posted in Programming on October 4th, 2007 by Chris Stewart – Be the first to comment

Well, not exactly but Scott Guthrie announced yesterday that Microsoft will be releasing the source to a lot of the .NET Framework with the release of .NET 3.5 later this year.

I’m excited about it for a number of reasons. The biggest plus Microsoft is giving for releasing this code is the ability for developers to debug all the way to the root of the framework. That will certainly help but I’m more excited about it another reason. I enjoy developing frameworks for my solutions with the idea that I’ll reuse them in the future. Obviously the .NET framework is a wonderful example of that. I’m excited to have the opportunity to look through what Microsoft has put together conceptually so I can learn from it to refactor and build new frameworks for my use.

One tool to “Get Things Done”

Posted in Programming on September 18th, 2007 by Chris Stewart – 4 Comments

Last week we discussed “Getting Things Done” on Kill The Desktop. I’m in need of this kind of process for my work life. I’m as guilty as the next guy for using my Outlook inbox as a to-do list. It hasn’t really gotten me into trouble, but I do forget things and simply have a hard time understanding everything I need to do in one glance.

What makes this harder for me is that I’m a Mac user. I can’t run Office natively on my Mac and I’m OK with that. I use Mail.app and iCal right now and my plan has been to use them once my new job starts. But again, I’m not really doing anything different to make myself more productive.

I started to search around a bit to see what I could find and ran across Chandler. That project was written about in a book I recently read called “Dreaming In Code“. Highly recommended! I happily read through their site to see if Chandler could help me. Sure enough it’s platform independent and follows the GTD principles. The problem, as is well described in the book, is that Chandler still hasn’t hit 1.0. Not that I care much about a version number, but if you read the site it explains that one release may not have a data format compatible with the next. Bummer.

Anything out there I should check out?