Showing posts with label Silverlight. Show all posts
Showing posts with label Silverlight. Show all posts

Thursday, March 31, 2011

WCF RIA Services Compositions with Entity Framework

I haven't been working on anything outside Genexus, Deklarit and Genexus Server for a while, so when my friend Mateo asked to help him on a new project for a client of his, based on brand new Microsoft technologies, I was saying yes before the end of the sentence.

This blog post and probably some more to come will be related to our experience with Entity Framework 4, RIA Services 1 and Silverlight 4.

But in this particular case I wanted to blog about a problem we had with Compositions. Compositions are very useful when you have an Association where a Parent entity needs to have its children all the time.

The Update method of parent entities is a bit different than regular entities. I took the patter from this article (Compositional Hierarchies) but things didn’t work as expected. I have to add that “m generating POCO entities and using the Unit Of Work pattern so it wasn’t easy just copy and paste the code for the article. I posted a few questions to the RIA services forum and found out that that pattern exposed a bug :(

Fortunately I was redirected this post from Brett Samblament which described the new pattern to follow to write a fully functional UpdateParent method. But again, I can’t just copy and paste, so here’s the code I wrote for it. Also, by using generic, I’m able to call the exact pattern for every composition in my model, cool uh?!

public void UpdateParent(Parent parent)
{
EntityHelper.UpdateParentEntity(parent, ObjectContext.Parents, ChangeSet, ObjectContext);

foreach (Child child in ChangeSet.GetAssociatedChanges(parent, o => o.Children))
EntityHelper.UpdateChildEntity(child, ObjectContext.Children, ChangeSet, ObjectContext);

}


And my EntityHelper class has the following methods:



public static void UpdateParentEntity<T>(T entity, IRepository<T> repository, ChangeSet chgSet, IUnitOfWork oc)
where T : class
{
try
{
ObjectContext ctx = oc as ObjectContext;
repository.ObjectSet.AddObject(entity);

T originalEntity = chgSet.GetOriginal<T>(entity);

if (originalEntity == null)
ctx.ObjectStateManager.ChangeObjectState(entity, EntityState.Unchanged);
else
repository.ObjectSet.AttachAsModified(entity, originalEntity);
}
catch (Exception ex)
{
TraceManager.Error(string.Format("An error occurred updating a {0}", typeof(T)), ex);
throw;
}
}


public static void UpdateChildEntity<T>(T entity, IRepository<T> repository, ChangeSet chgSet, IUnitOfWork oc)
where T : class
{
try
{
ObjectContext ctx = oc as ObjectContext;
ChangeOperation change = chgSet.GetChangeOperation(entity);

switch (change)
{
case ChangeOperation.Delete:
if (GetEntityState(entity, ctx) == EntityState.Detached)
repository.ObjectSet.Attach(entity);
ctx.DeleteObject(entity);
break;
case ChangeOperation.Insert:
// do nothing
break;
case ChangeOperation.None:
ctx.ObjectStateManager.ChangeObjectState(entity, EntityState.Unchanged);
break;
case ChangeOperation.Update:
T original = chgSet.GetOriginal<T>(entity);
if (original == null) { throw new Exception("Update with no original value found"); }
if (GetEntityState(entity, ctx) == EntityState.Detached)
repository.ObjectSet.Attach(entity);
repository.ObjectSet.AttachAsModified(entity, original);
break;
default:
break;
}
}
catch (Exception ex)
{
TraceManager.Error(string.Format("An error occurred updating a {0}", typeof(T)), ex);
throw;
}
}


public static EntityState GetEntityState(object entity, ObjectContext ctx)
{
System.Data.Objects.ObjectStateEntry ose;
if (ctx.ObjectStateManager.TryGetObjectStateEntry(entity, out ose))
return ose.State;
else
return
EntityState.Detached;
}


I hope this helps someone clear the way… and I promise I’ll post more (and more often) about this.



As usual, this code is ‘works on my machine’ certified.



works on my machine



I never get tired of this Smile

Read Full Post

Friday, August 06, 2010

Annoy Smarx from Windows Phone 7

Annoy Smarx is a web app that Steve Marx showed in Windows Azure Firestarter event. It is a web app that once you clicked on an image, Steve’s notebook wallpaper would change to the selected image. It was a pretty cool demo cause it showed you a good practical sample of AppFabric ServiceBus.

Last week I started playing around with the new Windows Phone 7 SDK, and I faced the problem I do when I start something new, “what should I code”? I t has to be simple cause I’m new to this stuff (I’m no too familiar with silverlight either) but not too simple… I hate the “Hello World!” sample. So I thought maybe I can create a client for the AnnoySmarx sample and so I did.

I liked it cause it a simple project, yet it has many cool features, like downloading blobs from Azure Storage, using a ListBox to show the images, handling the double tap event on Windows Phone 7 (there’s no OnDoubleTap event, so it might be a little more complicated than you think), and calling a rest service on ServiceBus which is actually running on my computer.

You’ll find all the resources you need at Steve’s post and the code for this client here.

I recorded a video of the app running on the emulator…

pretty cool uh?!


works-on-my-machine-starburst_3_thumb[1]

Read Full Post

Sunday, August 03, 2008

Next stop: Florida!

dklogo

I didn’t want to say anything but a few days ago Milano mentioned it in a post in our forum so I guess that gave me the green flag :)

