If you are new to Windows Phone or WP7 development like me, probably you will also have one question in your mind and that is – What is the best way to organize my projects. I did my first WP7 application, hosted it in the marketplace but didn’t pay much attention to details. Now I am writing my second app and started to think on separation of concerns. Now when I look back at my first app source code it didn’t take care of the separation of concern at all. I had everything in one project. So if you are one among those perfectionist who wants to do things right, then stay tuned and read on.
Separation of Concerns:
One of the jargons that is widely used in architectural talks is Separation of Concerns. Here is what Wikipedia defines Separation of Concerns as:
Separation of concerns (SoC) is the process of separating a computer program into distinct features that overlap in functionality as little as possible. A concern is any piece of interest or focus in a program.
So as you can see Concern is a piece of interest or focus in a program. What are those concern you may ask, well here are some of the things that I personally treat as concerns:
View – These form the Visual screens/windows of a project.
Presentation – I consider View Model as a presentation concern and would like to keep it separate from the View project
Services – In most of the architecture the Business Logic Layer or BLL is often termed as Service Layer. This is not the WCF service layer rather the Business services within the app. These normally orchestrate the proceedings between presentation and data layer
Data – As the name says, this is the data interaction layer.
From my opinion the above form the basic concerns of a any project not only a WP7 app architecture. Now that we know the different concerns, lets see how these can be achieved in WP7 world.
Setting the stage:
Lets go ahead and create a simple Windows Phone 7 application which we will use a reference in the sections to come. Fire up a Visual Studio IDE and create a Silverlight for Windows Phone template based Windows Phone Application project. Note that I am creating this project against Windows Phone 7.1 SDK.
After the above step, we will now have a empty Windows Phone Application. Now lets see how to separate out the concerns.
Adding Class Library Projects:
Now having worked with .NET for over a decade my first instinct was to add a Class Library Project as usual. So I went ahead and added a class library project as below. Lets call the new project SoCApp.Presentation. Intention is to house all the ViewModels in this project.
Here is how my solution looks like now:
Now lets add a reference of SoCApp.Presentation in SOCApp.When we try to add the reference, Visual Studio will throw back an error. Here is the error:
Well, what we created was a standard .NET Class library project. But it seems we need to create a Silverlight for Windows Phone Project. It was very natural of me to try and add a standard class library project within Windows Phone solution. So how do we create a Windows Phone based class library project. Well lets look at that action in next section.
Windows Phone Class Library:
So we cant add a standard .NET class library project to separate out the concerns. What we need is another Windows Phone project to added to the solution. Lets see how that can be done. First lets delete the project SoCApp.Presentation we created in the previous step. Right click on the solution and select Add > New Project. In the project template window select Silverlight for Windows Phone and Windows Phone Class Library project. As the name goes this is a class library project which targets Windows Phone environment.
Now if you add reference to this new project all is well. Here is how my solution looks like now:
Similarly you can create the rest of the concerns as a separate Windows Phone Class library and reference it in the main project.
Whether you are creating a market place app or practicing Windows Phone development, it is better to understand and keep in mind some of the architectural concerns. One such architectural concern is Separation of Concerns. So we saw how you can separate out concerns in a Windows Phone development project using the Windows Phone Class library. This instance did really happen to me and I had to figure out where I went wrong for a moment. Everyday is a learning for me and hope I did convey a new lesson to you my fellow reader. Hope you find this useful and informative.
Till next time, as usual – Happy coding. Code with Passion, Decode with Patience.