27.11.16

MVC MiniProfiler - Installation and Setup


MVC MiniProfiler - Installation and Setup
https://www.codeproject.com/articles/516627/miniprofilerplus-plusinstallationplusandplussetup

24.11.16

Angular nested tabs and ui-router


1.) Angular nested tabs and ui-router

http://stackoverflow.com/questions/36043556/angular-nested-tabs-and-ui-router

2.) UI-Router: Why many developers don’t use AngularJS’s built-in router

http://www.funnyant.com/angularjs-ui-router/

22.11.16

MVC Dependency Injection Best Practices in an N-tier Modular Application

ASP.NET MVC 5: Using a Simple Repository Pattern for Performing Database Operations


http://www.dotnetcurry.com/aspnet-mvc/1155/aspnet-mvc-repository-pattern-perform-database-operations


Dependency Injection Best Practices in an N-tier Modular Application


http://www.developer.com/net/dependency-injection-best-practices-in-an-n-tier-modular-application.html

19.11.16

Performance or Best Practices of Asp.net MVC Application

MVC Performance
Index:
1.)      Using Asynchronous Methods in ASP.NET MVC 4
2.)      Profile and debug your ASP.NET MVC app with Glimpse
3.)      Bundling and Minification
4.)      Using CDNs and Expires to Improve Web Site Performance
5.)      Donut Caching and Donut Hole Caching with Asp.Net MVC 4
6.)      Use Caching
7.)      Improve perceived performance of ASP.NET MVC websites with asynchronous partial views
9.)      Run your ASP.NET sites in release mode
10.)   MiniProfiler in ASP.NET MVC 4
12.)   Use AJAX
13.)   If using only Razor in MVC
14.)   Put style sheets at the top and scripts at the bottom
15.)   Make JavaScript and CSS External
16.)   Optimizing images
17.)   Avoid Empty Image src
18.)   Replaced Json.Net with a faster library (e.g. ServiceStack)
19.)   Use caching and lazy loading in a smart way
20.)   Do not put C# code in your MVC views
21.)   Use Fire & Forget when applicable
22.)   Use monitoring and diagnostic tools on the server

Summary

1.)      Using Asynchronous Methods in ASP.NET MVC 4
The ASP.NET MVC 4 Controller class in combination .NET 4.5  enables you to write asynchronous action methods that return an object of type  Task.
Making these web service calls asynchronous will increase the responsiveness of your application.


Why Use:
This might not be a problem, because the thread pool can be made large enough to accommodate many busy threads. However, the number of threads in the thread pool is limited (the default maximum for .NET 4.5 is 5,000). In large applications with high concurrency of long-running requests, all available threads might be busy. This condition is known as thread starvation.
When you’re doing asynchronous work, you’re not always using a thread. For example, when you make an asynchronous web service request, ASP.NET will not be using any threads between the async method call and the await. 

Where to Use:
You're calling services that can be consumed through asynchronous methods, and you're using .NET 4.5 or higher.
Parallelism is more important than simplicity of code.
You want to provide a mechanism that lets users cancel a long-running request

To realize the benefits of an asynchronous web application, you might need to make some changes to the default server configuration. You might need to increase the HTTP.sys queue limit from the default value of 1,000 to 5,000. See view Link for whole Configuration.


2.)      Profile and debug your ASP.NET MVC app with Glimpse
While Fiddler and the F-12 development tools provide a client side view, Glimpse provides a detailed view from the server. You can install Glimpse from the NuGet package manager console or from the Manage NuGet Packages console.

3.)       Bundling and Minification
Why use: Most of the current major browsers limit the number of simultaneous connections per each hostname to six. That means that while six requests are being processed, additional requests for assets on a host will be queued by the browser.

Bundling is a new feature in ASP.NET 4.5 that makes it easy to combine or bundle multiple files into a single file. You can create CSS, JavaScript and other bundles. Fewer files means fewer HTTP requests and that can improve first page load performance.
Minification performs a variety of different code optimizations to scripts or css, such as removing unnecessary white space and comments and shortening variable names to one character

Bundle Caching: Bundles set the HTTP Expires Header one year from when the bundle is created.

4.)      Using CDNs and Expires to Improve Web Site Performance
Content Delivery Network (CDN) to deliver static content ( jQuery, images, CSS files, etc.)
If another site has loaded your jQuery version from the Microsoft CDN, and your web site requests that version from the Microsoft CDN, the client cache can satisfy the request, eliminating the download cost.
Each browser has its own cache, so Firefox won’t use files cached by Chrome or IE.


5.)      Donut Caching and Donut Hole Caching with Asp.Net MVC 4
Donut caching is the best way to cache an entire web page except for one or more parts of the web page
Donut Caching was introduced which cached only one copy of the entire page for all the user except for a small part which remain dynamic.
Why Use: If you want to cache all these pages for all the users by using Output Cache with VaryByParam UserID, then the entire page would be cached every time for each user with a different user name (or whatever your dynamic part of the page is). This is not a good practice since there will be 1000 cached pages if there are 1000 logged in user at a time.

