Archive
Generating a WPF Smart Client using the Smart Client Producer
Hello!
In a previous posts, we showed you the output of the WPF Smart Client Producer. In this post we’ll show you how to generate a WPF smart client. It will be a WPF application consuming WCF services thanks to a proxy and the data will be stored in a database
.
Creating the solution
Start by creating a new CodeFluent Project. We’ll use the “ContactManager Sample Model” for the purpose of this article.![]()
Add a new Class Library project named “ContactManager” to your solution. It will be used to host the generated files corresponding to your Business Object model and WCF service’s contracts. Add it a folder called “ServicesConfig” that will be used to store your WCF service’s configuration files.
Add a new Class Library project named “ContactManager.Proxy” to your solution. It will be used to host the generated files corresponding to the proxy.
Add a new SQL Server Database project named “ContactManager.Persistence” to your solution. It will be used to host your generated SQL scripts.
Add a new WPF application project named “ContactManager.SmartClient” to your solution. It will be used to host the generated files of your WPF application. Remove the MainWindow.xaml file since we are going to generate our own. Also, by default, Microsoft Visual Studio 2010 sets the target framework to .NET Framework 4 Client Profile. Go to the property of your project and change the target to .NET Framework 4 in order to use the standard libraries.
Adding the producers
The Business Object Model Producer
To generate your Business Objects model, add the Business Object Model Producer to your CodeFluent Entities project by clicking “Add new producer” on the “Producers” folder in Visual Studio’s Solution Explorer, expand the Business Layer Producers node and select it:
In the Target Directory field, select your Class Library project called ContactManager.
Click OK.
The SQL Server Producer
To generate your persistence scripts, add the SQL Server Producer (or the one that suits your configuration) to your CodeFluent Entities project by clicking “Add new producer” on the “Producers” folder in Visual Studio’s Solution Explorer, expand the Persistence Layer Producers node and select it:
In the Connection String field enter your connection string used to connect to your SQL Server. Also in the Target Directory field, select your SQL Server Database project called ContactManager.Persistence. Finally in the Target Version field, select your SQL Server version.
The Service Object Model Producer
To generate your WCF service and your proxy, add the Service Object Model Producer to your CodeFluent Entities project by clicking “Add new SubProducer” on your “Business Object Model” producer in Visual Studio’s Solution Explorer, expand the Business Layer Producers node and select it:
In the Target Directory field, select your Class Library project called ContactManager.Proxy and click OK.
The Template Producer
Note: The Template producer is not mandatory. However, it is really handy if you want to be able to quickly run and test your WCF service using the CodeFluent Entities Hoster.
Lets now generate the configuration files of your WCF service. Add the Template Producer to your CodeFluent Entities project by clicking “Add new producer” on the “Producers” folder in Visual Studio’s Solution Explorer, expand the Utility Producers node and select it:
In the Source Directory field select the “Server” folder located at “CodeFluent Entities installation folder > Modeler >Templates > ServiceModel”. And in the Target Directory field, select the folder called ServicesConfig located in your Class Library project called ContactManager, then click OK.
The WPF Smart Client Producer
Then, to generate your WPF smart client, add the WPF Smart Client Producer to your CodeFluent Entities project by clicking “Add new producer” on the “Producers” folder in Visual Studio’s Solution Explorer, expand the Application Producers node and select it:
In the Target Directory field, select your WPF application project called ContactManager.SmartClient and click OK.
Generating code
Now to start generating code, build your CodeFluent Entities project called ContactManager.Model.
Here is the visual studio solution you should have by now, each project containing the generated code (your database should have been created too):
Building and running the application
At this stage, your application has been generated and is almost ready to be built and run. Since your WPF application uses the Proxy you need to add your Class Library project called ContactManager.Proxy as a reference to your WPF application project called ContactManager.SmartClient project.
Now you can build your solution, then run your WCF service as you usually do or thanks to the CodeFluent Entities Hoster using your generated service configuration files. Finally start your WPF application.
Tip: If you use the CodeFluent Entities Hoster, a quick and easy way to ensure the executable is automatically copied to the output directory is to add a reference to it. This is absolutely not required to compile your Business Object model (your Class Library called ContactManager), it’s just a shortcut to have the executable in the output directory without having to create post-build events.
And tadaaa! You’ve created your 100% functional WPF smart client without a single line of code!
Enjoy!
Thibault Nestor
CodeFluent Entities is not an ORM
CodeFluent Entities is a unique product integrated into Visual Studio 2008/2010/vNext which allows you to generate components such as scripts (e.g. T-SQL, PL/SQL), code (e.g. C#, VB), web services (e.g. WCF, ASMX) and UIs (e.g. ASP.NET, SharePoint, WPF).
The code generation process is model-first and continuous: from your declarative model, a meta-model will be inferred which code generators will then translate into code. Over 20 code generators (a.k.a. “producers”) are provided “out of the box” and can be combined to create your own application following your desired architecture, using your desired technologies.
For instance, adding an instance of the SQL Server Producer to your project will translate your model into a database (schemas, tables, stored procedures, views, constraints, etc.):
Adding an instance of the Business Object Model Producer will translate your model into .NET classes (C# or VB.NET) consuming the persistence layer:
Adding a WCF Producer will generate WCF services, contracts and proxies exposing your .NET classes:
Adding a WPF Smart Client Producer will generate a WPF application consuming the WCF services thanks to the generated proxy:
And that’s just an example of what you can do: you can also build ASP.NET web sites, SharePoint apps, Windows Forms apps, Silverlight apps, etc.
Your application is layered following best practices, all its business logic is in the middle-tier (the Business Object Model generated by the Business Object Model producer), and supporting a new technology gets down to adding a new producer or creating screens on the generated code.
Last but not least, you can create your own producer which translates this inferred meta-model into what you need.
Cheers,
Carl Anderson
The Smart Client Object Model: Silverlight Support
We introduced the Smart Client Object Model in a previous post and we mentioned the fact that it could be used in both standard .NET apps as well as Silverlight apps. This is true thanks to Silverlight’s CoreCLR.
Starting from Silverlight 2, Silverlight ships a scaled version of the original .NET CLR named the CoreCLR. It basically is a stripped down version of the standard CLR, only containing what is needed to develop rich .NET internet applications. In a nutshell, the CoreCLR contains:
- Most of the same Base Class Libraries as the ones provided in the standard CLR,
- JIT,
- Garbage Collector,
- Security Model,
- Exception Handling,
- Loader & Binder,
- Debugging APIs
Nevertheless, as you understood, not all .NET classes are available so to ensure that the generated Smart Client Object Model can be used by any Smart-Client including Silverlight ones you’ll need to reference the CodeFluent Silverlight Runtime (corresponding to your Silverlight version)which adds support for missing features required by the object model.
Furthermore, the runtime also adds a key feature which is the Silverlight Automatic Asynchronous Proxy (aka SLAAP). As you may know, the Silverlight programming model requires that all server calls cannot be issued from the UI thread and need to be asynchronous. As a consequence developers need to implement the IAsyncResult interface for all their server calls. Therefore, without using CodeFluent Entities, making a server call in Silverlight looks like something like this:
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
MySampleService.CustomerCollectionClient client = new MySampleService.CustomerCollectionClient();
client.LoadAllCompleted += new EventHandler(client_LoadAllCompleted);
client.LoadAllAsync();
}
void client_LoadAllCompleted(object sender, MySampleService.LoadAllCompletedEventArgs e)
{
listBox1.ItemsSource = e.Result;
}
Well using CodeFluent Entities and its generated Smart Client Object Model you don’t have to bother using the IAsyncResult interface anymore with those LoadAllAsync and LoadAllCompleted members, but simply call the LoadAll method as you would have done in a standard .NET application. However, since Silverlight requires that server calls should not be issued on the UI thread you need to run it on a background thread such as this:
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
ThreadPool.QueueUserWorkItem(GetCustomers);
}
private void GetCustomers(object state)
{
CustomerCollection customers = CustomerCollection.LoadAll();
Dispatcher.BeginInvoke(() => listBox1.ItemsSource = customers);
}
Better, but still this implies that developers have to do the QueueUserWorkItem and BeginInvoke themselves. To remedy this, the runtimes actually include two handy extensions RunAsync and UpdateUI which actually take care of all this. This way loading business data using the Smart Client Object Model in Silverlight gets down to do the following:
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
this.RunAsync(() =>
{
// 1. business code
CustomerCollection customers = CustomerCollection.LoadAll();
this.UpdateUI(() =>
{
// 2. UI update code
listBox1.ItemsSource = customers;
});
});
}
To sum-up, key points using the Smart Client Object Model in Silverlight are:
- It’s the same object model for all smart clients,
- this is possible mostly thanks to the runtimes which provide the missing features in the targeted CoreCLR (2, 3, 4),
- furthermore the runtimes provide the Silverlight Automatic Asynchronous Proxy feature so you don’t have to bother with IAsyncResult anymore.
The Smart Client Object Model
The Service Object Model Producer generates the communication layer between the server and a client side. It’s based on Windows Communication Foundation (WCF) and generates the needed services and contracts to expose your object model, as well as an enhanced proxy which allows us developers to create Smart-Clients without having to take care of all communication aspects.
This communication layer is absolutely standard WCF, so you can configure it as you wish using your desired addresses, bindings and configurations. Likewise, since the generated services and contracts are standard WCF services you don’t have to use the generated proxy, you could perfectly consume those web services using the Microsoft Visual Studio generated proxy.
However, the generated proxy is in fact a little more than a just a proxy. It’s in fact a remote version of the generated Business Object Model (BOM), and since it takes care of all communication aspects, it actually provides Smart-Client developers the same development experience as when developing a rich client: you’re manipulating Customers, Orders, Products, and not Channels, Endpoints and Factories, and that’s why we named it the Smart-Client Object Model (SCOM).
Using CodeFluent Entities, the Service Object Model Producer – with its generated services, contracts and Smart-Client Object Model – is the way to create Smart-Clients. Furthermore, the generated SCOM can also be used in Silverlight 2 and upper, so the same Smart-Client Object Model can be used for all your Smart-Clients whether they are standard .NET apps (Windows Forms, WPF, etc.) or Silverlight Apps.
Want to know more? Here’s a set of links that might be of interest to you:




