Friday, November 18, 2011

Handle Data in .NET 4.0 and .NET 2.0


Recently i involved in .NET 2.0 project and experienced few technical issues cause of less default support available . Today im going to explain how i overcome those .

Well .NET 4.0 came with lot of user friendly lovely features ( though we have to think 100 ways before use them ) . However i like the way they handle data structures . Especially LINQ queries with lambda expressions . Though there are many  arguments for using LINQ i would personally think , " Eventually LINQ is quite good(may be most ideal ) for small scale projects .


When we have a data set from sq l server we can manipulate it using data structures with in our server side code . Generic lists are favorites . But someone still can use dictionary or hash table up to their requirements . When you want to store ur objects belongs to some key for ease of search and identification , hashtables are good . So now everything is typed ( strongly typed) we have our general person ( employee/ contact / manager ) and product classes ( objects would represent them ) . So we fill them out and store in a generic list . Everything is mapped pretty sweetly and we call it ORM .

Well above is small and common scope in day today programming .

After we fill the list of objects , next task is to utilize the data . LINQ comes to scene and sort our all problems in quick steps . ( LINQ queries are pretty cool and easy to learn / Simply revert our sql query order ) .Say we have 1main list ,

1)managershavecontacts list

You can query it and get whatever data with mentioning the relationship .


Now our lives are cool because of those features introduced with .NET 3.5 / 4.0 .

But you still can involve in 2.0 project . How would you do without linq ?


Basics are same . You create a generic list .as above main list .

  1. First sort the list with some predicate condition . We have default sort() function . Include some custom predicate (if you compare objects ) to notify compiler about your sorting method .You can override Compare To() method and write one .
  2. If you wanna Group the list you need to remove the duplicates .
  3. Write a simple logic ,
  4. create another list of same type and fill that list from your previous list by checking for exist value .
  5. You probably needs a contains() method .
  6. To handle some complex scenarios you may need dictionaries .
  7. Store your object with keeping object's ID field as a key
  8. Then filter and search for whatever your known items in dictionary

    Please note when search for item , list.Contains() method is slow but easy to use .After finalize the programming logic and complete the code we can think about optimizing .  In later article ill come up with tricks .

So above are some information for handle the scenarios with or without LINQ .
Anyway some of my blog readers ask me to write some server side programming articles and here is a beginning .
You guys should keep this in your mind . Server side code is easy since we all are good in logic's and till good debuggers exist in programming world . But client side code is different we should have some rare skills on sort those without debuggers ( there are like firebug though ) . And client side code syntax also kinda difficult and hard to read  . Most of developers dont use concepts in writing client scripts . Therefore work in client is lot more work and harder in programming world and i always encourage everyone to learn it .









Tuesday, October 25, 2011

Why MVC3 RAZOR ?

When I am coding, always having confusion between performance and clean code . Those two are the key success factors of the quality program. But sometimes it is hard to maintain clean organized architecture while achieving a performance.

By the way , we have to think out of the frame and avoid using building block type .NET applications development with controls , if we run towards high quality end product which matches current web standards.

Well, one of the main advantages I experience using php (interpreted) over c# (compiled) is language flexibility. Earlier when I use php I am confident enough to say, “This is possible “. If I use .NET for web applications , I used to think  several times before say that . Reason is inflexibility.
In earlier days (.NET 2.0 ) we had web forms (aspx) . Microsoft has introduced set of compatible tools / controls / plugins to integrate with them .We drag/drop and develop. GOD knows what happens inside the controls . When control gives some runtime error , developers spend hours to fix it . Sometimes it is just asking for official support . Now everything has become new . Luckily even after many years microsoft introduced MVC , integrated with jquery . 

And Razor is one of the view engine introduced later .
Earlier when u want to display a dataset .you use a gridview . Now it is fairly simple and flexible with just plain html tables .No one can hide anything from you . Everything is visible. Here is the simple sample notation.
@foreach (var dataitem in dataset){
<tr>
<td>@dataitem.property1 </td>
<td>>@dataitem.property2 </td>
</tr>
}
We can use @ symbol infront of serverside code block in html page and then compiler identifies it .
Obviously razor views are slower since it iterate the code from server side . Except that , we can also easily pass data to client and iterate through javascript  .

