Tuesday, September 11, 2012

Great MVVM, WPF, SOA and EF4 Articles

I was asked to put together some links to articles that would help our IT department ramp up on some of the patterns and technologies we're using. By the time I was done with the email, I realized it might be of use to others outside our organization so part three of my MVVM with WPF series was born. There are many approaches and I highly recommend researching beyond this handful of articles, but for anyone not yet familiar with these concepts I hope they help.



The Model-View-ViewModel (MVVM) pattern helps to cleanly separate the business and presentation logic of an application from its user interface (UI). This separation is key for testability, maintenance and scalability.

Here’s an introduction to MVVM which illustrates how it leverages WPF data binding and commands.

The main difference between what we’re proposing and how they define MVVM in the previous articles is, by using a convention-based framework such as Caliburn.Micro and events/commands to wire up the View and ViewModel, they are completely decoupled allowing for better testability and future scalability. The Caliburn.Micro wiki has a good introduction to the functionality it provides.

This article gives a good introduction to Attribute Based Validation which allows us to keep validation logic down in the business layer and entities where it can then bubble up and be reflected in the UI.

If there’s interest in going deeper, here’s an article on using a Service Oriented Architecture (SOA) to decouple service providers from consumers allowing for greater reuse and insulating the application from core business logic.

Finally, a  series of articles outlining the use of a Generic Repository with Entity Framework 4 (EF4) to reduce the amount of plumbing that must be written and maintained for data access.



See also:
MVVM with WPF Series Part 1. Disconnected WPF Applications
MVVM with WPF Series Part 2. WPF MVVM with WinForms Controls

Friday, August 17, 2012

How much is experience worth?


During the Facebook IPO in May, UBS lost more than $350 million as a results of a glitch in their systems that re-sent buy orders and accumulated 40 million shares that would have to be sold at a loss.

As a software developer, it's not difficult to imagine the type of glitch that might cause such an issue. Something as small as comparing a value to null or improper error handling can cost a company hundreds of millions of dollars, which begs the question, how important are the architects and developers creating these systems?

It's easy to understand the importance of experience when building something as physically monumental as a sky scraper. People's lives depend on the quality of design and workmanship. Unfortunately, many companies don't see their enterprise software in the same light. They balk over the cost of senior talent and begrudge resources for quality control, then end up spending much more in damage control when systems fail.

How can we nurture a corporate culture that recognizes the value of experienced talent and proactive quality control?

Friday, August 10, 2012

You're Interviewing Them

Most of us are raised to believe that a job interview is where a potential employer asks you a bunch of questions to see if you're right for the job, but did you know that the best candidates often spend as much time asking questions as answering them?

Asking questions shows that you're serious about the job and indicates potential employment is as much your decision as it is theirs. Questions about culture and coworkers are a great way to take the focus off money and help the interviewer realize you're looking ahead to what employment with them might look like. A potential long-term employee wants to know if it will be a good fit and somewhere they will be happy working for an extended period of time.

Once an interviewer has established that you have the skills to do a job, the next question on their mind is usually performance. It's one thing to have ability, but quite another to have work ethic and focus. By asking about their top performers you can show your interest in becoming one of them. By keeping the question open ended, such as "What is the most important quality you see in your top employees?", you can gain insight into what's most important to them and provide examples of how you've displayed said quality later in the interview.

Asking the right question isn't just about gathering information and doesn't replace the need for preparatory research. In fact, it's vital to have a basic understanding of the company's business in order to know what questions to ask. One example is the question, "How do you deal with [insert challenge here]?" Addressing a challenge facing the business not only shows you've done your homework, but it also indicates you're a problem solver and interested in being part of the solution.

Some questions depend on the interviewer. When being interviewed by someone from human resources it is probably best to keep questions general with focus on the overall company and position in question. With a hiring manager you're able to delve deeper into how they measure success and what they need most in an employee. The best opportunity for information gathering, however, comes when interviewing with a peer. Asking about their experiences with that company and manager not only provides valuable insight into the position, but shows respect for their opinion which can be very endearing.

Good questions don't take the place of being qualified and informed, but they are a great way to make yourself stand out from the crowd and determine if this specific opportunity is right for you.

Thursday, August 9, 2012

WPF MVVM with WinForms Controls

This is the second part of my series on using MVVM with WPF. In the first article I discussed architecting disconnected applications and how MVVM was a great fit for WPF smart clients as well as Silverlight web clients. Now we move on to the process of refactoring an old Windows Forms application to use this new architecture.

