_______________________________________________________________________________________________________________________________________________________________________________________________

There are various factors that need to consider while building enterprise class applications in WPF. It’s very important to understand WPF architecture at the initial stage on how WPF application runs in the windows operating system.

WPF Programming Model

WPF programming happens in the managed code. The WPF becomes more robust, stabilized when running through the managed code as the CLR provides number of features that makes development productive (including memory management, CTS etc) which comes with a cost.

The WPF architecture is as illustrate below:

The position of WPF within the .NET Framework.

test.png

The red sections of the diagram are the code portion of the WPF which includes Presentation Framework, Presentation Core, and milcore. These are the code portion of the WPF. There is one unmanaged component milcore.  This is directly coupled with DirectX Engine; all display happening through WPF is through DirectX engine which make effective hardware and software rendering and make the application looks Jazzy.

Initial Architecture

 

The architecture consideration for the WPF is extremely important in building WPF/Silverlight applications, this determines the performance, it’s possible to build applications which are highly robust than web applications, consumes less bandwidth compared to ASP.NET,  low memory intensive and can take advantage of hardware acceleration to deliver high quality graphics through the web. The XBAP application running in full trust can run outside the browser sandbox and can take the advantage of hardware which is impossible in ASP.NET or any other web technologies offers without plug-ins which need to be separately installed on the client.

Since .NET framework is shipped with windows operating systems with windows server 2003 hosting .NET framework 1.1, Vista hosting .NET 3.0, Windows 7 with .NET 3.5, Windows 8 and server 2008 hosting .NET 4.0, it is possible that all applications demand .NET framework as it remain only model for building next generation applications in Windows.

Windows 8 supporting Windows 8 apps, the application becomes very powerful and capable of providing high end graphics, high quality UI, which can be run on variety of platform like Windows, web, touch pads, laptops, mobiles , all with same or little code change when building applications on WPF, which becomes ideal platform for all developers.

Considering the initial architecture, WPF provides various programming model like MVVM (Model View View Model) being the most famous one, MEF (Manage extensibility framework), PRISM which embeds MVVM design, MVP etc.

The MVVM is coined based on various other design patterns of its predecessor like MVP (Model view presenter), MVC (Model View Controller). The MVC is a design pattern which is coined on Ruby on Rails development which became extremely popular and has been adapted extensively in ASP.NET applications which provides new platform for the development.

Note: The main point what I want to highlight here is these design pattern, though several website indicates these are the design patterns which provides enormous benefits while building enterprise class application, most of the designers or developers intend to follow the pattern as most WPF applications falling under MVVM or PRISM and few built on MVC, MVP or MEF frameworks.

The detail descriptions of these architecture are beyond the scope of this article, I am trying to say based on my experience in developing more than 20 Application across 6 companies with around 15+ clients, these architectures makes extremely cumbersome for the development and leads to various flaw in the development and can lead to various problems which expects only expert developers to work for implementation, implementing unwanted commands, makes cumbersome to pass events argument to the function, decreasing performance, increasing memory consumption and also increase in network bandwidth.

However, I am not pointing against this, but will be required only in certain kind of development and should not be usual choice for developers and designers.

These add lots of additional complexity in development more than resolving issues and also make application unusable in the production.

The MVVM has the following architecture:

The User general Choice for developers will be using is PRISM design patterns which has MVVM in-built for application development. This architecture has View Layer which is usually a presentation layer or UI, The PRISM or MVVM design pattern indicates there will be no .cs file for XAML.

This can become more cumbersome for the developers as there will be no .xaml.cs file and all the events are to be pushed to View Model as methods, and also for the events which requires complex parameters it becomes extremely difficult for the developers especially who are switching from ASP.NET and doesn’t bring any additional benefits, this can be helpful when UI developers develop in Expression Blend and use in .NET by developers. This however adds additional complexity and majority of the project involves developers to be involved in UI development and can become extremely cumbersome for those.

It’s possible to attach ICommand objects than delegate commands and can be done without PRISM or MVVM.

