Monday, 27 February 2012

MVC Client Side and server Side View Engines




Introduction

Microsoft ASP.NET MVC framework follows a standard MVC pattern - the Model contains data that will be shown, the Controller performs actions when some event happens, initializes the Model, and passes it to the View, and the View takes a Model and renders the HTML output that would be sent to the client browser. This architecture is shown on the following figure:
template-mvc.png
Client(browser) sends some HTTP web request to the server-side. On the server we have controllers that handles request, takes the data using the model, and pass it to the view. View generates HTML that should be sent back to client. In MVC 3, there are several view engines that can be used - standard ASP.NET view engine, Razor, Spark, NHaml, etc. All of these view engines use different syntax for generating the view; however, they are all working in the same way - the model is taken on the server-side, formatted using the template, and HTML is sent to the client browser.
This article introduces a different concept of view engine - a client-side view engine that renders a view in a client browser. In this concept, the model and the controller are still on the server-side. However, instead of the HTML, JSON is generated as an output from the server-side, it is accepted on the client-side, and HTML is generated using the JavaScript templating engine. This architecture is shown on the following figure:
template-json.png
Sending HTTP request to the controller and handling model is same as in the standard method. However, instead of passing model object directly to the server-side view, in this case model is sent to the client side formatted as a JSON object where it is processed by the client side view engine.

Background

This example shows how the client-side JavaScript template engine in the ASP.NET MVC project can be used instead of the standard server-side templating engines. JavaScript templating engines use JSON objects as a model and generate HTML on the client side. Principle of JavaCript template engine is shown on the following figure:
templating-engine.png







Date Diff in C#

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

After rereading some of your posts it seams like a better understanding of what .net really is is what is needed for you to find an answer to your question.  The DateDiff method that is contained in Microsoft.VisualBasic is not a visual basic method, nor is it a C# method.  It is a .net method contained in a .net library.  It was created to give programmers who use a language other than VB some of the functionality that is included in VB as part of the runtime.  May I suggest reading the wikipedia articles on CLR and CLI:
Common_Language_Runtime
Common_Language_Infrastructure

These, along with the description at the top of this page (List_of_CLI_languages ) which I found useful, should help you solve your problem.

If you are still unsatisifed, then you can always continue along the line of my previous post and just pull Microsofts source code out of Microsoft.VisualBasic.dll and insert it into your own C# code.





http://forums.asp.net/t/1107932.aspx


http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/81806efe-f565-4444-8b94-f3332d29efc6/



Wednesday, 22 February 2012

How to Search Google smartly

ASP.NET MVC 4 Features




The ASP.NET MVC 4 Beta includes a bunch of great new features and capabilities. Some of the highlights include:

  • Bundling and Minification – ASP.NET MVC 4 includes the new bundling and minification support we are also adding to ASP.NET 4.5. These features enable you to build web applications that load faster and feel more responsive to users, by minimizing the number and size of HTTP requests that your pages make. Included with the MVC 4 beta are new “cache busting” helper methods that enable easy proxy caching of bundled files (with automatic invalidation if you change the cached CSS or JavaScript). You can learn more about bundling and minification from my previous blog post about it.
  • Database Migrations – ASP.NET MVC 4 includes the new Entity Framework 4.3 release, which includes a bunch of great new features. One of the most eagerly anticipated features it provides is database migration support. This enables you to easily evolve your database schema using a code focused migration approach – and do so while preserving the data within your database. I’ll blog more about this in the future - you can also see a walkthrough of database migrations in this tutorial.
  • Web API – ASP.NET MVC 4 includes some fantastic new support for creating “Web APIs”. This enables you to easily create HTTP services and APIs that can be programmatically called from a broad range of clients (ranging from browsers using JavaScript, to native apps on any mobile/client platform). The new Web API support also provides an ideal platform for building RESTful services. I’ll be blogging much more about this support soon – it is really cool, and opens up a bunch of new opportunities.
  • Mobile Web – ASP.NET MVC 4 includes new support for building mobile web applications and mobile web sites, and makes it much easier to build experiences that are optimized for phone and tablet experiences. It includes jQuery Mobile, and includes new support for customizing which view templates are used depending upon what type of device is accessing the app.
  • Razor Enhancements – ASP.NET MVC 4 includes V2 of our Razor View engine. Razor V2 includes a bunch of juicy enhancements that enable you to make your view templates even cleaner and more concise – including better support for resolving URL references and selectively rendering HTML attributes.
  • Async Support and WebSockets – You’ll be able to take advantage of some additional language and runtime capabilities when using ASP.NET MVC 4 with .NET 4.5 and VS 11. Async support is one of the big ones, and the ASP.NET MVC runtime support for this combined with the new C#/VB async language enhancements (which are super elegant and clean) is going to enable you to write incredibly scalable applications. You will also be able to take advantage of the new WebSocket support built-into .NET 4.5 to build applications with even richer browser/server communication.


Sunday, 19 February 2012

JQuery Ajax Call and ASP.net MVC 4.0



Addressing Efficiency lets take a look at a simple comparison between a classic data edit flow and an Ajax driven one. In the classic model that MVC 3 scaffolding template creates, you have the Index page where a list of items are being displayed, in order to edit an item you’ll have to click on edit link, navigate to the dedicated Edit page, save the data and then return to index. Using Firebug I’ve trace the bandwidth usage and it goes like this:
StepOperationBandwidth
1Index page first load140.7 KB
2Click on edit link
3Edit page load133.9 KB
4Save data on the server8 KB
5Return to Index140.7 KB
423.3 K

The same operation performed over Ajax with a modal dialog pop-up instead of navigating to an Edit page:
StepOperationBandwidth
1Index page first load140.7 KB
2Click on edit link
3Edit dialog load12.8 KB
4Save data on the server4 KB
5Close dialog and update Index html4.1 KB
161.6 KB

These figures should change when applying Compression and Omission techniques, but the Ajax powered edit operation is more efficient nevertheless.
Lets see this implemented in my Post to Wall app:
In the controller expose a JsonResult action that will load the note from the database and returns it in a partial view:
TextNotesController.cs
[HttpGet]
public JsonResult EditNote(int id)
{
string message = string.Empty;
var note = new TxtNote();
try
{
note = (from x in db.TxtNotes where x.Id == id select x).FirstOrDefault();
}
catch (Exception ex)
{
message = ex.Message;
}
return Json(new
{
Html = this.RenderPartialView("_NoteEdit", note),
Message = message
}, JsonRequestBehavior.AllowGet);
}
The _NoteEdit partial view makes the html for the dialog form except the buttons:
_NoteEdit.cshtml
@model MvcSp.Models.TxtNote
<div class="edit-box">
<textarea name="edit-content" id="edit-content" class="edit-textarea" rows="1" maxlength="4000">@Model.Titletextarea><br />
div>
All we need now is to write the JQuery code that will call EditNote over Ajax, get the html from the partial view and render it using the JQuery UI dialog modal form:
Index.cshtml
//edit note
$(".edit-note").live("click", function (e) {
e.preventDefault();
showLoader('Editing note');
var noteId = $(this).attr("id").replace('edit-', '');
var liId = '#bar-' + noteId;
//load note from db and open in dialog
$.ajax({
type: "GET",
url: "TextNotes/EditNote",
data: { id: noteId },
cache: false,
dataType: "json",
success: function (mynote) {
$("#edit-popup").html(mynote.Html);
$("#edit-popup").dialog({
resizable: true,
height: 210,
width: 510,
modal: true,
buttons: {
Save: function () {
var boxval = $("#edit-content").val();
if (!boxval) {
showError("Can't save an empty note");
return;
}
$.ajax({
type: "POST",
url: "TextNotes/SaveNote",
data: { id: noteId, content: boxval },
cache: false,
dataType: "json",
success: function (data) {
if (data.Message) {
showError(data.Message);
} else {
$(liId).replaceWith(data.Html);
$(liId).slideDown("slow");
$("#flash").hide();
$("#edit-popup").dialog("close");
}
}
}); //end save call
}, // end ok button
Cancel: function () {
$("#flash").hide();
$(this).dialog("close");
}
},
close: function () {
$("#flash").hide();
} //end buttons
}); //end modal edit
}
}); //end ajax call
}); //end edit
The dialog has two buttons, the Save button will call SaveNote on the server, if everything goes well, we’ll close the modal dialog and update the Index code by using JQuery .replaceWith(), the SaveNote method is another JsonResult action that returns just the html code that has changed:
TextNotesController.cs
[HttpPost]
[ValidateInput(false)]
public JsonResult SaveNote(int id, string content)
{
string message = string.Empty;
var note = new TxtNote();
try
{
note = (from x in db.TxtNotes where x.Id == id select x).FirstOrDefault();
note.Title = content;
db.SaveChanges();
}
catch (Exception ex)
{
message = ex.Message;
}
return Json(new
{
Html = this.RenderPartialView("_NotesList", new List<TxtNote>() { note }),
Message = message
}, JsonRequestBehavior.AllowGet);
}
Before downloading the source code make sure you have VS.NET 2010 SP1, MVC 3 Tool Update and IIS 7.5 Express installed.
download source code files


