Javascript hell

In 1997-200x days, when I have been programming in VB3-6 , there was a problem named ”DLL Hell”.

I have been hoping that, with the introduction of .NET, we will escaping this – and I have had my request answered( ok, from .NET 2 )

However, a new problem is for WebDevelopers : JavaScript Hell. Why I am telling that ? Simple :

  1. I have an application that works well in IE 8Star – and uses Json.Stringify to post data. Normally, when I was trying with IE7, it does not work – because JSON.Stringify was not allowed …Of course , solution was adding javascript from http://www.json.org/js.html .
  2. Same application use jquery 1.4 + http://knockoutjs.com/ 1.1.2 (to can modify dynamically some textboxes) + other js( a date time formatter,jquery ui and others)- . Now , in IE9 RC , jquery1.4 does  not function – and you have to use jquery1.5.1.rc also.Please tell me how to know what modifications the other js files must have( in particular knockout) .

And the mock idea is not solving the issue. You must test either manually , either with an automated test for browser(like Selenium) and testing explicitely in browser version! And I have  speak only about IE, not Firefox and /or Chrome…

If you are a developer, how many javascript do you use? If more than 2 ,welcome to javascript hell!

Star : Please read the requirements from http://msdn.microsoft.com/en-us/library/cc836459%28v=vs.85%29.aspx :

Note:

Starting with JScript 5.8, by default, the JScript scripting engine supports the language feature set as it existed in version 5.7. This is to maintain compatibility with the earlier versions of the engine. To use the complete language feature set of version 5.8, the Windows Script interface host has to invoke IActiveScriptProperty::SetProperty.

Internet Explorer 8 opts into the JScript 5.8 language features when the document mode for Internet Explorer 8 is “Internet Explorer 8 Standards” mode. For other document modes, Internet Explorer uses the version 5.7 feature set.

JScript 5.8 includes native JavaScript Object Notation (JSON) support and the accessor methods for Document Object Model (DOM) prototypes.

Asp.NET MVC, Jquery and Razor – Cascading dropdown, retrieving partial views, Json send objects, handling errors

There are many blogs and other posts on using jQuery with MVC. You can find individual posts on:

·         How to send objects to an action method via JavaScript.

·         How to retrieve partial views.

·         How to handle errors.

asp.net mvc jquery razor cascading demo

I have written a sample showing how to do all of these together. The sample is written in  MVC 3 using Razor views