$.ajax ({
 url:"Employee",
type :"get",
data:{id:123},
contenttype:"application/json"

}).done(function(data){

console.log(data.Name); // May be you can write code to display in div here

}) ;

 Razor does not have viewstate and postback . Therefore it come up with ViewData dictionary and ViewBag ( Dynamic variables), to share information between controller and view.

Eg -  ViewBag.Title ="Good Article ";  // In Controller
 Can display in view using @ViewBag.Title

Simply which is Performance vs cleancode  as I mentioned in startup of the article .
So in huge applications where employees working as teams , we have to  manage the code and use patterns to organize  the code . So strongly typed razor views will helpfull to maintain proper architectural code  .Then we can use more hardware resources to achieve performance . Disk space + RAM etc .

That is a small  information about RAZOR .

Saturday, June 18, 2011

Achieving best performance in complete web transaction


Web development is quite fun, but it starts behave stupid when we mess the things. Understanding and separating layers, is a strategy. There are many ways to think developers in tricky ways to do it.
From fundamentals we should know, client / server architecture. That is basically caring what are the things should happen in client and what should happen in server.
Usually I try my best to handle many things in client. If I really need to do something in server I cache it. Those are the building blocks.
I have seen many developers, render client component in server and pass to views. It is a mess .additional overhead to the whole program.
Then someone can ask me, Okay, then how could we populate dropdown from database table?
Here is the answer,
First let’s start by identifying process flow.

CLIENT --> REQUEST --> SERVER --> SCRIPT --> SQL SERVER --> SCRIPT --> SERVER --> RESPONSE --> CLIENT

If we want to achieve best performance definitely we should thoroughly look at the each and every part of this flow.
All DB transactions should happen in server and we should cache the data using sql cache or 3rd party cache layers such memchached or velocity .Then pass the data in user friendly format .Json is ma favorite. Believe me if u have proper caching system and properly indexed dB , that process is super-efficient .
Now here we come to the point, which most developers forget. It is rendering. After u got whatever the data, still  need to populate it .   
I will use php in this article since it has sweet inbuilt function to encode data to json format .(json_encode)
Ill just write the pseudo code .
1)      Select data from DB table
2)      Store data in php array ( array (key => value) where key will be the id column and value will be the data column )
3)      Now pass your array to inbuilt function name json_encode( $mysweetphparray );
You just have json formatted data string
 This will be the format.

{  {1:hasith},{2:Ar} }

Now return your json string .

Get json string to your client , and format json data with in ur callback function.
There are 2 sweet jquery functions  available to do this stuff.
1)parseJson
2)stringnify
3)jquery each to loop through data object nodes


Here is the psudo code ,

$.post (URL , dataposttoserver , function(datareturnbyphpfunction){

Var json =parsejson(datareturnbyphpfunction);

$.each(json, function(key, value) {

  alert(key + ': ' + value);

});



Please note ill attach working sample later regarding this . But pseudo code or idea is just enough for good engineer to catch up the stuff.







Saturday, May 21, 2011

Do we really need CMS systems like Joomla /drupal or wordpress (There are many though) ??

Well from long time I have being thinking this. By definition CMS systems are for the people who have lack of technical knowledge. With most of the popular CMS systems , you can make a website in quick time .Yes true ..! That can be done for anyone who is in any technical level . Before go further, we will just have a look what are the popular technologies used in those.

  • Php (may be ruby on rails)
  • Mysql (don’t forget SQlite or postgre)
  • Jquery plug-in s (Some are good  ... But sadly some are crappy )
Just assume u downloaded wordpress and stared developing a website. What you would do is download free wordpress plain template from internet and start integrating  with embedding ur own stuff. Nothing to worry u just wanna upload a compressed format (zip/rar) of the template they given , and rest of the stuff will be cared by wordpress. After that you would need to show some upto date news /events and some feeds. Just search for internet for plugins and integrate (Very easy).

Finally ull be really happy for your product and after u host the site even everyone will appreciate you.
Week gone.. Month gone … Now almost 1.5 years of time passes since you hosted the site. You will be really happy by seeing the site traffic and all too. But think wisely behind the scene is not beautiful as u see.
When u getting more and more traffic ,the website can face difficulties to maintain. Following are the reasons.


  •        Most parts of the CMS stored in the database and you need to pass DB hit many many times when page loads
  •     Cllient computer will experience a  issue with resource usage cz of some unnecessary browser scripts ( Unorganized js code and jquery plugins )
  •       If your website has 100 pages. You have to iterate through 100 each time to find out a single page
I hope we are clear so far.
Now lets see what u actually need to do with CMS?

  • You probably need to update your articles / posts frequently once you created.
  • You might need to create a web page on the fly.
Eventually your simplest requirement is to create webpage on the fly by few clicks .Once you create , it should make menu item and should appear in header menu.
Now will see what are the main parts of web page .

1) Page Title
2) Page content

