Big Boss gives you a call, as usual at the last moment. He want you to write a code, a code that is used to encrypt or decrypt passwords. You wrote an algorithm instantly and created a class for that. The class Encryption that contains two functions, Encrypt and Decrypt, do their respective jobs when given.
So far so good, but there is a problem. The problem lies not in the code, but however in the structure of code itself. As you know, Big boss is not so kind enough to let you write bad code for the project you are given. Proper optimization of resources is what he demands. Let say your task is to send the encrypted data from server to client, and decrypt that onto server. And when you are to send data from client to server, you need to encrypt it in client, and decrypt it in server. Same class, same functions and same sets of code are to be used on both server and client.
Well you have two options to do that:
1. Create a redundant class on to client, which is nothing but just a line by line replica of the class encryption which is at the server.
2. Quit and leave the house.
The second option however, is not what you should go for. The first one is the one, that when you choose it, you will be redirected to second option by default. Puzzled! well WCF RIA save the day. What you can do is to share the resources between server and client. That means, you can make that class to be shared between client and server, that is, you can use the class in client project also just as you do it in the server.
Let us see how can we do that. I have a silverlight project opened already, you might need to open a new silverlight project with WCF RIA enabled.
I have a folder classes where I’d created a class MyShared.cs. Please look closely to the nomenclature for the class to be shared.
I’d used MyShared.shared.cs. Using .shared extension after the class name, specifies the compiler that the following class is to be shared between the two, server and client projects, compiler thus then generate the generated code for the class when the project is build. Just like it did with my MyShared.shared.cs class.
In this way, you can use the code written inside the class on both server and client. You can share as much classes you want. Same as what we’ve discussed can be done with the Encryption class, so that the functions, encrypt and decrypt can be used on both server and client.
This is a good code optimization technique, which big boss should admire.
0 comments