Thursday, September 18, 2014

What is difference between N-tier vs N-layer architecture ?

N-tier and n-layer are entirely different concepts. People often use this term interchangeably during the design of the application architecture. N-tier refers to the actual n system components of your application. For ex: Suppose you create a web application, its components include your application server where it is being hosted, the database being used with it, on another server and the user machine who access the application, forms the third component. These are referred to as the 3 tiers of your application. So they may be n in number and so the term n-tier application. So tiers are the physically separate components of the same system. On the other hand, layers refer to the internal architecture of your component. For ex: in your code, you divide the code into different layers like Data access layer, Business logic layer etc.. So they are internal to the component and these layers interact with each other internally to form the entire component.

How to display database data in webgrid in mvc 4

http://dotnetawesome.blogspot.in/2014/04/how-to-display-database-data-in-webgrid-in-mvc4.html
http://dotnetawesome.blogspot.in/2014/06/how-to-implement-custom-user-defined-minimum-age-validation-rule-mvc4.html#at_pco=smlwn-1.0&at_tot=1&at_ab=per-13&at_pos=0&at_si=541b0ca472ba1312

Wednesday, September 17, 2014

Difference between finalize and dispose methods in c#

Difference between Finalize & Dispose Method
Finalize
Dispose
Used to free unmanaged resources like files, database connections, COM etc. held by an object before that object is destroyed.
It is used to free unmanaged resources like files, database connections, COM etc. at any time.
Internally, it is called by Garbage Collector and cannot be called by user code.
Explicitly, it is called by user code and the class implementing dispose method must implement IDisposable interface.
It belongs to Object class.
It belongs to IDisposable interface.
Implement it when you have unmanaged resources in your code, and want to make sure that these resources are freed when the Garbage collection happens.
Implement this when you are writing a custom class that will be used by other users.
There is performance costs associated with Finalize method.
There is no performance costs associated with Dispose method.
For Example,
  1. // Implementing Finalize method
  2. public class MyClass
  3. {
  4. //At runtime C# destructor is automatically Converted to Finalize method.
  5. ~MyClass ()
  6. {
  7. //TO DO: clean up unmanaged objects
  8. }
  9. }
For Example,
  1. // Implementing Dispose method
  2. public class MyClass : IDisposable
  3. {
  4. private bool disposed = false;
  5.  
  6. //Implement IDisposable.
  7. public void Dispose()
  8. {
  9. Dispose(true);
  10. }
  11.  
  12. protected virtual void Dispose(bool disposing)
  13. {
  14. if (!disposed)
  15. {
  16. if (disposing)
  17. {
  18. //TO DO: clean up managed objects
  19. }
  20.  
  21. //TO DO: clean up unmanaged objects
  22.  
  23. disposed = true;
  24. }
  25. }
  26. }

Note

  1. You cannot override the Finalize method in the C# or C++ languages. But you can override Finalize method in VB.NET since it does not support destructor.
  2. You should not implement a Finalize method for managed objects, because the garbage collector cleans up managed resources automatically.
  3. A Dispose method should call the GC.SuppressFinalize() method for the object of a class which has destructor because it has already done the work to clean up the object, then it is not necessary for the garbage collector to call the object's Finalize method.
  1. // Using Dispose and Finalize method
  2. public class MyClass : IDisposable
  3. {
  4. private bool disposed = false;
  5.  
  6. //Implement IDisposable.
  7. public void Dispose()
  8. {
  9. Dispose(true);
  10. GC.SuppressFinalize(this);
  11. }
  12.  
  13. protected virtual void Dispose(bool disposing)
  14. {
  15. if (!disposed)
  16. {
  17. if (disposing)
  18. {
  19. // TO DO: clean up managed objects
  20. }
  21.  
  22. // TO DO: clean up unmanaged objects
  23.  
  24. disposed = true;
  25. }
  26. }
  27.  
  28. //At runtime C# destructor is automatically Converted to Finalize method
  29. ~MyClass()
  30. {
  31. Dispose(false);
  32. }
  33. }

UNDERSTANDING TEMPDATA IN DETAIL

What is TempData?

Tempdata let us maintain data within single request cycle
Example:
publicActionResult M1()
{
TempData["a"] = "Value";

string s = TempData["a"].ToString();
returnRedirectToAction("M2");
}

publicActionResult M2()
{
string s = TempData["a"].ToString(); // Data will be available 
returnRedirectToAction("M3");
}
publicActionResult M3()
{
string s = TempData["a"].ToString(); // Data will be available 
returnview ("Some View"); // Data will be available inside view also
}

Can we use Tempdata to pass data from Controllers to View?

Many people says No but reality is YES.
Example:
Sample 1 -
publicActionResult M1()
{
TempData["a"] = "Value";
return view("MyView"); // MyView can access Tempdata
}
Sample 2 -
publicActionResult M1()
{
TempData["a"] = "Value";
returnRedirectToAction("M2"); }