The MVVM architecture expects all calls from model to service in Async patterns, this may look beneficial at the outer look, but this raises lot of overhead and complexity in application development, Since the service calls are async it’s possible to trigger multiple service calls at one shot, but results are to be captured in Completed event. (This supports only CallBack functionality on remote service with normal configurations).

For example, consider you were filling employee history in the form from database which has multiple service calls, till all the service call happens the UI should be blocked, in this case it very difficult to manage in async programming in CallBack fashion.

The Calculator service demonstrated below represents the scenario:

public void Add()

{

                MathClient.AddAsync();

}

Public void Sub()

{

                MathClient.SubAsync();

}

public void Add_Completed(object sender, EventArgs e)

{

//Process Result

}

 Public void Sub_Completed(object sender, EventArgs e)

{

//Process Result

}

In the above code which returns the result first and which returns result last, the developers will not be able to predict, it will be hard to block the UI thread till all results are returned. Hard to synchronize all items. It will be very difficult to dispose service. The services have the inner Channel which is unmanaged and need to be dispose or else memory consumption will be high. There are many instance we see where Silverlight or WPF application hangs the UI, which makes the application very unfeasible at the production.

The service call has to be made in synchronous fashion to avoid all these issues.

Since almost all the code have delegates and events, it consumes more memory, long processing time, and since everything is asynchronous here it can lead to instability in the code, which has to be addressed.

There are chances that same service is called many times, causing unnecessary roundtrip to the server, may also impact on the bandwidth, where you see only data-transfer consumes more memory than web applications, the performance will also have an impact, causing long time to update UI.

KSmart pattern for WPF

 

Here by I present my custom design pattern which solves most of the problems incurred in WPF development, by making application development easier, keeping low memory, increased stability, increased performance, lesser network bandwidth.

The detail of the KSmart pattern will be presented separately in-detail in another code project article, which contains comparisons of various design patterns with KSmart pattern.

 

 

 

 

 

  • 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.

 

This design pattern provides greater flexibility in implementing effective code for WPF/E or Silverlight.

 

Model Layer implementation

 

Even though the model layer provides service interface to WCF client, this will be the key component in improving performance, providing better memory management, reducing network bandwidth.

Let see how model layer improves overall performance of the application. The model layer make calls to WCF layer, it’s possible than model layer can make async calls for set of operations in WCF, whose results are required parallel, the model layer can wait till all of it returns result thus enhancing performance of the application and still returns result in synchronous style to the business layer.

Its responsible for better memory management, the memory has to release as it make use of unmanaged channel for communication, once result is returned, the service should be disposed.

    public class CustomerModel

    {

        public int Add()

        {

            using (CalcService service = new CalcService())

            {

                return service.Add();

            }

        }

        public int Sub()

        {

            using (CalcService service = new CalcService())

            {

                return service.Sub();

            }

        }

    }

In the above code the service is created, the call to service is made, and service is disposed. This kind of code provides large memory reductions, make WPF/Silverlight applications healthier and perform better, which is must in WCF.

The CalcService is wrapper, which interns takes care of opening channel and returning service.

This is very critical to application performance, better memory management and reduces network bandwidth. As compared to:

public void Add()

{

                MathClient.AddAsync();

}

Public void Sub()

{

                MathClient.SubAsync();

}

public void Add_Completed(object sender, EventArgs e)

{

//Process Result

}

 Public void Sub_Completed(object sender, EventArgs e)

{

//Process Result

}

Here, it very hard to dispose objects, hard to maintain synchronization among objects consumes more memory, bandwidth and hinders performance as everything is based on delegates and events.

 

Conclusion

The Author has close to 7 years of experience in working with Software industry. Has started his career as Trainee in Siemens, Project Engineer in wipro technologies, Consultant in Capgemini at Philips, Software Engineer in Trigent Software, senior software engineer in Aditi Technologies and Senior system analyst at Sonata Software from 2010. Has been hooked onto .NET throughout his career and has almost worked on all Microsoft technologies with specialities in .NET, C#, Sharepoint, ASP.NET, Winforms, WPF, Dynamic CRM to name a few.