12/1/09

getting started with Silverlight consuming ADO.NET Data Service


Start new Silverlight project (I am using Silverlight 3 here)


Give it a name, click OK. 
Then “New Silverlight Application” host comes up. 

Click OK. This will create SilverlightApplication and SilverlightApplication.Web projects in your solution.

Add Add ADO.NET Entity Data Model to your SilverlightApplication.Web project



Entry Data Model Wizard will show, select Generate from Database, choose or create your database connection, your database objects. 



Note your entities name, you'll use it later in when adding it to the WebDataService as your data service type (I named mine MyDbEntities)
 

Add WebDataService to your SilverlightApplication.Web



In WebDataService.svc.cs add your Entity

public class WebDataService : DataService< /* TODO: put your data source class name here */ > 
public class  WebDataService :  DataService<MYDBEntities >
also in the same file there config rights, for now to get things going I’d put “*” in both…

config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
Build the SilverlightApplication.Web (so that you can reference the service in your SilverlightApplication project)

Add a Service Reference to SilverlightApplication project (click Discover if need to)




Finally, done with the setup. Lets add little code:

In SilverlightApplication project, the MainPage.xaml the page already has a Grid x:Name=”LayoutRoot”
Add a grid to it:

<data:DataGrid x:Name="dataGrid"/>

In MainPAge.xaml.cs:
Create a private field "entities", instanciate it, query, then set the dataGrid.ItemSource to what you got back…

Notes about the code below:

1. entities = new MYDBEntities(new Uri("http://localhost:3431/WebDataService.svc", UriKind.RelativeOrAbsolute));
if not sure about the Uri, then right-click on WebDataService.svc > View in Browser, to copy the link …also the port number is just a random port being assigned. If you add your service to IIS, then you can use relative url.

2. if IntelliSence doesn’t come up, or you're not sure what the name of your entities are (mine is MYDBEntities), open Model.Designer.cs and look for a class that derives from ObjectContext.

3. var query = entities.users; //users is just a table in my db… user yours
Here's the code...


public partial class MainPage
    {
        private MYDBEntities entities;
        public MainPage()
        {   

           InitializeComponent();
           entities = new MYDBEntities(

                        new Uri("http://localhost:3431/WebDataService.svc", 
                        UriKind.RelativeOrAbsolute));
           var query = entities.users;
           try
           {
              query.BeginExecute(c => 

              { 
                dataGrid.ItemsSource = query.EndExecute(c).ToList();
              }, query);
           }
           catch (Exception exception)

           {
              MessageBox.Show(exception.ToString());
           }
        }
    }



that's it, run it, and you should see some data in the grid.


8/28/09

Sustainable Mopping

got to work on Friday morning, this is what I get in my I.M.

(BTW - my dad doesn't speak English)


[10:08:32 AM] : your dad is washing our plywood floors with toilet water!!!!!!

[10:08:37 AM] : he put the mop in the toilet

[10:08:51 AM] : and then used it to mop the plywood floors

[10:11:04 AM] : i told him NO!!!

[10:11:37 AM] : asked him how it's clean

[10:11:45 AM] : then he lied and said it was the 1st time he did it

[10:12:01 AM] : and i was suspicious

[10:12:21 AM] : why would he be taking the mop into the bathroom and flushing the toilet

[10:12:24 AM] : then i saw him do it

[10:12:45 AM] : that was the 2nd time

[10:12:51 AM] : and i yelled at him 'no'

[10:13:05 AM] : he's now washing our plywood floors with water and vinegar

[10:13:07 AM] : same mop

[10:13:11 AM] : sorry to bother you about that

[10:13:21 AM] : i just cant believe it



8/21/09

Nothing to be ashamed off...

Yesterday going back home on the train I had time to reflect for a couple of minutes until someone stepped on my foot.

This summer presented me with this amazing opportunity to work on myself. Something in me grasps it. Something embraces it. And, of course, there is another part. The old story begins - that part does not want to go through the turmoil. But not that it doesn’t want it a 100%. It’s just that this part is tired and needs to be re-energized a bit. Once it gets a little breather, it’s becomes less disinterested.

But there is no breather. And this is what is great about this summer. It shattering a naïve outlook I had about myself. About my abilities to work. About my accomplishments in these years efforts. Accomplishments? What’s that? Everything still happens with me. Efforts? What efforts? One effort that I would attempt against one little push of a button?

I learnt to play checkers and make a good move once in a while. What happens when the game changes to chess? Where are the previous simple efforts - the ones that can help me? Can they? If they can’t, then I was going in a completely wrong direction and have to start from the very beginning. I would like to think that I was working in the right direction. But how little! And, again, how naïvely!

I was thinking about all this last night, while my father was trying pair after pair of jeans in the “XXX store”. Right in there by the shelves where jeans were displayed. Replying to my suggestion to use the fitting room with “Nothing to be ashamed off, I am wearing my brand new trousers”.

motivation

Been working on this bug for two and a half days. Feeling pretty dumb and down. Got an email from one of the guys who hired me for my first dev position. He writes:

-I’ll let you in on a secret.. It wasn’t about if you got the answers right or wrong….. anyone can learn that shit. It’s about showing passion, desire and willingness to try. That’s what makes a good developer. Don’t ever lose those qualities.

I gotta remember this more often. Why am I in it? I just wanted to code. That's all. ...going back to that bug.

1/22/09

XAML - Binding to complex types

Lets say you you have a User object with properties that return simple built-in types:
string Name, bool IsActive ...etc.

If you bind the controls parent to User, then with simple types you can do something like this:

<TextBlock Text="{Binding Name}"/>

well, if you add a complex type to the User, such as Contact of type ContactInfo that has its own set of properties - string Address, string Phone,  string Email,  ...etc. 

How do you bind then?

<TextBlock  Text="{Binding Contact.Email}"/>
won't work ( overriding ToString() on ContactInfo... or writing a Converter did not work for me)

What did work was:

settting the DataContext to the complex type property, then setting a binding path on XAML Control's Text, Content (...depending on the control that you are using) to the "inner" Property.

<TextBox DataContext="{Binding Contact}", Text="{Binding Path=Email}"/>