6.)      Use Caching:
The output of the Index () action method will be cached for 20 seconds. If you will not have defined the duration, it will cache it for by default cache duration 60 sec.
[OutputCache (Duration=20, VaryByParam="none")]

7.)      Improve perceived performance of ASP.NET MVC websites with asynchronous partial views
Moved the CSHTML responsible for rendering each slow section to a partial view if it was not already done.
Use jQuery to find and call. load () for each section.

Asynchronous database calls are not as straight forward as other types of asynchronous operations but sometimes it will gain so much more responsiveness to our applications. We just need to get it right and implement them properly.

9.)      Run your ASP.NET sites in release mode

10.)   MiniProfiler in ASP.NET MVC 4
MiniProfiler is a "simple but effective" profiler you can use to help find performance problems and bottlenecks.
Url: http://odetocode.com/Blogs/scott/archive/2012/09/11/elmah-and-miniprofiler-in-asp-net-mvc-4.aspx

While developing WEB Applications, when you have a requirement to retrieve data from multiple services at the same time, then it is a good idea to use Asynchronous action methods in .NET 4.5. This will make your code less complex and easy to maintain.
Url: http://www.devcurry.com/2013/04/aspnet-mvc-4-making-asynchronous-calls.html
12.)  Use AJAX
Use AJAX to update components of your UI, avoid a whole page update when possible.

13.)   If using only razor in mvc
 If you use Razor, add the following code in your global.asax.cs, by default, Asp.Net MVC renders with an aspx engine and a razor engine. This only uses the RazorViewEngine.
ViewEngines.Engines.Clear();
ViewEngines. Engines. Add.  (new RazorViewEngine ());

14.)   Put style sheets at the top and scripts at the bottom

Url: http://thomasardal.com/yslow-rule-5-and-6-%E2%80%93-put-style-sheets-at-the-top-and-scripts-at-the-bottom/

15.)   Make JavaScript and CSS External
Using external files in the real world generally produces faster pages because the JavaScript and CSS files are cached by the browser.

16.)   Optimizing images
One way to solve this is to reduce the size of the images or in other words, optimize the images. 

The following VSextension will help to optimize the images from the Visual Studio IDE itself,

17.)   Avoid Empty Image src
Otherwise, the browser makes another request to your server.
18.)   Replaced Json.Net with a faster library (e.g. ServiceStack)
https://aspguy.wordpress.com/2014/02/17/building-high-performance-asp-net-applications/

19.)   Use caching and lazy loading in a smart way
Another approach for improving the responsiveness of your site is using Lazy Loading. Lazy Loading means that an application does not have a certain piece of data, but it knows that where is that data.

20.)   Do not put C# code in your MVC views
if you include too much C# code in them, your code will not be compiled and placed in DLL files. Not only this will damage the testability of your software but also it will make your site slower because every view will take longer to get display (because they must be compiled).
21.)   Use Fire & Forget when applicable
if users can sign-up and create an account in your web site, and once they register you save their details in the database and then you send them an email, you don’t have to wait for the email to be sent to finalize the operation.
In such a case the best way of doing so is probably starting a new thread and making it send the email to the user and just get back to the main thread. This is called a fire and forgets mechanism which can improve the responsiveness of an application.
22.)   Use monitoring and diagnostic tools on the server
There might be many performance issues that you never see them by naked eyes because they never appear in error logs. Identifying performance issues are even more daunting when the application is already on the production servers where you have almost no chance of debugging.
To find out the slow processes, thread blocks, hangs, and errors and so forth it’s highly recommended to install a monitoring and/or diagnostic tool on the server and get them to track and monitor your application constantly. I personally have used NewRelic (which is a SAS) to check the health of our online sites.
https://newrelic.com/





Asp.net MVC: What to used instead of Session and Cookie


These are my findings while searching that what to use instead of Session and Cookie:

Summary:
1.) If we are creating a "Angular based Application" then 'Local Storage' is the best choice instead of 'Session and Cookie'.
2.) If we are creating a "Mvc application" then we can use 'Local Storage' in the places where data shows on the client side and if we
need data on server side, we need to send that data with previous page URL or send with current page Ajax request.

Also we used Tempdata (instead of Session), QueryString, Hiddenfield , Html5 attributes, Ncache, Redis, Memcached instead of
'Session and Cookie'.


TempData: TempData is session, so they're not entirely different. However, the distinction is easy to understand, because TempData is for
redirects, and redirects only. So when you set some message in TempData and then redirect, you are using TempData correctly. Tempdata can
be used to maintain data between controller actions as well as redirects.

QueryString: Use for small amount of unsecured data.Passing data in query string is not a good choice if those are secure and large. Also
query string too have it's own limit. So you can not pass data after certain limit.

Local Storage: Use for client side. You are not using this data server side, so you don't need a cookie.local Storage is never sent to the server
unlike a cookie.


