Nermin's .Net

My Thoughts on .Net and Software Development - correct spelling is optional

About the author

Author Name is someone.
E-mail me Send mail

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Bye Bye MS AJAX, Welcome JQuery

Finally the news that some of us in the ASP.NET MVC World have been secretly hoping for, has come true. Microsoft will ship JQuery as a part of ASP.NET inside Visual Studio - IntelliSense and all!  Not just that - it is to be the basis of any of their new Ajax UI widgets and elements they design in ASP.NET.

But why trust me, check from the source himself - Scott Guthrie:

http://weblogs.asp.net/scottgu/archive/2008/09/28/jquery-and-microsoft.aspx

For some of you that might have dropped out if this planet for the past year or two,  JQuery is a very powerful, yet simple JavaScript/Ajax framework that allows for a effective manipulation of Html elements without much of the script.  For details please check out:

http://www.jquery.com

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: MVC | ASP.NET
Posted by ndibek on Sunday, September 28, 2008 12:56 PM
Permalink | Comments (966) | Post RSSRSS comment feed

An interesting migration problem from "Linq to Sql" to "Linq to Entities"

As you might have seen  from few of my previous posts I have been working on the MVC demo implementing Csla.  DAL layer of that demo used the Repository pattern, which is essentially just a wrapper around Linq2Sql DataContext:   

 
Then we would have one implementation of IRepository for each Linq to Sql table / DTO object, like for example SqlCustomerRepository.GetAll(), SqlOrdersRepository.GetAll(), etc.
 
All that each of the GetAll() methods would do is return (this is the implementation in SqlCustomerRepository):
 
 
where _db is an instance of the Linq DataContext.  Hiding DataContext away from our BO and abstracting it away from business layer objects makes it easier to test our business objects.  It makes it easy to replace the SqlCustomerRepository, with TestCustomerRepository that might return hard-coded list of Customers instead.
 
Then we use this inside our business object applying necessary filters - all that the method returns is IQueryable<T> so it makes it easy to use result of GetAll() in another Linq query:
  
 
One problem Rocky noticed in this design is that we are returning Linq generated DTOs.  What if our application needs to migrate to Oracle as a back end, for example.  The only option then is Linq to Entities.  But the type generated by Linq to Entities is different than type generated by Linq to Sql.  Although they might have similar signatures they are different objects.
 
One solution would be t create generic Data Transfer Objects that are neither  Linq to Sql neither Linq to Entities and then "map" the either implementations to these "shared" objects that are instead sent as DTOs.  Problem - one would have to hand code all that stuff.  
 
So for now I decided to give up on Linq to Sql in my demo and migrate it to Linq to entities.  Having 2 different database back ends would not matter in Entity Framework as either Sql Server or Oracle tables are mapped into Entities.  But that meant migrating to Linq to Entities.
 
I thought that this will present not much work at all as the queries are rather simple, and both frameworks support the same Linq syntax.  So I regenerated my Entity diagram using EF.  Everything compiled - good!.  
 
But when I ran the application - I got the following error:
 
Only parameterless constructors and initializers are supported by LINQ to Entities.
 
So although it compiled as perfectly valid Linq Query, at runtime it broke due to the fact that Linq to Entities can not evaluate expression if there are constructors with parameters, or initializers that are not an anonymous objects.  Took me a while to wrapmy head around that statement, but that essentially is what EF team states.  You can check it out in greater detail on following two sites:
 
Essentially Linq2Entities was complaining about:
 
select new OrderInfo(order)
 
part.   OrderInfo is the BO that is part of the list I am trying to populate from EF Entity - order, which is passed as a parameter to its constructor.  So essentially I had to modify the Fetch() method to following:
 
 
 
So what changed?  We are selecting the "order", which is the Linq to Entities object, and then we have to iterate through the list of those objects to map one at the time (as can be seen from use of Add() method instead of AddRange() which was used in Linq to Sql version).
 
Yet another "feature" that any of us migrating from Linq to Sql to Linq to Entities should be aware of.
  
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: C# | Linq
Posted by ndibek on Monday, September 15, 2008 7:09 AM
Permalink | Comments (2315) | Post RSSRSS comment feed

Implementing MVC Site using Csla and ExtJs - Part 3 - UI, ExtJs Customer DropDown List

This is the third part in series "Implementing MVC Site using Csla and ExtJs".  The first two parts can be found here:

Part 1 - Introduction 

Part 2 - Solution Structure 

 

In the previous 2 posts I mentioned decisions behind some of the technologies used in the demo as well as the file structure.

 If you remember I stated that url reguest like:

http://server/<Controler>/<Action>/<Parameter(s)>

is mapped into Controller.Action(Parameters...) call.

So lets take a look at the  our example - what happens when we click on Customer link:

As you can see the url follows the pattern we mentioned above.  So with the assumption from above we would presume that the CustomerController.List() method would be called.  Lets take a source code of that Action/Method to se how do we get to the view:

Where is the beef? List method seems to create some SelectList object, and then passes it to the method called View as a parameter.  Hmmm...  OK, method View probably results in View called List being rendered, and we are passing it SelectList for perhaps databinding...

Where is CSLA code?  Well, customerFactory is an instance of ReadOnlyListFactory class.  RetreiveAll() returns CustomerList instance which is ReadOnlyBase implemenation.  But why not use  CustomerList.RetreiveAll() - factory method?  Instead of using Factory Method design patern, we are using Abstract Factory, moving Create(), and Fetch() outside of our Csla BOs, for the purpose of easier Unit Testing.  I will explain the details of this in one of the future posts that has to explain the whole process of testing all of the app layers in Csla - which will probably be the best part of this demo.

For now, all we need to know is that there is AbstracBusinesstFactory class that defines following: 

 
and then the ReadOnlyListFactory.RetreiveAll() looks like this: 
 
and cutsomerFactory is instantiated with, in controller's constructor with:
 
var customerFactory = new ReadOnlyListFactory<CustomerList,CustomerInfo>();
 
Again all we care for now is that customerFactory.RetrieveAll() serves same purpose as customerList.RetriieveAll().
 
What does then SelectList object do?  It is used by Html.DropDownList() helper method of the MVC frameowrk (there are number of others) that renders html  <select>element for us.  All that SelectList does is it takes the IList implementation, like CustomerList, the other two parameters being Key Value (CustomerID)and  Display Value (CustomerName), for SelectList.
 
Lets now take a look at the Html source of the List.aspx: 
 
This actually is all of the Html that is in the List.aspx view, by the way.  I will explain later how the Order and OrderDetials grid "Magically" appear on it.
 
  

But wait, when I run the sample site the drop down I see has a different style from standard windows drop-down.  In addition to that we can see that the drop down has a type-ahead, kind of like Intelisense...

How come?  When I look at the html source very simple <select> element is rendered... 

Simply put this is where ExtJs comes in.  We have used ExtJs DropDown control, that simply can "inherit" from the Html element/instance, and extend it to implement some new functionality, like in this case.  What complicated JS did we have to write to do this, you might ask?
 
Well here it goes, this is all that is needed:
 
It appears that the script creates an Instance of Ext.form.ComboBox() class (I know, I know some of you might say: "But wait JS does not support classes".  Well ExtJs does, and so does JQuery, and Prototype - 3 most popular Ajax libraries on the planet). And it passes parameters, like typeAhead: true, and transform:'customerList'.  TypeAhead is obvious, but transform parameter is interesting.  It tells ExtJs.for.ComboBox class which html <select> element it needs to enhance/transform.  And that is all.  Oh, by the way there is an extra call that registers JS function refreshOrderGrid() to listen to "select" event on our drop-down, after the constructor.  That is the extCombo.addListener() call.
 
But Nermin, we should not use JavaScript, although that chunk of code looks cool.  Well then neither should folloing companies, all of which have sites built on top of ExtJs:
 
Adobe, Aetna, AIG, Alcatel-Lucent, Amazon.com, Best Buy, Boeing, Borland, CA, Canon, Capgemini, Cisco, CNN, Dow Jones & Co., EMC, Fidelity, General Electric, Hallmark, HP, HSBC, IBM, Mott MacDonald, NATO, NetApp, Nortel, Northrop Grumman, Panasonic, Pixar Animation Studios, Qualcomm, Inc., S&P, SAP, Siemens, Sony, Symantec
 
The difference between the JS that used to get us burned in the old ASP days and ExtJs is that ExtJs is extremely simple, it is used strictly for UI rendering - no business logic in JS, and allows for all of the modern language constructs in JS.  Building extension methods, overloads, custom expressions is easier in ExtJs than in C#.  And would you rather have unreadable and unmaintainable Java Script that some of the Web Forms control generate (that by the way is not compatible with all of the browsers, and can't be touched - it is generated), or would you use this easily extensible JavaScript  library directly.  Unlike Web Forms generated script this is easily manipulated for style, adding new features etc.  One can inherit from Ext.form.ComboBox class create its own extenions override existing methods behaviors.  Limit is only your cretivity.
 
