Archive for September, 2008

JQuery and Microsoft…Big News!

September 28th, 2008

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

Scott Hanselman at Microsoft: http://www.hanselman.com/blog/jQueryToShipWithASPNETMVCAndVisualStudio.aspx

John Resig from jQuery: ( http://jquery.com/blog/2008/09/28/jquery-microsoft-nokia/ )

Some snippets from ScottGu:

I’m excited today to announce that Microsoft will be shipping jQuery with Visual Studio going forward

 

Enough said.  I’ve been in love with jQuery since the first day I used it, been using it since.  Glad to see Microsoft making the move.    It’s been a powerful tool with asp.net mvc so far.  Very intuitive, easy to extend, etc…

The new ASP.NET MVC download will also distribute it, and add the jQuery library by default to all new projects.

 

We are really excited to be able to partner with the jQuery team on this.  jQuery is a fantastic library, and something we think can really benefit ASP.NET and ASP.NET AJAX developers.  We are looking forward to having it work great with Visual Studio and ASP.NET, and to help bring it to an even larger set of developers.

For more details on today’s announcement, please check out John Resig’s post on the jQuery team blog.  Scott Hanselman is also about to post a nice tutorial that shows off integrating jQuery with ASP.NET AJAX (including the new client templating engine) as well as ADO.NET Data Services (which shipped in .NET 3.5 SP1 and was previously code-named “Astoria”).

 

I do think this library is superior to what MS has used, and I was also curious why they did their own.  I figured it was because they couldn’t use Open Source.  So, I guess they will/can.  Good to see. Now… I think they’d be wise to refactor their own libraries to use jQuery/add on to jQuery (plugins, etc…)

I’ve personally built a few Html Ajax helpers, form validation helpers, etc.. for my asp.net mvc projects.  Plugins are easy to create and there is a tons of plugins available as well.

Great news!

ASP.NET MVC and Codebehind Files

September 23rd, 2008

http://www.infoq.com/news/2008/09/aspnet-mvc-codebehind

I’ll throw my 2 cents in here:  I will agree that by default this shouldn’t be needed.  I’d rather add the ViewData model type to the aspx page itself.

What do I mean?

public partial class Employee : ViewUserControl<Core.Domain.Employee>
 

Quite honestly I think that is about all I use the code behind files for in the views.   There were some cases I would handle the page load event and bind to a Repeater, but those were typically not the approach I would use (I opted for a  HtmlHelper, ie. Html.Grid or Html.Repeater instead).

So, I like the idea of defining the ViewData<T> type in the actually aspx and then not require this additional code behind file.

Personally, I’d keep both available, making Tim Barcz’s technique the default

NHibernate MultiCriteria

September 22nd, 2008

Today I ran across an interesting problem – I needed to query to get counts after a process runs.  Obviously I can make two database calls, but I instead wanted to take advantage of NHibernate’s ‘MultiCriteria’ capability.  This will make one call to the database with the 2 queries vs. 2 database calls.  My example:

 

IMultiCriteria crit = NHibernateSession.Current.CreateMultiCriteria()
                .Add(CreateCriteria<CompanyDeposit>()
                    .Add(Restrictions.Between("ProcessedDate", lo, hi))
                    .SetProjection(Projections.RowCount()))
                .Add(CreateCriteria<EmployeeDeposit>()
                    .Add(Restrictions.Between("ProcessedDate", lo, hi))
                    .SetProjection(Projections.RowCount()));
            IList results = crit.List();
            var companies = (IList)results[0];
            var employees = (IList) results[1];

            var companyCount = (int)companies[0];
            var employeeCount = (int) employees[0];

 

 

One queries CompanyDeposits and gets the rowcount, the other queries EmployeeDeposits and gets it’s rowcount.

Slick…

For reference, check out Ayende’s post here as well 

(and thanks to Stefan for asking me about it, which made me want to go try it out… lol)

Cohesion & Coupling

September 22nd, 2008

Jeremy Miller’s MSDN article on Cohesion And Coupling.  Jeremy covers some of the OO ‘basics’ as part of his MSDN series ‘Patterns in Practice’ (see ‘The Open Closed Principle‘ article as well),  including

Decrease Coupling
Increase Cohesion
Eliminate Inappropriate Intimacy
The Law of Demeter
Tell, Don’t Ask
Say It Once and Only Once
Wrapping Up

As always, Jeremy does a good job explaining these software design fundamentals.

Disappearance of DVD drive in Vista

September 21st, 2008

So, I installed the first part of SQL 2008 – and after the machine rebooted, my DVD drive was gone!  There is a hotfix that SQL 2008 installs first, so it makes me wonder if that is the culprit?

After a few searches, I found this KB 929461 

After removing the two values in that article and rebooting… my drive is back…whew

Great News – RSS Reader

September 20th, 2008

I’ve been looking for a good RSS reader (outside of browser plug-ins) – and so far I’m impressed with ‘Great News’ RSS reader

Quick, easy to organize, and obviously it imports my rss feeds without any problems

Architecture Layering Part II

September 10th, 2008

Back awhile ago I made this post on Architecture layering.

Having a service layer in a ASP.NET MVC is a great thing!

Initially I was wrapping Actions with transactional attributes, making calls to the Dao objects, etc… Sure, the dao objects are being injected by my IoC container (Windsor) – which by the way, is very sweet, it’s so transparent that I forget who it creating the objects… I just use the interface… (that is another story)

I show a simple example below, but this sample doesn’t capture where I’ve been able to move business logic back into services – simplifying the controllers.   (Also, it cuts back on any duplication)

Simple example:

MyCustomerController

private readonly ICustomerDao CustomerDao;

ctor : MyCustomerController(ICustomerDao CustomerDao){ this.CustomerDao = CustomerDao;}  //injected

Then on a save:

[Transaction]
public ActionResult Save(string id)
{
     …  CustomerDao.Save(Customer);
}

Instead I’ve moved this into a service layer – now the controllers know nothing about the data access layer:

public class CustomerService : ICustomerService
{
    private readonly ICustomerDao CustomerDao;
    public CustomerService(ICustomerDao CustomerDao){ this.CustomerDao = CustomerDao;}  //injected

    public void SaveCustomer(Customer customer)
    {
         using(ITransaction tran = NHibernateSession.Current.BeginTransaction())
         {
                try….
                CustomerDao.Save(Customer);
                tran.Commit();
                …

         }
    }
}

That is our service – it handles all transactions – where needed – in conjunction with the Dao objects

Now the controller gets an injected CustomerService rather than a CustomerDao.  Notice the Transaction attribute is no longer needed.

MyCustomerController

private readonly ICustomerService CustomerService;

ctor : MyCustomerController(ICustomerService CustomerService){ this.CustomerService= CustomerService;}  //injected

Then on a save:

public ActionResult Save(string id)
{
     …  CustomerService.SaveCustomer(Customer);
}

————-

As far as the IoC goes, I’m doing that all within the Global.cs

ie.

ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(Container));

