Saturday, August 29, 2009

Budget App - Part 2 : Introducing WCF Into The Picture

Considering that I've been looking at the job market for almost a month now one commonality that all the jobs have had is the requirement for WCF experience. Unfortunately this is something that I haven't had the luck of working with commercially. Even though I've been confident that it would be easy enough to get up to speed, it's difficult to persuade potential employers to see things the same way.

Hence the first thing I wanted to add to my budget application was some WCF services for data access. While my intent is not to go into too much detail on what the ABC's are of WCF services, each of these will, hopefully, be illustrated as I describe how I built and deployed my first WCF services.

Firstly, let's quickly have a look at the changes that were made to the folder structure of the solution.


A number of new projects have been added to the solution, namely: Budget.Business, Budget.DataAccess, and Budget.WCF.

Upon adding a WCF Service Application Visual Studio kindly starts things off for you by including an interface and corresponding class file. It's these files that make up part of the Contract. The interface has the attribute [ServiceContract] and each method definition has the attribute [OperationContract]. Here's what mine looks like.

[ServiceContract]
public interface IIncomeService
{
    ///
    /// Gets all Income objects
    ///
    [OperationContract]
    IList GetAll();
    ///
    /// Gets a single Income object by it's ID
    ///
    [OperationContract]
    Income GetIncomeById( int ID );
    ///
    /// Saves a single Income object, either inserting or updating the records values
    ///
    [OperationContract]
    void Save( Income income );
}

Without these the service will not work. Now you may be wondering what the other part of the contract is? We've now defined what the service can do, now we need to define what can be passed through to the service. This is achieved by adding some new attributes to our data objects. Each class that is to be passed through must have the attribute [DataContract] and each property that is to be access within the class must have the attribute [DataMember]. The data classes to be used were defined in my Linq-to-SQL file which is automatically generated, so any modifications manually made to it would be lost the next time the file refreshed itself. This next bit would have taken me quite a while to figure out were it not for google. You can set the attributes against all the Linq objects by setting the SerializationMode of your DataContext object to Unidirectional.



You will then end up with generated code similar to this:
[Table(Name="dbo.Income")]
[DataContract()]
public partial class Income : INotifyPropertyChanging, INotifyPropertyChanged
{
    ...
    ...
    ...
    [Column(Storage="_IncomeId", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true, UpdateCheck=UpdateCheck.Never)]
    [DataMember(Order=1)]
    public int IncomeId
    {

Once the Contract has been defined the next step is to use them. Here is where all the configuration in the web.config is required. Thankfully as you add services to your project the app.config file is automatically updated by the WCF Service Application project. All you need to do is copy the <system.servicemodel> tag into the appropriate location in the your web.config file.

You should now be able to add the WCF service into your main application via a Service Reference, how ever this is not what I wanted to do. I wanted an extra layer to any business logic I may need and possibly to perform my validation there. Just add another class library, add a Service Reference to this. The app.config file of this new project also contains some extra tags that should be copied over to the web.config of you main app.

Providing you use the WCF Test Client and the default Visual Studio WCF Service Host you should now be able develop your services to your hearts content.

Enjoy!

Thursday, August 27, 2009

Budget App - Part 1

One thing I seem to find a hassle is conjuring up some sort of realistic application to apply all the really cool technologies and techniques to.

I figure that something that I'm really bad at is budgeting. Whether this is due to my wife (I hope she doesn't read this) or it's just me no one will ever know. A small budget application was in order and considering that I actually want to use this soon I thought I'd whip a little budget up and then slowly enhance it with some of the technologies I haven't had the luck of working with yet, such as WCF. I'll apply development techniques such as Test Driven Development (TDD) as much as I can and let you know what sort of issues I face.

Here's a snap-shot of the little application.


So what features/technologies have I used include:
ADO.Net Entity FrameworkLinqDevExpress web controls v2009 vol2

Data Access
Having used the Linq-SQL before I thought I'd initially have a play around with the ADO.Net Entity Framework for this app. I must say though (without knowing all the nuances of the Entity Framework) that I prefer Linq-SQL.



using ( budgetEntities = new BudgetEntities() )
{
    spendings = ( from s in budgetEntities.SpendingSet
                  where s.DateSpent.Month == DateTime.Now.Month && s.DateSpent.Year == DateTime.Now.Year
                  orderby s.DateSpent descending, s.Timestamp descending
                  select new SpendingView
                  {
                      SpendingId = s.SpendingId,
                      Description = s.Description,
                      Amount = s.Amount,
                      DateSpent = s.DateSpent,
                      Category = s.MthCategory.Category.CategoryId
                  } ).ToList();
}

Here I am directly accessing the Data Context from the UI. This will be changed in the next post. What pattern will be used I'm not entirely sure, but it will definitely involved WCF.

User Interface
I've decided to try out the Developer Express suit of user controls for this. I've used others in the past but they just didn't cut the cake and considering that DevEx are always winning first place for all the 3rd Party Control categories I figured they'd be the best bet.

The controls that I used were the ASPXGridView, the ASPxTabControl, the ASPxRoundPanel and the ASPxCallbackPanel.



I started out using their v2009 vol1 release, but have since upgraded it to v2009 vol2.

Enjoy!

TFK