Silverlight 4 Simple Project Part 2 (WCF Service)

Silverlight 4 Simple Project Part 1 (Overview & Database)

Here I will create a WCF Service as a middleman for our Silverlight <-> Alkitab Database that I mentioned in Part 1.

New Project & LINQ to SQL

To start, I will create a WCF Service Application from Visual Studio 2010 (I didn't choose WCF Service Library because we will host it as a 'web' application in the future).



Then right-click the project, Add New Item -> LINQ to SQL Classes. We want to drag our two tables from the Server Explorer into the dbml file. This will provide us the DataContext we require for performing our queries.



WCF Service

Next, rename Service.svc and IService.cs to friendlier names like AlkitabWebService.svc and IAlkitabWebService.cs

IAlkitabWebService.cs is the interface class for our service. This will hold all the OperationContract (functions) declarations and any DataContract (web class) that you want to create along with its DataMember (class property). Here I have sets of operations and AlkitabSnippet class that will hold verse and content:

[ServiceContract]
public interface IAlkitabWebService
{
[OperationContract]
string GetContent(string bookTitle, int bookChapter, int bookVerse);

[OperationContract]
ObservableCollection<AlkitabSnippet> GetAlkitabSnippet(string bookTitle, int bookChapter, int bookVerse);

[OperationContract]
ObservableCollection<AlkitabSnippet> GetAlkitabSnippets(string bookTitle, int bookChapter, int bookVerseStart, int bookVerseEnd);

[OperationContract]
ObservableCollection<string> GetBookTitles();

[OperationContract]
ObservableCollection<int> GetBookChapters(string bookTitle);

[OperationContract]
ObservableCollection<int> GetBookVerses(string bookTitle, int bookChapter);
}


// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class AlkitabSnippet
{
string verse;
string content;

[DataMember]
public string Verse
{
get { return verse; }
set { verse = value; }
}

[DataMember]
public string Content
{
get { return content; }
set { content = value; }
}
}


In the AlkitabWebService.svc.cs, we implement all the service functions that we need to get data out of the database. Here are two of the functions:

public class AlkitabWebService : IAlkitabWebService
{
public ObservableCollection<AlkitabSnippet> GetAlkitabSnippets(string bookTitle, int bookChapter, int bookVerseStart, int bookVerseEnd)
{
ObservableCollection<AlkitabSnippet> snippets = new ObservableCollection<AlkitabSnippet>();

AlkitabDataContext db = new AlkitabDataContext();

var query = from c in db.Alkitabs
join b in db.AlkitabBooks
on c.book equals b.bookno
where b.booktitle == bookTitle && c.chapter == bookChapter && c.verse >= bookVerseStart && c.verse <= bookVerseEnd
select new { verse = c.verse, content = c.content };

foreach (var e in query)
{
AlkitabSnippet snippet = new AlkitabSnippet();
snippet.Verse = bookTitle + " " + bookChapter + ":" + e.verse;
snippet.Content = e.content;
snippets.Add(snippet);
}

return snippets;
}

public ObservableCollection<string> GetBookTitles()
{
ObservableCollection<string> booktitles = new ObservableCollection<string>();

AlkitabDataContext db = new AlkitabDataContext();

Table<AlkitabBook> AlkitabBook = db.GetTable<AlkitabBook>();

var query = from a in AlkitabBook orderby a.bookno select a.booktitle;

foreach (var e in query)
{
booktitles.Add(e.ToString());
}

return booktitles;
}
}


One question you might ask (I asked this to myself as well when I was a total newbie) is what is an ObservableCollection. WCF Proxy Class use this type of collection by default. However, this collection is different from other type of normal collection, lets say List, in a sense that it provides notification when items get added, removed, or refreshed and when the items property is changed (it implements INotifyCollectionChanged and INotifyPropertyChanged).

This is required for Silverlight to get the notification any change so that it can update all the UI elements associated with the collection.

That's all we need to do for our WCF Service :)

In the next post we will create our Silverlight application that references this service. I will start with the explanation of MVVM model, the popular pattern of building your application.

Silverlight 4 Simple Project Part 3 (MVVM Pattern)

Comments

Popular posts from this blog

SharePoint 2013 anonymous access add attachments to list item

CRM Plugin - Parent and Child Pipeline

Sitecore custom publish agent from specific node and at a specific time