_container.RegisterControllers(Assembly.Load(“Controllers”));

Then I use reflection to add the Dao’s and the Services’s to the container.  It uses Windsors ‘autowiring’ to handle creation based for dependencies.

NHibernate – AutoGenerated Identity on a Non-Key Field

September 10th, 2008

We have a requirement to store an additional auto-incremented id to support a legacy app upgrade.  Our primary key is a GUID – the old system used ints, and the client still wants to have that int id – as they use it, etc…

So, within NHibernate we were able to add the following to the property:

property name="GroupId" type="int" generated="always" insert="false" />
 
Thats it.
 
 

Multiple ‘Simultaneous’ Ajax Calls with jQuery

September 5th, 2008

One major problem with ajax is that when sending two ajax calls – there is no guarantee which call will return first – resulting in data being ‘mixed’ up.

The first time I saw this it was very confusing!

ie.

Let’s say I have two parts of a page that need to be updated –

function UpdatePage(){
     UpdatePart1();
     UpdatePart2();
}

 

function UpdatePart1(){
     $.ajax(beforeSend…, success…);
}

function UpdatePart2(){
     $.ajax(beforeSend…, success…);
}

What can occur in this situation is that the UpdatePart2 can return before the UpdatePart1 – and the ‘success’ from UpdatePart2 will being getting the ‘success’ from UpdatePart1  -definitely not good!!!

