Pages

Thursday, June 16, 2016

In this article I will explain how you can cache object in
memory in WPF application to get better performance.

Cached data will be stored in memory so you will get faster access
to that data compare to accessing it from File or Database. So whenever you need to make frequent
calls to some database or file, better you cache that data and read from cached object this will improve
your application performance via reducing multiple calls to database or file.

Caching mechanism provided by .Net framework 4.0. Caching is
available under System.Runtime.Caching namespace and you need to add this
namespace in your project references.

See below example, where multiple calls are being made to
database for getting employee details based on employee name.

In above example, for each employee search is costing
database call. To avoid database call for each employee, we can cache all
employee name at once and for each subsequent search we can get it from cached
object. This will reduce database call every time we search on employee and
increase performance. See below code which implements caching.

Output is same as above example, but in this
example In-Memory caching is being used. In constructor, I loaded all the employees
from database and added to the cache object. When every time user search for
employee, search will be perform on in memory object and result will be returned.
In this example cache expiry is set to 5 minutes but you can set as per your
need. After every 5-minute cache object will get expired
and employee list will be null. After cache expired, if you still need cached object then you need to load it again from
database or file and add to cache again.

In memory caching is very beneficial and recommended for applications which
are making multiple calls to database, file system or others. You can set cache expiry time if you want to delete or update or refresh cache object after certain time. Caching will
improve performance and scalability of your application.

Hope this article helps you to understand how to cache data objects in WPF application. Please leave your feedback in comments below.

Saturday, June 11, 2016

Prototype pattern is used to create an object from exiting
object via cloning. Prototype pattern falls under creational pattern of GOF
(Gang of Four) pattern.

When to use –

When you want to create an object by copying it from other object or prototype. So
prototype act as master or blueprint and other objects created from master object are copies of that object. In short, Prototype pattern is used to clone objects.
In real world example, a photocopy machine creates multiple copies from
original document and you can independently use or send photocopies wherever
you want to use original document.

Major components of prototype pattern are –

Prototype– It’s an interface for cloning.

Concrete Prototype – Implements prototype
interface and clone itself.

Client – Creates new object and ask to create
clone of created object.

Prototype pattern is easy to implement. You can create your
own interface which contains Clone method and use it as prototype. In below
example I used ICloneable interface as Prototype.

In above example, I used MemberwiseClone () method which is
available to all objects. This method creates shallow copy of an object. This
method copies the values of all the fields and doesn’t copy references in the
object points to. When object is simple and doesn’t contain any references of
other objects then this method works very well. In above example I changed
address of P2 object then it has also changed address of P1 object as well and this
is not expected when you clone an object. To solve this problem, we need to use deep copy functionality to copy an object. See below example with little change in Clone
method to implement deep copy.

As you can see in above example, I made changes in Clone
method and implemented deep copy functionality to copy reference object along
with other values of object. Now you can see there is no change in P1 object’s
address after changing P2 object’s address. It means implementing deep copy
in prototype pattern we get brand new P2 object from prototype which has no
relation with P1 object.

Difference between shallow copy and deep copy –

Shallow Copy

Deep Copy

Copies value types from source object to target object. It also
copies object’s reference type as reference but not the referenced object
itself.