Extracted From -
http://www.stefanprodan.eu/2011/05/edit-data-in-dialog-form-with-jquery-and-asp-net-mvc/





Wednesday, 15 February 2012

Behavior-driven development framework for testing your JavaScript code



Jasmine is a behavior-driven development framework for testing your JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax so that you can easily write tests.
describe("Jasmine", function() {   it("makes testing JavaScript awesome!", function() {     expect(yourCode).toBeLotsBetter();   }); });
Jasmine can be run anywhere you can execute JavaScript: a static web page, your continuous integration environment, or server-side environments like Node.js .
Find out more in the documentation .


ttp://pivotal.github.com/jasmine/

Tuesday, 7 February 2012

Choosing technologies for .NET project - nice one




Aspect-Oriented Programming



When thinking of an object and its relationship to other objects we often think in terms of inheritance. We define some abstract class; let us use a Dog class as an example. As we identify similar classes but with unique behaviors of their own, we often use inheritance to extend the functionality. For instance, if we identified a Poodle we could say a Poodle Is A Dog, so Poodle inherits Dog. So far so good, but what happens when we define another unique behavior later on that we label as an Obedient Dog? Surely not all Dogs are obedient, so the Dog class cannot contain the obedience behavior. Furthermore, if we were to create an Obedient Dog class that inherited from Dog, then where would aPoodle fit in that hierarchy? A Poodle is A Dog, but a Poodle may or may not be obedient; does Poodle, then, inherit from Dog, or does Poodle inherit from Obedient Dog? Instead, we can look at obedience as an aspect that we apply to any type of Dog that is obedient, as opposed to inappropriately forcing that behavior in the Dog hierarchy.

In software terms, aspect-oriented programming allows us the ability to apply aspects that alter behavior to classes or objects independent of any inheritance hierarchy. We can then apply these aspects either during runtime or compile time. It is easier to demonstrate AOP by example then to describe it. To start, though, it is important to define four key AOP terms I will use repeatedly:

  • Joinpoint—Well defined points in code that can be identified.
  • Pointcut—A way of specifying a Joinpoint by some means of configuration or code.
  • Advice—A way of expressing a cross cutting action that needs to occur.
  • Mixin—An instance of a class to be mixed in with the target instance of a class to introduce new behavior.