We are now working on the next version of DeKlarit code-named “florida”. We want to add a lot of new stuff but I think one of the most important features will be the Silverlight addin which will let you create web applications with Silverlight controls. We are still in the process of making a few decisions like, will it be a full Silverlight app or will it have aspx pages as containers for the Silverlight controls? We believe the second is the best choice right now since we have two excellent web addins and we can use the navigation logic we already have in those. That means it will be pretty easy for you to migrate your current WebGenerator generated app to a brand new Silverlight app. Cool uh?

Also, we’re thinking on REST support for our WCF addin, WPF for a fully funtional WinGenerator addin independent of third party controls, something you guys’ve been asking for, and full text search in generated applications among others. Also, I have to work on our security features for the generated apps and add a few features like allowing a user to change his password, sending conformation emails and more.

So if you have any suggestions, now is the time. I can’t assure you we’ll implement them all but we’ll surely keep’em under the radar.

 

Read the full thread of the “announcement” here.

Read Full Post

Tuesday, May 27, 2008

Microsoft Live Mesh: WOW!

mesh I don't know when was the last time a Microsoft product made such an impression on me. Microsoft Live Mesh surely did it.

About a week ago Ignacio send me an invitation to take a look at Mesh sharing one of his folder with me. I started up installing the small (1.57 MB) client app in my notebook computer. After doing that a shortcut appeared in my desktop to the folder Ignacio shared with me. I double clicked it and a light winform asked me where I wanted that folder.

Now that folder is synchronized all the time and we can add, remove or modify files and the client will sync those files up. Even when we modified the same word doc at the same time, the client told us there was a conflict and let us view both version of the file and pick the one we wanted to keep as final version (no merging yet).

Another cool feature is that I don't need the client to access the files. I can go online to mesh.com and go to my Live Desktop where I can see al my folders and files, work with them and upload them back up to keep them synchronized with all my computers.

Also, you can connect via Remote Desktop to any of your devices and work as if you were sitting in front of them. Better yet than regular Remote Desktop, since, when you're working somebody can be watching what you're doing. Let's say my wife's at home having some kind of problem and calls me for help (she does that). I can connect to my home computer (DSL connection) and show her how to do what she needs to do.

And one more feature I haven't been able to test is the ability to extend your desktop to one of your devices. The client installs a video controller which I assume will be used for that kind of stuff.

Live Mesh is a tech preview (not even in beta yet) but if you ask me, It's already a great product!

Note: I always mentioned devices because now you can only add computers but mobile devices and mac computers will also be available for synchronization.

Read Full Post

Saturday, March 15, 2008

Popfly Blocks

I've been playing around with Popfly for a while now, so I thought I'll post something about it.

Popfly is one of Microsoft 'newest' online toys that allows you to create mashups and blocks. Mashups are pieces of code which using Silverlight will display something nice to show in your web site, Facebook profile, Live Space, etc.

But, to show what?!... well pretty much everything that's available on the internet. To create a mashup you don't need any programming skills cause it's pretty much dragging, dropping and linking blocks. To build blocks you must have at least some programming knowledge cause you'll need to write some javascript and a config file with xml.

You can build your block from the online editor (not too nice) or you can download the VS2008 Popfly Explorer and build your blocks from VS2008, where you can debug your code.

I created a few mashups and two blocks. One of them is called Picasa Web Block and takes two parameters, username and albumname. After filling those up you'll get a collection with all the pictures from that album and you can display those pics with any of the display blocks available.
The other block was a little trickier, it takes a US zip code or a location code and the temperature unit (c or f) and it'll return the "current" weather conditions from the given location. The weather forecast is provided by Yahoo! Weather which takes the data from Weather.com.

In my spanish blog (scroll down all the way) I have a mashup I created with my Yahoo Weather block and the Live Earth block from Microsoft. Here's a picture of what it looks like.

weather

Mahalo to my visitor from Hawaii :)

Read Full Post

Thursday, August 23, 2007

Microsoft Tafiti

Google has proved that a good search engine has nothing to do with looks(1). They came out of nowhere with the simplest UI ever, no adds, no categories, nothing (at least at first sight) and rule the Internet. Suddenly being at the top of the Google result page was what everyone wanted and page rank became an obsession for some people.

But now, there's a new search engine called tafiti (Swahili for "do research") that has a very appealing UI (done with Silverlight). Obviously Microsoft or any other company couldn't compete with Google's simple yet efective design so they went a little further and tried a new thing.
At first look Tafiti is a cool web search site. It also has a simple way to show the results, so far with no adds, but it also has a more complex way (see it yourself).
Another cool feature is the possibility of saving and tagging results right away. Yes, I know Google does that, but Google saves every little search you did (some of them you probably wish it didn't). Tafiti lets you drag your result to the right to save them and tag them. It also shows you results from feeds, news, images and books.


One more thing, if you tafiti my name I'm at the top of the results so it looks the index engine is working just fine ;)


(1): Goomez has proved that too, but that's for another post :)

Read Full Post