Architecting High Performance Enterprise Class Application in WPF 4.5
· Overview.
· Introduction.
· WPF/E or Silverlight
· Factors need to considered for enterprise class application with WPF/Silverlight
1. WPF Programming Model.
2. Initial Architecture.
3. KSmart pattern for WPF.
4. Model Layer implementation.
· Advance Memory Management.
1. Caching objects.
2. Weak References.
· Garbage Collection.
1. Unmanaged Objects.
· Asynchronous Programming.
1. Dispatcher.
a) Using the Dispatcher.
b) Updating the UI.
c) Updating the UI Asynchronously.
2. Background Worker.
a) Using Background Worker in WPF.
· Binding.
1. Direction of Data Flow.
2. What Triggers Source Updates.
· Page and Navigation.
1. Refreshing the current Page.
2. Navigation Lifetime.
· Programming Guidelines for Improving Performance.
· Conclusion.
· The Ksmart pattern has view, which generally contains presentation logic for the UI.
· The controller can be .xaml.cs or can be viewmodel or combination of both, this contains code for binding data, handling events etc.
· All processing logic will be done by business layer and layer can be shared across different controllers.
· Model contains code for making service calls, disposing service after its use.
· The service contains WCF Services for model calls.
· Business layer at the service handles all the business logic at server side.
· The DAL will make call to the Database and return result to business Layer.
· The service interface and data contract project will be share across all layers and will have flexibility to type cast to service, or service disposal in another layer and also data is share across through data contracts in the application.
· Strong reference objects, by default all objects are of strong reference, the Garbage Collector requires the objects to be explicitly marked for collection.
ApplicationCache cache = new ApplicationCache();
object item = cache.GetItem("itemKey");
cache = null;
GC.Collect();
· WeakReference objects, which are automatically picked up by the garbage collector. These objects need not to be marked explicitly and these are short lived objects.
class Program
{
static void Main(string[] args)
WeakReference refe = new WeakReference(new Test());
//test.Display("123");
Console.WriteLine(refe.IsAlive);
Console.Read();
}
· Dispatcher.
· Thread Affinity.
· Navigating . Occurs when a new navigation is requested. Can be used to cancel the navigation.
· NavigationProgress . Occurs periodically during a download to provide navigation progress information.
· Navigated . Occurs when the page has been located and downloaded.
· NavigationStopped . Occurs when the navigation is stopped (by calling StopLoading), or when a new navigation is requested while a current navigation is in progress.
· NavigationFailed . Occurs when an error is raised while navigating to the requested content.
· LoadCompleted . Occurs when content that was navigated to is loaded and parsed, and has begun rendering.
· FragmentNavigation . Occurs when navigation to a content fragment begins, which happens:
· Application.Navigating
· Application.NavigationProgress
· Application.Navigated
· Application.NavigationFailed
· Application.NavigationStopped
· Application.LoadCompleted
· Application.FragmentNavigation
· Use asynchronous programming where possible to improve performance, this can lead to performance degradations if not properly used.
· Write code smartly, use objects where it is necessary, and declare only in the scope.
· Avoid mistakes like:
bool test = false;
//Some Processing
if (test == true)
//some processing
We can write instead
if (test)
· Use centralized cache so that disposal of objects are easy similar to WeakReference.
· Dispose static objects and event when not required by assigning to null.
· Use linq or lambda expressions carefully avoid programming mistake which is similar to SQL joins and can take long time if not properly used.
· Try to do without recursion and implement while loop to exhibit same behaviour.
· Avoid using delegate and event, use Action command from .NET 4.0 and higher versions and release those command immediately after use.
· Build application with proper design from the start, which helps to follow later and can provide large benefits.