This will just get more interesting in the next post when we discuss the "magically" appearing Order, and Order Detail grids. Grids that support everything that ASP.NET Web Forms GridView do and lot more.  And it is my humble hope that the article will prove to you that this results in by far simpler and easier to maintain code, that is easily testable, and more stable.
 
Source for this demo can be found at:

ExtJsDemo.zip (2.58 mb)

 
 
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,
Categories: ASP.NET | Csla.Net | MVC
Posted by ndibek on Wednesday, September 10, 2008 9:32 AM
Permalink | Comments (923) | Post RSSRSS comment feed

Implementing MVC Site using Csla and ExtJs - Part 2 - Solution Structure

This is the second article in "Implementing MVC Site using Csla and ExtJs".  There will be few more!  First one can be found at:

Part 1 - Introduction 

Unfortunately I have to get through this part and UI before I starttalking about my favorite parts TDD using MVC, Unit Testing in Csla,Design of the DAL in this example to support testability, and newfeatures of Csla 3.6 that support Unit Testing, like ObjectFactory. This part being UI, and how Csla BOs get rendered in MVC app that usesExtJs Ajax library.  Fortunate part about that though is that I hopemany of you will find the UI part to be something different and cool! 

So lets take a look at the ExtJsMvc solution (image on the left).  It includes 3 projects:

- ExtJsCslaDemo.Web (web site)

-  ExtJsCslaDemo.Web.Test (Unit tests library built on top of xUnit.net and RhinoMocks)

- Northwind.Data (Linq DAL + few classes that make testing a DAL breeze, on all on top of Northwind sample DB - attached in App_Datafolder)

 

Key to any Web MVC project are the following 3 folders:

- Controllers (controller classes are in this folder)

- Views (This folder contains all the Views/Pages to be rendered inthis application.  Views are grouped in Subfolders that hold the samename as a controller that is in charge of that view.  SO for example,you have a Customer subfolder of the Views folder that holds veiws thatthe CustomerController controls - a single veiw called List.aspx).

- Models (This is the place for your BOs that the Views will render- in this example, this is where I placed the Csla classes, butobviously for any solution that ).

 

As far as controllers our focus is going to be strictly onCustomerController, and OrderController.  HomeController is just thedefault one that gets created by the MVC project wizard, andAccountController is the one that takes care of security (login,changing passwords, etc).

 When it comes to views just notice that Customer Controller has oneview (List.aspx), while OrderController has no views in the Views/Orderfolder.  I will explain that part later in the UI section. 

 Source for this demo can be found at:

ExtJsDemo.zip (2.58 mb) 

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,
Categories: Csla.Net | MVC | ASP.NET
Posted by ndibek on Wednesday, September 10, 2008 6:34 AM
Permalink | Comments (1510) | Post RSSRSS comment feed

An Issue with [HandleError] attribute in MVC and JQuery Ajax Load()

In CTP release 4 of MVC frameowrk for Asp.Net we have seen an introduction of [HandleError] attribute that can be assigned to a Controller class or individual Action methods.  It works great.  It captures any unhandled exceptions thrown by your code, and redirects to the default error Handling page/view Error.aspx that displays the exception detail.

The behavior can be fine-tuned further to have different pages for different exception types, or different Action methods.  But I have found one scenario where this attribute does not work, but could easiuly be addressed.

 Lets imagine a scenario where lets say you are builing a wizard.  Your primary view gets loaded as a container for wizard pages, and then you want each of your actual pages to be loaded inside a panel on your wizard.  Obviously you want to avoid post-backs, and you want to load those wizard pages as Ahax loaded partials.

In other words, Ajax call invokes a call on your controller that renders the Html for your page, and then your Ajax/JScript libary inserts that HTML at pre-destined place on your page.  Simple.

 Except that for this case, when error hapens inside the Controller that reneders my partial page I get nothing - no expected page and no error page either.  So what is the problem? Take a look at the source code of the HandleErrorAttribute.OnException() method (I used Reflector to get to it):

 

 
As you can see in the last 2 lines we clear the existing Response stream and return a StatusCode of 500.  Ajax libraries, like the one I used - JQuery, assume that the 500 is "Internal Server Error", and do not load/get the actual Html.
 
Solution to this problem:  I built my own HandleErrorAjaxPartial that inherits from HandleError and overrides the OnException() method, calling the base.OnException() and resetting StatusCode to OK (instead of 500).  Then all of the Controller actions that render wizard pages and are called from my Ajax code got marked with this new attribute, and now everything is fine.
 
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,
Categories: MVC | ASP.NET
Posted by ndibek on Wednesday, September 10, 2008 4:59 AM
Permalink | Comments (1488) | Post RSSRSS comment feed