Grid in Windows Phone 8 #windowsphone

When we are creating Windows Phone application first thing that we will be using is GridWindows Phone Grid

Grid, it’s our base for creating our world in devices. In this post I want to approach closer this topic. First thought is, how to use it ? and why it’s so powerful ?

Let’s me answer. It’s quite powerful mechanism because <Grid> can be our box for others items like (list box, text block etc.).

Moreover you can nest <Grid> inside <ListBox> or similar items.

<Grid> has got various members, for declaring rows and columns we have to use  <Grid.RowDefinitions> and  <Grid.ColumnDefinitions> by these members we can declare width of column, and height of row.

Example implementation :

wp_grid_col_row_def

As you can see I made three rows, and four columns. Now if you want place something on specific column or row, it’s similar to game “Battleship“. Let’s place text box, with help comes Grid.Row and Grid.Column.

<TextBox Grid.Row="0" Grid.Column="1" />

wp_grid_col_row_def_box

Notice: good practice – you should implement whole sizes at this step (by Row-Column Definitions), because in my opinion using fixed width and height at items property is mistake (of course sometimes you have to use item property), it’s like clothes, you want them to well fit on your body, you do not resize your body to clothes.

Notice: When you are designing your UI by the <Grid> make sure that you are include margin inside these definitions, I think it’s good practices to define spacing by this mechanism.

Nesting

You can define <Grid> inside of <ListBox> (i.e), and you can form <ListBox> view as you wish, it’s very helpful to create custom view of listbox.

wp_listbox_grid

As you can see I didn’t use here fixed height, width. Even margins are implemented inside Row-Column Definitions. Of course this grid we can use inside DataTemplate, so if we have dynamic render data.

Conclusion : When you are modeling UI remember to use grid, and try to not use margin,height,width properties. It’s much more maintainable.

MongoDB and Web API, power of NoSQL philosophy

Few days ago I had to create very simple database, and there was a need to expose API, so first thought was no RDBM! (because there wasn’t any relation). @mfranc has showed me this powerful diagram, where you can easily decide which database engine will be the best for your solution. In my case I had choose MongoDB, it’s document-oriented database, tree of MongoDB is quite similar to RDBM, engine->database->collections->documents (so each document represent record of our data).

Now let’s go to see some code, first of all you have to download MongoDB, and then install it. If you did it, now we can run our VS, and select new Web API project.

First of all you need to add Package of MongoDB C# Driver.mongodb_package

Now we can write some code, so first we need model of our data. Speaker.cs, this class represents model of speaker.

And here is example data inserted to this model, as you can see ILIst<Presentation> create us a list of presentation. Moreover this record is one document inside of speaker collection.

mongodb_speaker_tree

Presentation.cs model of presentation.

Example data based on this model,

mongodb_presentation_tree

You have to know that Presentation Collection isn’t member of Speaker IList<Presentation> data! This isn’t a relation!

After these steps, now we can create our api controllers, Controllers->Add->New Item->Web API Controller, PresentationController.cs

As you can see there is only implemented Get Method because in my case I need only this, MongoDB queries are somehow similar to ORM’s, just look at this method where you want to get only one specific Presentation, you have to use Query.EQ – In this case every record are tested, are they equal to specific value. Also I use .Single() to cut off collection to single record.

SpeakerController.cs, Here is a dynamic method to show you that you can form your api as you wish

Conclusion : NoSQL – not only sql, it is philosphy to not use only sql in your solutions, in this case using MySQL or MSSQL will overpower my project, moreover Mongodb has better time r/w than typical RDBM. I recommend hereby to use as simple as possible solution, if there is no need of using specific product. At least look at this side, how fast you create and expose this api for me it was a 15-20 minutes.

Comments: When you are creating MongoDB model, it should represent a specific view of our data, it’s very helpful and required. In this example there is no need to add relation at this two models, Speaker.cs and Presentation.cs.

Deploy from external repo

tl;tr; Deploy from external repo on MS Azure

Few days ago I was on a Global Windows Azure Bootcamp,We had few sessions, one of those session was lectured by @mpraglowski, he showed us this wonderful feature. What if we want to deploy our website on each git push without any extra action on our server ? With help come Azure Web Sites.

First, create new web site by choosing New -> Compute -> Web site-> Custom createcreatewebsite_0

Next step will be, fill out these boxes. Important is that to check box “Publish from source control”.
createwebsite_1

Third step is to choose which service we want to use (Bitbucket, TFS, Github etc.)

screatewebsite_2

In final step of creating web site, we choose repository that will be use.

createwebsite_3

If there is no error we should see deploy with a green header like in this picture. Azure has a nice feature, we can freely manage which git push we want on our website any time. Let’s imagine that you push commits but something don’t work exactly how you assumed. You can change to early version of deploy just clicking on it and clicking “redeploy” on bottom of website

createwebsite_4

Async and RESTSharp

tl;tr; Examples of using Async method of RESTSharp, and comments

As I mentioned last time I was forced to use async architecture because Windows Phone require it. I was newbie in async programming but when I met RESTSharp, it clear everything, I will show you some code block with my comment for it. Basics, and how to connect to Web API check this blogpost

If we want execute GET.

We have to remember that async method is runs in brand new thread! First we have to set REST request, type here your api path, and what method, for this case it will be GET. r.Data is deserialised List of Locations.

If we want execute POST

Here we fill body of our REST request, we add new object which will provide information that we want to POST.

If we want execute DELETE

Now we have to add parameter to URL so our request for example will be “http://example.com/api/location/1”, and method DELETE

If we want to update (PUT)

In this case we have to provide object which will replace old one, by using of AddObject method. And finally we can execute our request.

Most of you will ask me, how works this part. If we want to execute method ExecuteAsync<T>(). As a first argument we have to provide request. Second argument is CallBack – what we want to do when data will be received, so we can put here some MessageBox, event for logger or simply bind our given data.

In next episode we will be checking, is there any possibility to mix REST a XNA,

Introduction to RESTSharp

I would like to show you, good library to working with REST architecture. Nowadays REST is getting more popular because it’s very simple API, even ASP.NET MVC support this as default API service.

RESTSharp, is powerful library for any kind of .NET technology. Especially for Windows Phone where you have to use REST or SOAP to communicate with your external data. I came across RESTSharp when I was working on Project for Imagine Cup 2013. It saves me, a lot of time. RestSharp support asynchronously actions as well as windows phone because it is the main presumption of programming on that platform. I would like to show you few code block of using RESTSharp. At the begging basics and then in the next blog post some advance methods of using library with async.

First, we have to make a connection with server where API is located. Then we add physical path to specific API controller, and choose method of connection (GET, POST, DELETE, PUT etc.). Next step is create a query to our REST API services, our result will be list of Item, it’s highly recommended to use that type deserialization, now we can easily get access just adding .Data at the end of statement.

Now let’s look at Method.POST.

client and request stay same like in first example, but there is one change in method of connection, please set Method.POST. Next step is add body to our request create model that is expected in API controller, and finally last line, execute our request.

If we want run delete method, just use Method.DELETE

Declaring client stay same like in other cases, request has small change, first you have to add path with parameter and define method of connection Method.GET. Moreover, we have to add to request one more property, define parameter {id} and what values its get. Finally execute request.

ORM Series : Entity Framework Migration

Entity Framework, has given with last update, migrations at code first, undoubtedly it’s the best feature in this update. This tool is very helpful if we change something at our model, and then we can simply upgrade our database. Moreover it can fulfill role as a database versioning, because we can easily to manage our updates and freely manage between them.

Whole system is based on Package Manager Console. Basic commands

  • Enable-Migrations
  • Add-Migration {name of commit}
  • Update-Database

Enable-Migration we are using only once. It’s command which create basic files need to start work with migrations. Add-Migration is command we will use if we want to add some changes to database, and it generate file with . Update-Database – execute migrations files

First we have to create our model, I created simple music database:

 

 

 

Now we will run our Packer Manager Console with Enable-Migrations command.

If executed command was properly , we will see ‘Migrations’ folder.

image

Now we can execute next command, Add-Migration BaseOfDatabase. ‘BaseOfDatabase’ is name of commit so choose wisely because it should describe in one – two words what you have done here. Moreover there is a new generated file.

image1

That file include code that will be executed at final step, code sample one from few methods.

 

Now we can execute all of updates. By using Update-Database command. If everything goes properly, let’s change our model to show you, power of Migrations.

Album.cs

Song.cs

Let’s now commit our changes, execute Add-Migration command, result of execution

Update database, our database has given these two columns. Now we can freely downgrade or upgrade our version of database. 🙂 so if we decided after thousands of commit to delete these columns

Score : For this feature I will give EF two stars in category “Uniqueness”
summary_sec