NCache: NCache is an extremely fast and scalable Open Source distributed cache for .NET applications. Use NCache for database caching,
ASP.NET Session State storage, ASP.NET View State Caching, and much more. NCache provides high performance in-memory ASP.NET session clustering
for web farms that is faster and more scalable than storing them in a database. It is faster because sessions are kept in memory and closer
to the ASP.NET application
Code Demo:
a.) Insert Data:
Product product = new Product();
product.ProductID = 1001;
string key = "Product:" + product.ProductID;
try
{
cache.Add(key, product);
}
b.) Retrieve Data:
string key = "Product:1001";

object result = cache.Get(key);
Redis: Redis is perfect for storing sessions. All operations are performed in memory, and so reads and writes will be fast.
Code Demo:

a.) Insert Data: redisClient.Set(elementKey, "some cached value");

b.) Retrieve Data: redisClient.Get("some cached value");
Memcached: A highly available, high performance ASP.NET session state store provider using Memcached.

Comparision NCache, Redis:
Redis, written in C is completely open source while NCache is written in .NET C# and is open core (which means it also has a paid version).
memcached and redis differ quite a bit.
Redis focuses on performance so most of its design decisions prioritize high performance and very low latencies.

Ncache supports more features than Redis.
Comparision Memcached , Redis:
Memcached is completely in memory and will lose all of its cache in case the server is restarted.
Redis is persistent, and on top of that has a lot more features (like set operations, lists, counters, etc). ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Details links of RND:

Pass data between pages without using Session and Cookie:

1.) Session and Cookies in ASP.NET MVC?
https://gregorybeamer.wordpress.com/2012/11/04/session-and-cookies-in-asp-net-mvc-oh-my/
Ans: Okay, so there is an alternative: Use TempData instead --------------------------------------------------------------------------------------------------------------------------------------------------
2.) using Local Storage

a.) Best Practice for State Management in a Distributed Asp.net MVC 4 Application
http://stackoverflow.com/questions/18161100/best-practice-for-state-management-in-a-distributed-asp-net-mvc-4-application
Ans: You have Local Storage and can use frameworks like AngularJS. Than you can minimize the number of cases, where you'd need a session
state. b.) Local Storage or Session Storage:

http://stackoverflow.com/questions/19867599/what-is-the-difference-between-localstorage-sessionstorage-session-and-cookies

c.) HTML5 offline storage - Alternative to Session? [closed]
http://stackoverflow.com/questions/11849280/html5-offline-storage-alternative-to-session
Ans: Session data is stored on the server, HTML5 offline storage is stored in the browser. If you are comfortable storing session data in
the browser, sure that will work. If you have sensitive information that should remain on the server however, keep it in sessions.

Disadvantages:
http://stackoverflow.com/questions/16855680/are-there-any-drawbacks-to-using-localstorage-instead-of-cookies
Ans: 1.)If a user disable cookies, localStorage will not work either.
2.)You are not using this data server side, so you don't need a cookie. localStorage is never sent to the server unlike a cookie
3.) The data stored in localStorage and sessionStorage can easily be read or changed from within the client/browser so should not be
relied upon for storage of sensitive or security related data within applications. 3.) using Secure Query String:

a.) MVC Encrypt Query String:
http://stackoverflow.com/questions/895586/encrypting-an-id-in-an-url-in-asp-net-mvc
Ans: you have some sensitive data in the form of a query string and want it encrypted so the end user can't see it. But you need the ability
to decrypt this value in your application to do something with it. 4.) Using Hidden Fields and html 5 Attributes:

http://stackoverflow.com/questions/11770772/how-to-get-html5-attributes-and-values-into-mvc-hiddenfor
Ans: @Html.HiddenFor(x => x.Deleted, new { @class="deleted", data_id=Model.Id }) 5.)
How can I pass parameters to a partial view in mvc 4
http://stackoverflow.com/questions/20799658/how-can-i-pass-parameters-to-a-partial-view-in-mvc-4
Ans: @Html.Partial("_SomePartial", new ViewDataDictionary { { "id", someInteger } }); 6.) NCache:

http://www.alachisoft.com/resources/docs/ncache/help/asp.net-sessions.html?mw=MjQw&st=MQ==&sct=MA==&ms=QwAAEAAAAAAAAAACASAG
NCache provides high performance in-memory ASP.NET session clustering for web farms that is faster and more scalable than storing them in a
database. It is faster because sessions are kept in memory and closer to the ASP.NET application
Code: Hashtable allSessionData = cache.GetByTag(new Tag("NC_ASP.net_session_data"));
Ncache: https://ncache.codeplex.com/
7.) Redis: http://stackoverflow.com/questions/10278683/how-safe-it-is-to-store-session-with-redis

Asp.net Redis:
https://blogs.msdn.microsoft.com/webdev/2014/05/12/announcing-asp-net-session-state-provider-for-redis-preview-release/

Integrating Redis in your Asp.Net MVC Project:
http://patrickdesjardins.com/blog/integrating-redis-in-your-asp-net-mvc-project
8.) Memcached

https://github.com/rohita/MemcachedSessionProvider

Which is better Redis, Ncache?
https://www.quora.com/Which-is-better-Redis-or-NCache
CacheManager
http://cachemanager.net/