publicActionResult M2()
{ 
return view("MyView2");// MyView2 can access Tempdata
}
Sample 3 -
publicActionResult M1()
{
TempData["a"] = "Value";
returnRedirectToAction("M2"); }
}
publicActionResult M2()
{ 
 string s = TempData["a"].ToString(); // Data will be available 
 return view("MyView2");// MyView2 can access Tempdata
}

When the Tempdata values get removed?

  • At the end of the request values in the tempdata get removed automatically considering values are marked for deletion.
  • Values will be marked for deletion as soon as we read it.
Example:
Sample 1 -
publicActionResult M1()
{
TempData["a"] = "Value";
return view("MyView"); // MyView can access Tempdata
}
Sample 2 -
publicActionResult M1()
{
 TempData["a"] = "Value";
 string s = TempData["a"].ToString(); // Data will be available 
 return view("MyView"); // MyView can access Tempdata
}
Note: In the above two samples values are marked for deletion as soon it get read. Now for the next request values won’t be available.
Sample 3 -
publicActionResult M1()
{
 TempData["a"] = "Value";
 return view("MyView"); // MyView can access Tempdata but it wont access it
}

publicActionResult M2()
{
 string s = TempData["a"].ToString(); // Data will be available 
 return view("MyView2"); // MyView can access Tempdata
}

In the above sample,
  • Action method M1 creates tempdata which will be available throughout that request, but neither inside M1 nor inside MyView (which is the view here) values are read and hence values wont get removed at the end of the request.
  • When user makes a fresh request to M2 values created in the last request will be available and cycle continues. Means values will get removed at the end of this second request considering it will be read before request ends.

Keep and Peek methods

Keep

Keep method makes the values in the tempdata persistent for next request.
Example
Sample 1 -
publicActionResult M1()
{
 TempData["a"] = "Value";
 TempData["b"] = "Value1";
 TempData["c"] = "Value2";
 TempData.Keep();
 return view("MyView"); //My View can access all three tempdata values and even it will access
}
In above sample all three tempdata values will be maintained for next request regardless of the whether values are read or not.
Sample 1 -
publicActionResult M1()
{
 TempData["a"] = "Value";
 TempData["b"] = "Value1";

 TempData.Keep();

 TempData["c"] = "Value2";
 return view("MyView"); //Let say My View displays all three tempdata values
}
In the above sample only Tempdata with key “a” and “b” will be available for next request. Tempdata with key “c” get removed considering it was used inside view.
Sample 3 -
publicActionResult M1()
{
 TempData["a"] = "Value";
 TempData["b"] = "Value1";
 TempData["c"] = "Value2";
 TempData.Keep(a);
 return view("MyView"); //My View can access all three tempdata values and even it will access
}
In the above sample only tempdata with key “a” will be made available. Availability of tempdata with key b and c depends on the criteria “Whether value used in the current request or not”.

Peek

Peek will let us retrieve the tempdata value without marking it for deletion.
Example without Peek:
publicActionResult M1()
{
TempData["a"] = "Value";
returnRedirectToAction("M2");
}
publicActionResult M2()
{
string s = TempData["a"].ToString(); // Data will be available 
return view ("Some View"); // Data will be available inside view also
}
.
.
.
publicActionResult M3()
{
string s = TempData["a"].ToString(); // Will get an Exception 
…
}
In above sample Tempdata values (created inside Action method M1) won’t be available for next request because it already used in current request (in Action method M2).
When user make a fresh request to action method M3, null reference exception will be thrown.
Example with Peek:
publicActionResult M1()
{
TempData["a"] = "Value";
returnRedirectToAction("M2");
}
publicActionResult M2()
{
string s = TempData.Peek("a").ToString(); // Data will be available 
return view ("Some View"); // Data will be available inside view also but won’t be used
}
.
.
.
publicActionResult M3()
{
string s = TempData["a"].ToString(); // Will just work 
…
}

C# code for seperating lowes case, upper case,symbol, numbers and printing it as o/p with as individual group !

 Source Code:
Eg. S%t4*s8f2g# then o/p must be
 Lower case : t s f g 
Upper case : S 
Numbers : 4 8 2 
Symbols : % * #
 like this

string full = "AaBcDarR%4~@12'#3";

string lower = new string(full.Where( c => char.IsLetter(c) && char.IsLower(c) ).ToArray());

string upper = new string(full.Where( c => char.IsLetter(c) && char.IsUpper(c) ).ToArray());

string numeric = new string(full.Where( c => char.IsNumber(c) ).ToArray());

string symbols = new string(full.Where( c => char.IsLetterOrDigit(c) == false ).ToArray());


Console.WriteLine(lower);

Console.WriteLine(upper);

Console.WriteLine(numeric);

Console.WriteLine(symbols);