The main concern with changing an underlying technology is the initial investment in rewriting code. To help mitigate this issue, WPF has provided a WindowsFormsHost element that allows us to host WinForms controls inside of a WPF Page. Obviously this limits how effectively we can leverage other features provided by WPF, however with a few workarounds it's a viable solution to avoid having to completely rewrite those old WinForms controls.

How easy is it to implement the WindowsFormsHost?
<WindowsFormsHost><wfh:WinFormsUserControl x:Name="LegacyUserControl" /></WindowsFormsHost>

Okay, it's only slightly more complicated than that. You will need to add add a reference to WindowsFormIntegration and possibly System.Windows.Forms (if it is not already referenced).

Now the question comes to how we interact with this control from our ViewModel since we can't use WPF bindings in our WinForms control. We can bridge this gap by adding events to our WinForms user control.


Then we wire up the events to our DataContext in our WPF Page code-behind.


While I generally prefer to avoid using the code-behind, we also don't want our ViewModel to be aware of the internal workings of our View.

The end result is our Windows Forms control interacts with elements in our WPF Page.

demonstration

Download Demo Source Code

Tuesday, July 24, 2012

Disconnected WPF Applications

I was recently tasked with presenting a plan to turn an existing WinForms program into an enterprise level application. They wanted to stick with a Windows client, but since each form would already need significant rework to extract the business logic, I suggested moving to WPF using the Model-View-ViewModel (MVVM) presentation pattern.

MVVM is basically a type of Model-View-Presenter (MVP) pattern that makes use of declarative bindings. State and logic are stored in the Presenter (ViewModel) which presents an abstract view of the UI with no knowledge of what View will implement it.

With the Presentation Layer taken care of, it was time to address the heart of the Application. The immediate need was for a smart client, however the solution was still very young and continually changing so it seemed a Service Oriented Architecture (SOA) would provide the most flexability and reuse. The question was how a disconnected client would use services.

Microsoft used to offer an application block for this specific purpose. The Disconnected Service Agent Application Block helped maintain a queue of web service requests while working disconnected (offline), then submit them to the server when it became available. Unfortunately, it is no longer being maintained and I have not found a suggested replacement. My guess is that they would encourage use of Sync Framework, which is designed to synchronize databases and files, but can use custom providers as well.

We're using Sync Framework to replicate data down to the client for offline use, but to utilize SOA and keep business logic/validation on the server we need a way of queuing web service calls and resolving any conflicts that occur. I'm currently in the process of researching whether a Sync Framework custom provider would be a good fit or if the old disconnected service agent application block would be a good starting point.

The existing relies very heavily on SQL stored procedures. Fortunately, Entity Framework (EF4) supports calling the existing stored procedures so they wont all have to be rewritten immediately. The plan is to gradually move business logic up into the application layer and use entity model. I've also added a generic repository to allow mocking.

I welcome feedback and suggestions. If you've tinkered with custom providers for Sync Framework or have suggestions for how to best queue and resolve service calls I'd love to hear from you, either by leaving a comment below or on twitter (@gainesk).

Thursday, July 19, 2012

Water pressure issues? Let it flow!

Most of my articles are about technology and software development, however I recently purchased a new home and, as I worked on various things around the house I realized others might benefit from a few of the more common issues I've addressed. Some of these problems might seem simple to fix, however I figure there are others like me who are learning these things for the first time and might find my experiences helpful.

Problem: Water pressure is fine at first, but within a few seconds flow decreases significantly. The hot water never gets beyond warm, but both hot and cold suffer from the same decreased flow.

Diagnosis: It's important to distinguish between water pressure and volume. Low water pressure results in a lower flow as soon as valve is opened. This can result from undersized pipes or the use of valves/connections that reduce flow as well as simply receiving low pressure from the source. A volume issue results from some restriction farther down the line that allows a higher pressure to build over time, but once that pressure is released (such as in the first few seconds of running water) then the pressurized volume is exhausted and flow is limited by the restriction. If both hot and cold lines suffer from the same symptoms then the problem most likely exists on the main water line before the water heater.

Solution: Every situation is different, but in my case I found that the main cutoff valve at the meter box was not completely open. One and a half turns more and the restriction was gone restoring full flow to the house.

If you've run into a similar problem I'd love to hear about it. Comments are appreciated and you can find me on twitter (@gainesk).