So…one way, is to only call UpdatePart2 on the success of UpdatePart1…but… not really as efficient of course.

What to do?

UPDATE:  Danger Will Robinson!  Ok, this actually works fine in Google Chrome and Firefox, but dreadfully it caused issues in IE.

So… what to do?

Check out this one instead:

http://dev.jquery.com/view/trunk/plugins/ajaxQueue/jquery.ajaxQueue.js

‘ajaxQueue’

Now, the usage is off a tad in that link, that usage is from an earlier version.  The code listed there extends the $.ajax call, which is nice, by adding in one additional setting called ‘mode’

ie.

$.ajax({
     mode: ‘queue’,
     url: “test.php”,
      success: function(html){ jQuery(“ul”).append(html); }
});

modes available are ‘queue’, ‘sync’, and ‘abort’.   For the record I’ve only used ‘queue’ and it worked great on all 3 browsers (IE 7, Chrome, FF 3)

In my code I had to loop through a list, sending out a request for each item in succession (printer queue).  This worked well for my case, ensuring no overlapping or wierd results (and boy, in IE it was all over the place with the commented out code below…)

Well,  the “AJAX Queue/Synch/Abort/Block Manager” comes to the rescue  :)

Helps you to manage AJAX requests and responses (i.e. abort requests, block requests, order responses). It is inspired by the AJAX Queue Plugin and the AjaxQueue document in the jQuery-Wiki.

This is very easy to implement and have some great features as well.

Let’s take our above sample and rewrite it using the AjaxManager:

function UpdatePage(){
    var ajaxManager1 = $.manageAjax({manageType: ‘normal’, maxReq: 0, blockSameRequest:true});
    ajaxManager1.add({
         beforeSend…success…url, etc…
    });

   ajaxManager1.add({
         beforeSend…success…url, etc…
    });
}

Easy as I said to implement – I’m taking the two calls from above, adding each .ajax call to the AjaxManager.

What really shines here are the options available: (From the link above)

  • normal jQuery-Ajax-Options
  • manageType: (string) the queue-type specifies the queue-behaviour (default: ‘normal’):
    • ‘sync’: synchronizes the incomming responses (callbacks) in the same order the requests were made
    • ‘queue’: queues your AJAX-requests (you have to set the maxReq-Option)
    • ‘abortOld’: aborts all “older” requests, if there is a response of a newer request
    • ‘normal’: normal behvaiour
  • maxReq: (number) limits the number of simultaneous request in the queue. If you don´t use ‘queue’ as your manageType, then older requests will be aborted. (default: 0 = unlimited requests)
  • blockSameRequest: (boolean) prevents same request, compares url, data and type in the same queue to determine, wether the same request is already in process. (default: false)

 

So in my sample, I’m using the normal behavior, with unlimited requests, however, I do set the blockSameRequest to ‘true’ – obviously you can configure this to meet your needs.

And…. it’s free and built with the wonderful jQuery library  :)

Silverlight and Google Chrome

September 4th, 2008

Interesting response from a Silverlight MSFT on their forum regarding Chrome:

Hello, currently we don’t have plans to support Chrome. We will support it in the future if it gains enough market share.

 

So, this is interesting, I thought Silverlight’s whole concept was to be able to run on the different browsers ? 

To completely ignore Chrome will be a bad idea.  It’s one thing to say ‘we are working to add support for Chrome vs. we don’t have plans to support Chrome’.

Flash works…