Caching static files, js files, CSS and images in IIS 7



I will show how to configure IIS 7 to enable caching of scripts, CSS  and all other static files that will not change everyday

Select the folder that you want to enable caching and double click HTTP header

HTTP header

HTTP header

Enable caching

Enable caching

Enable caching

That’s it

And remember if you are still in the debuting time and you want to clear the cache just press ctr+f5 while you browse to clear the cache

Yield return, what does it mean?



Yield return, it’s a very strong keyword, all the LINQ to object use it when we use foreach or yield return.

It is used to iterate through a collection returned by a method. you can create methods that return a collection and don’t have to go through declare and maintain it’s state the compiler will do it for you, what does it means?

To understand the different lets see code that doesn’t use yield and code does

Without yield

class Program
 {
   static void Main(string[] args)
   {
     foreach (int i in GetInt())
     {
       Console.WriteLine(i.ToString());
       Console.Read();
     }
   }

   public static IEnumerable<int> GetInt()
   {
      List<int> nums = new List<int>();
      for (int i = 0; i < 5; i++)
      {
         nums.Add(i);
      }
      return nums;
   }
 }

With yield


class Program
 {

   static void Main(string[] args)
   {
      foreach (int i in GetInt())
      {
        Console.WriteLine(i.ToString());
        Console.Read();
      }
   }

 public static IEnumerable<int> GetInt()
 {
   for (int i = 0; i < 5; i++)
   {
     yield return i;
   }
   yield return 20;
   yield return 30;
 }

You can see here the different that you don’t need to declare a variable to hold the return collection from your method you just need to yield return the variable and if you make a break point and execute each method you will notice how this method has different execution path, because without yield the foreach of the GetInt has to be finished first and then populate the collection variable but in other case it will return after each iteration, so it will return what you real need as you need it, so it will continue in the second method where it stop.

In other word it will get iterate to bring the first number and then the caller will return to the first method “Main” and then when the method inside the Main call the second method it will go to iterate and bring the second number and return and so on.

LINQ basics


  1. Obtain the data sources(s)
  2. Create the query
    1. Declare variable var query = the following
    2. From variable Name in obtained data Source //Declaration
    3. Where variable name. Property == or > or < or any operator against any value // criteria and filtering
    4. Orderby variable name. Property, variable name. Property //sorting
    5. Select (variable name or variable name * 10 (any expression) or new { property1= val1 , property2 = val2 } (anonymous type) // projection
  3. Execute the query
    1. ToList() //by calling
    2. ToArraay() //by calling
    3. Foreach and access the query result

Example


 var files = new DirectoryInfo(@"c:\\").GetFiles(); //Obtain the data sources(s)
 var query = from file in files //Create the query //Declaration
 where file.Length > 0 // criteria and filtering
 orderby file.Name, file.Length  //sorting
 select new // projection
 {
 Name = file.Name,
 Length = file.Length,
 CreatedDate = file.CreationTime
 };
 var my = query.ToList(); //Execute the query

 //or

 foreach (var myFileInfo in query) //another Execute of the query
 {
 Console.WriteLine(myFileInfo.Name);

 }

Best Configuration of ReSharper for me



This configuration what I need to do with ReSharper after clean installation, it may fit with anyone else like me who has the same considerations with ReSharper.

Note: I will add and modify this post as my current needed so expect adding or removing configuration as needed  in the future

  • Cash

Make the cash outside the project folder for better with source control so you don’t need to make source control ignored this folder especially sometimes when we compare files local vs. server (TFS) it will compare these files (cash) and you don’t need them so you can have good and clear comparison

But remember its better that if you delete your solution or project to delete the corresponding cash, since it will not deleted because it’s not inside the project folder anymore

Resharper Cash

Resharper Cash

  • Shortcut and integration with Visual Studio

I choose ReSharper 2.x / IDEA scheme inspired by the community it may more fit with Java but I choose it and become familiar with it

Resharper shortcuts scheme

Resharper shortcuts scheme

For the shortcuts you can download it from here


http://www.jetbrains.com/resharper/documentation/documentation.html

  • Auto insert parentheses and quotes
  • Highlight current line

Actually I didn’t prefer this I used to use it and when the auto insert parentheses come you can’t see the method signature and this not good for me

Auto insert parentheses and highlight current line

Auto insert parentheses and highlight current line

Visual studio option to change color

Visual studio option to change color

  • IntelliSence

I prefer visual studio IntelliSence

Resharper IntelliSence

Resharper IntelliSence

  • Complete code behavior

Resharper complete code behavior

  • Line wrapping
Resharper Line wrapping

Resharper Line wrapping

  • Add using Namespaces
Resharper using Namespace

Resharper using Namespace

How to pass function by name variable not by body to JQuery?



It is a very small tip but the final comments are very important and it just here for fast remember, the story begin when I start re-factoring my team JQuery code, I found that there are many functions passed by body and inside these function another many functions also passed by body and most of these functions repeated with no need and the code become terrible and can’t maintained specially for someone like me that didn’t write JQuery everyday :-)

So all you need to do just declare a function and pass it like the following:

<script language="javascript" type="text/javascript">

        var func1 = function () {
            alert("Finished");
        };
        var func2 = function () {

            $("#div2").fadeOut(5000, func1);
        };
        $("#div1").click(func2);

    </script>
  • My mistake was I was pass the function with the parentheses, so I only need to write the function name and if I write the parentheses it will give me errors
  • If  I create a function that take parameters I can’t pass parameters in a callback
  • Remember who call the callback, if the JQuery do?  it will pass the parameters that needed if the callback signature require parameters like the function exist in the Ajax request callback which has parameter data that response from the request
  • The function must be declare before the usage of  it in the code so consider the arrange of the code
  • Remember you can call a function and pass a parameters normally inside any function but the key here that you can’t pass parameter when you register or passing the function to JQuery code
  • ddd
 <script language="javascript" type="text/javascript">
        var text = "Hello";
    </script>
    
    
    <script language="javascript" type="text/javascript">
        var func1 = function () {
            alert(text+" Seif and Lara");
        };
        var func2 = function () {
            
            $("#div2").fadeOut(5000, func1);
        };
        var s = "#div" + 1;
        $(s).click(func2);
      
    </script>