To better understand these terms, think of a joinpoint as a defined point in program flow. A good example of a joinpoint is the following: when code invokes a method, that point at which that invocation occurs is considered the joinpoint. The pointcut allows us to specify or define the joinpoints that we wish to intercept in our program flow. A Pointcut also contains an advice that is to occur when the joinpoint is reached. So if we define a Pointcut on a particular method being invoked, when the invocation occurs or the joinpoint is invoked, it is intercepted by the AOP framework and the pointcut's advice is executed. An advice can be several things, but you should most commonly think of it as another method to invoke. So when we invoke a method with a pointcut, our advice to execute would be another method to invoke. This advice or method to invoke could be on the object whose method was intercepted or on another object that we mixed in. We will explain mixins in further detail later.



Extracted From - http://msdn.microsoft.com/en-us/library/aa288717(v=vs.71).aspx

Microsoft Unveils Microsoft Dynamics CRM Mobile

Microsoft Corp. (Nasdaq “MSFT”) today announced that Microsoft Dynamics CRM will release its next service update in Q2 2012. This service update will deliver the capability for customers to access the complete functionality of Microsoft Dynamics CRM on virtually any device with a new cloud-based, cross-platform, native mobile client service for Windows Phone 7, iPad, iPhone, Android and BlackBerry mobile devices.

“In today’s hyperconnected world, customers need to be able to access their business-critical data on the device of their choice from wherever they are,” said Dennis Michalis, general manager, Microsoft Dynamics CRM. “These advancements, combined with the strength of the Microsoft platform, make Microsoft Dynamics CRM an obvious choice for any business.”

Maintaining Microsoft’s commitment to delivering rapid innovation to customers, the Microsoft Dynamics CRM Q2 2012 service update also includes enhanced social functionality and new enterprise-class features, and adds multiple Web browser options for users, including Internet Explorer, Chrome, Firefox and Safari, running on PC, Macintosh and iPad devices.

Delivering CRM on Virtually Any Mobile Device

Microsoft Dynamics CRM Mobile will deliver customers a fast and familiar experience, helping sales, customer service, marketing and management professionals connect with their customers and each other on multiple mobile devices through a security-enhanced environment. For instance, sales and marketing representatives will now be able to quickly capture and convert leads 24/7 and develop marketing campaigns on the go.

Using the cloud-based, cross-platform, native mobile client service, customers will be able to sync their information and sales pipelines offline, helping them stay connected on the road and on their terms.

Through the power of xRM, the flexible application development framework of Microsoft Dynamics CRM, customers will also be able to easily mobilize their extended CRM applications by having seamless integration between Microsoft Dynamics CRM Mobile and their existing Microsoft Dynamics CRM environment.

Microsoft Dynamics CRM Mobile will be available at a starting price of $30 (U.S.) per user, per month and supports the use of up to three devices per user.

Additional Social Enhancements

Building on the Microsoft Dynamics CRM November 2011 service update, the new release will include more social capabilities with Activity Feeds that give people the ability to like and unlike status updates, improved status filtering, and the capability to view all statuses relating to a particular record view, which will make it even easier for users to stay on top of all relevant customer information to help them be more productive.

Complete information on the Microsoft Dynamics CRM Q2 2012 service update is available in theRelease Preview Guide and a blog post from Michalis on CRM Connection. Those who want to follow and engage with the Microsoft Dynamics CRM Twitter community can do so at @MSDynamicsCRM, using #MSDYNCRM and #crm2011.

About Microsoft Dynamics

Microsoft Dynamics CRM and ERP solutions empower your people to be more productive and your systems to last longer and scale as your business grows, while enabling you to derive the insights necessary to respond quickly in an ever-changing world of business.

About Microsoft

Founded in 1975, Microsoft (Nasdaq “MSFT”) is the worldwide leader in software, services and solutions that help people and businesses realize their full potential.

Note to editors: For more information, news and perspectives from Microsoft, please visit the Microsoft News Center at http://www.microsoft.com/news. Web links, telephone numbers and titles were correct at time of publication, but may have changed. For additional assistance, journalists and analysts may contact Microsoft’s Rapid Response Team or other appropriate contacts listed athttp://www.microsoft.com/news/contactpr.mspx.