The sample demonstrates:

  1. Cascading dropdown ( see the button Populate from action and Cascading drop down)
  2. Populating a table from a Partial View(see the button Add New Employee(jquery call action and render)
  3. How to handle  error from action ( press Save 2 )
  4. How to send id parameters (long) from javascript to action ( press Delete 1 or Delete 2)
  5. How to send objects(Employee) from javascript to action ( press Save 1 or Save 2)

Without further ado, this is the project . Enjoy!

Jquery MVC Razor demo full

PS: If you seems that the code is too hard, here is a more detailed explanation :http://msprogrammer.serviciipeweb.ro/2011/12/05/jquery-ajax-request-and-mvcdetailed/

Programmer Sql Server Management settings

Please left click on “Tools (menu item)” in “Microsoft SQL Server Management Studio”
clip_image001[3]
Please left click on “Tools (menu item)” in “Microsoft SQL Server Management Studio”
clip_image002[3]
Please left click on “Options… (menu item)” in “&Tools”
clip_image003[3]
Please left click on “Prevent saving changes that require table re-creation (check box)” in “Options”
clip_image004[3]
Please left click on “Environment (outline item)” in “Options”
clip_image005[3]
Please left click on “At startup: (combo box)” in “Options”
clip_image006[3]
Please left click on “Open Object Explorer and new query (list item)”
clip_image007[3]
Please left click on “Text Editor (outline item)” in “Options”
clip_image008[3]
Please left click on “Transact-SQL (outline item)” in “Options”
clip_image009[3]
Please left click on “Line numbers (check box)” in “Options”
clip_image010[3]
Please left click on “Query Execution (outline item)” in “Options”
clip_image011[3]
Please left click on “Query Results (outline item)” in “Options”
clip_image012[3]
Please left click on “SQL Server (outline item)” in “Options”
clip_image013[3]
Please left click on “SQL Server (outline item)” in “Options”
clip_image014[3]
Please left click on “Results to Grid (outline item)” in “Options”
clip_image015[3]
Please left click on “Include column headers when copying or saving the results (check box)” in “Options”
clip_image016[3]
Please left click on “Multiserver Results (outline item)” in “Options”
clip_image017[3]
Please left click on “Add server name to the results (row)” in “Options”
clip_image018[3]
Please left click on “Page down (push button)” in “Options”
clip_image019[3]
Please left click on “OK (push button)” in “Options”
clip_image020[3]

MVC Zip Result

Sometimes you need to send to the user more than 1 file – or, maybe, the file is too large
The simplest way is : made a zip file that contains the others.

What do you need
1. SharpzipLib from http://www.icsharpcode.net/opensource/sharpziplib/ ( or download via NuGet in VS)
2. obtain the file(s) that you want as a string or as a byte[] – let’s say you have a byte[] to store in a str variable
3. make in your action something like that:

 var fcr = new ZipResult("Export.xls", str);
            fcr.AddFile("readme.txt","this zip file contains ..");
            return fcr;

4. copy the following code in your asp.net mvc projects:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using System.Text;

namespace utilsResult
{
    public class ZipResult : FileResult
    {

        
        private Dictionary<string, byte[]> content = new Dictionary<string, byte[]>();
        public string FileNameZip;
        public ZipResult(string FileName, byte[] Contents)
            : base("application/octet-stream")
        {
            this.FileDownloadName = Path.GetFileNameWithoutExtension(FileName) + ".zip";
            AddFile(FileName, Contents);
        }
        public void AddFile(string FileName,  byte[] Contents)
        {
            content.Add(FileName, Contents);
        }
        public void AddFile(string FileName,string Contents, Encoding e = null)
        {
            if (e == null)
                e = ASCIIEncoding.ASCII;

            content.Add(FileName, e.GetBytes(Contents));
        }

        protected override void WriteFile(HttpResponseBase response)
        {

            using (ZipOutputStream zos = new ZipOutputStream(response.OutputStream))
            {
                zos.SetLevel(3);
                zos.UseZip64=UseZip64.Off;

                foreach (var item in content)
                {
                    ZipEntry ze = new ZipEntry(item.Key);
                    ze.DateTime = DateTime.Now;                    
                    zos.PutNextEntry(ze);
                    int count=item.Value.Length;
                    zos.Write(item.Value, 0, count);
                   
                    
                }                
            }
        }
    }
}

5. future improvements:
Zip the file(s) in a dll project to made fully testable!

ASP.NET MVC make users–roles fast

please left click on “Microsoft SQL Server Management Studio (push button)”
clip_image001
please create a database in “Microsoft SQL Server Management Studio”
clip_image002
please left click on “C:\Windows\system32\cmd.exe (push button)” and enter the following command : aspnet_regsql.exe -E -S .\SQLExpress -d proprii -A rm
clip_image003
please left click on “ListPanel (list)” in “Microsoft SQL Server Management Studio”
clip_image004
please left click on “Tables (outline item)” in “Microsoft SQL Server Management Studio”
clip_image005
please keyboard input in “Microsoft SQL Server Management Studio” [F5]
clip_image006
please right click on “aspnet_Applications (list item)” in “Microsoft SQL Server Management Studio”
clip_image007
please left click on “Select Top 1000 Rows (menu item)”
clip_image008
please left click in “Microsoft SQL Server Management Studio”
clip_image009
please left click on “aspnet_Users (list item)” in “Microsoft SQL Server Management Studio”
clip_image010
please right click on “aspnet_Users (list item)” in “Microsoft SQL Server Management Studio”
clip_image011
please left click on “Select Top 1000 Rows (menu item)”
clip_image012
please left click on “RdcDAL – Microsoft Visual Web Developer 2010 Express (Administrator) (push button)”
clip_image013
please edit web.config in your MVC application and change the connection string to point to your database
clip_image014
please scroll down until membership and change application name from / to /applicationName
clip_image015
please run your application and left click on “Log On (editable text)” in “Home Page – Windows Internet Explorer”
clip_image016
please left click on “User name (editable text)” in “Log On – Windows Internet Explorer”
clip_image017
please left click on “Register (editable text)” in “Log On – Windows Internet Explorer”
clip_image018
please left click on “User name (editable text)” in “Register – Windows Internet Explorer”
clip_image019
please input your details (I have put mine : ignatandrei)
clip_image020
please see that I am registered to the site
clip_image021
please verify to the database – left click on “Microsoft SQL Server Management Studio (push button)”
clip_image022
please left click in “Microsoft SQL Server Management Studio”
clip_image023
please press [F5] in “Microsoft SQL Server Management Studio”
clip_image024
please see the results – application name
clip_image025
please left click on “Execute (push button)” in “Microsoft SQL Server Management Studio” for the query that lists users.
clip_image026

Programmer retrospective – 2010

What have I done in 2010 ?
1. I started this blog in English( I have one in Romanian, too)

2. I have made a book (outdated) about ASP.NET MVC 2 tips and tricks , published on Amazon .
3. I have initiated a book about programming in VS2010 – alas, it’s in Romanian at http://ronua.ro/CS/media/p/216886.aspx

4. I have learned ASP.NET MVC by answering to the questions at 1146 and I have nominated moderator on ASP.NET forums.

5. I have made a project for Azure – it is call “backup” – a project that you can deploy on Azure and have abackup for your files – like Mozy or DropBox . It is here :http://myazurebackup.codeplex.com/
(and those are other mine projects on codeplex:

AjaxForWinForms
Change Data Capture Helper
CHM2Word
Cropper.TimeCapture
IEnumerable – DataTable Exp…
PrintScreen
SqlConsolidate
UsingLib
vhd to boot entry
windows azure backup

It was a good year for me!

Hope it was for you too!

Optimizing EF4 and EDMGen2

Just reading (from Adrian Florea links ) how the EF4 could be optimized : http://www.codeproject.com/KB/database/PerfEntityFramework.aspx

First modification is to “Pre-generated Your View”. For this you must have .ssdl, .csdl, and .msl files – so you change the “Metadata Artifact Processing property to: Copy to Output Directory.”. Then you process the .ssdl, .csdl, and .msl with edmgen in order to can have the  views.

Until here , all ok.

But, in the next advice, is to keep “Metadata Artifact Processing property to: Embed in Output Assembly.”

One solution is to put “Metadata Artifact Processing property to: Copy to Output Directory.” , compile, put again “Metadata Artifact Processing property to: Embed in Output Assembly.” and compile again. But , if you change the edmx (add fields or tables ) you must redo the operation  – so you will have more things to do(if you remember)

A solution is to build them on the pre-build step .But how to generate the .ssdl, .csdl, and .msl files  ?

Edmgen2 , http://code.msdn.microsoft.com/EdmGen2 , to the rescue. Download, put into a lib folder under your solution folder and put a pre-build like this :

$(SolutionDir)lib\edmgen2\edmgen2 /FromEdmx $(ProjectDir)prod.edmx”

“%windir%\Microsoft.NET\Framework\v4.0.30319\EdmGen.exe” /mode:ViewGeneration /language:CSharp /nologo “/inssdl:prod.ssdl” “/incsdl:prod.csdl”   “/inmsl:prod.msl” “/outviews:$(ProjectDir)prod.Views.cs”

What you must change on your project?

1. IF you are on 64 bit, change Framework to Framework64

2. change the prod with your edmx name.

What is the performance?

Tested by loading a table with 301 rows by doing the steps :

1. open connection, load all table in objects(POCO), closing connection

2. open connection , find object with PK = 1, closing connection

3. open connection ,  loading 1 tables with 2 related (include ) , closing connection

The results are in milliseconds:

Without pre-compiled views

LoadTable LoadID LoadMultiple Total Time
579 800 172 1551
563 755 171 1489
559 754 169 1482
568 762 240 1570

With pre-compiled views:

LoadTable LoadID LoadMultiple Total Time
606 807 183 1596
509 706 177 1392
852 137 192 1181
530 733 221 1484
523 722 183 1428

The average / min / max results:

average max min
without 1523 1570 1482
with 1413.25 1596 1181

In the next picture the smaller the duration(milliseconds), is the better :

image

Conclusions:

1.  For the average and min the difference is 7%, respectively 20%. Please remember we are using only 3 queries.

For the max, it is vey curious : the with is more than without. The penalty is 1%, I think that is a measuring error ? – or maybe not. However , the penalty is small comparing with others.

2. Very curious, find after ID, with a table with 301 rows, took longer than loading the whole table.However, did not take into accound finding in the list the object( it is in memory also)

3. It may worth to add the pre-build step shown before to pre-compile views.

Links :

http://www.codeproject.com/KB/database/PerfEntityFramework.aspx

http://msdn.microsoft.com/en-us/library/bb896240.aspx