If you think a bit..Using this u can do more. Think wisely … If u make unique title, can request your content belongs to it. And again once u makes several pages, u endup with title list.
Then can’t u make that list as your website main menu?
How simple it is?  Once you created a simple core application then style it using effective template engine.
Lately we can add caching for static types , may be using simple  json cache .
Security can be main issue for you , but can manage with third party system like OAuth when needed.
Here I’m not going to explain in details .But later with ma articles ill provide a sample CMS system I developed for ma own purposes.





Friday, February 18, 2011

What is SOA ( Service Oriented Architecture ) ? Simple picture

Often  I heard  some  developers  used to say  that , “ I am open source person “  or I am Microsoft person else I am mobile guy . Well, having competence and expertise knowledge on one selected programming language is a good thing. And it is must. But do you know? Today whole world is moving towards service oriented architecture. (SOA). Naturally people always willing to favor the platform where they good at. We can’t change it (difficult …: P). so best thing is to host your programme as a service which is independent from the consumer’s platform.  So no more blames and no more platform / language criticisms.

We will take simple story. Your familiar with Microsoft technologies, your brother is a totally open source person. Interesting scene is your father is a good mobile application developer.
Father asks you all to develop simple monthly expense calculator to make family budget for month.
Sad part of the story is you all lives separately at 3 ends. So how do you solve this problem?
Here is the answer.
Develop small samples with your favorite languages (platform) which calculate all ur expenses and return some common output (ill later come to this common thing).
All three applications have common input output and endpoints (End point can be differing).

  • Inputs- your expenses
  • Output – Total
 This endpoint is a magic. It will be SOAP or REST (I always prefer REST).
Endpoints simply handle your output and return it as some common medium.
Simply as a XML or lightweight JSON (REST is perfect architectural style to handle  JSON result which returned by service )
 Then finally your father can make simple java client to his mobile and speak with  services you all hosted , via  endpoints .( REST/SOAP)

I didn’t try make fed up you all by explaining shitty technical terms in details. But actually that is what happens in SOA.
Microsoft delivers kind of framework name WCF by enveloping all technologies related to SOA. Ill provide WCF sample in my next article.
Good luck guys.

Wednesday, February 16, 2011

How to use web technologies effectively ?

A good engineer should have capability to provide good technical solutions as well as business solutions .Understanding business domain and, design a scalable /flexible, clean /readable, architecture is never easy. Even after modeling the system next step is to find out technical feasibilities. How to achieve it technically. Well this article is small support for initiatives that love and live with technologies, and finds suitable /perfect technical solutions.
Web is a massive area. Many stuff to learn. Updating frequently. So best thing is keep in touch refresh the stuff always. When you have a good look you can find out few big nouns/ names with today’s web standard (web 2.0 -3.0).
  • SOA (simply I take Web services)
  • Jquery (Second most beautiful JavaScript framework …. I always love EXTJS /Sencha first because I started career with it)
  • JSON (Lightweight JS objects ... Perfect replacement to XML)
  • MVC (A simple yet awesome pattern mostly use in open source frameworks (codeignitor) / CMS systems (word press))

Lot of lazy young programmers today willing to study server side and backend only. Eventually they never see complete picture of the applications. Sometimes they make complex logic and succeeded with server side code but stuck around final steps. Reason is lack of knowledge about the client side. Always keep in mind today’s way is “MUCH MORE INTO CLIENTSIDE “. Once you make everything in server side your application stuck with performance issues. Once many users use your application concurrently it hardly uses many server resources.
If you make your logic in server and pass the result as serialized object. What do you think? Magic starts …
You can easily consume your result nice and awesome manner in client side to format with classic output. (Then you are a super hero).
What you need to learn to do it so? Jquery and JSON, Simple yet beautiful technologies always used to do client side magic. What you do in client side, working on your client’s computer/ browser and no droughts it is efficient.

You will start blame me. “Crazy nothing is clear “. This article is a series of upcoming articles and ill stepwise explain how to take good approach in web projects.