This theme is downloaded from wordpress themes website.

Archive for the '.net' Category

Fluent NHibernate’s AutoPersistenceModel: I Love it!

I checked out the latest version of Fluent NHibernate today and noticed the AutoPersistenceModel. I looked at the unit-tests to see what it’s supposed to do and I loved what I saw…

Let’s say you have a 2 simple classes like this:

image

Now, in the global.asax’s init (assuming a web application), we can configure NHibernate using Fluent NHibernate like this:

image

It uses convention over configuration in lots of places, eg the Id property is always used as identity-column if a property with that name exists.
But you could also easily add your own conventions. If you want to have the Id colums to be ArtistId and AlbumId (and any other className + Id), you’d do:

image

Want to restrict the namespace as well? No problem!

image

Fantastic! Now, to see what mapping file this code automagically creates for our Artist class, we can run the following code:

image

which returns an XmlDocument. In this case, the InnerXml of that document looks like this:

image

Again: fantastic! See how it even generates the one-to-many relation with the correct end-class, because we defined a property IList<Album>!

Running the same code for the Album class gives:

image

If you want to use Fluent NHibernate the ‘old’ way, you can still add ClassMap<Artist> to your assembly. Hence, you can even mix it with the AutoPersistenceModel! If you have a classmap file, it’ll use that for your class, if it isn’t there it’ll automatically create a mapping for you!

I’m sold! This makes using NHibernate a breeze. You can get up and running with your NHibernate project so much faster now.



kick it on DotNetKicks.com

.net & c# Chris van de Steeg 02 Dec 2008 11 Comments

Howto use jQuery with asp.net mvc preview 4’s AjaxHelper

Working on jQuery4mvc preview 4, one of the first things I ofcourse wanted to do was to replace the ASP.NET Ajax library that’s used by the AjaxHelper.

Unfortunately the AjaxHelper itself is sealed, so atm we can’t (easily) change how the AjaxHelper generates its links. So, as an alternative, I created a javascript library that uses the same methodnames as the MicrosoftMvcAjax version.

Though at first I was upset that AjaxHelper wasn’t opened up, this javascript-only solution makes it very easy to use. All you have to do is

  • place the latest jQuery version in your Content folder;
  • place my javascript file in your Content folder;
  • add references in your views (or masterpage ofcourse) to these 2 files instead of referencing the MicrosoftMvcAjax.js and MicrosoftAjax.js file.

If you haven’t played with the AjaxHelper yet, take a look at Scott Hanselman’s post on Using Ajax and Ajax.Form

This jQuery version also has a little extra: browser-history support for your ajax requests

Now, go download the js file and play!



kick it on DotNetKicks.com

.net & Ajax & asp.net mvc & c# & javascript Chris van de Steeg 21 Jul 2008 6 Comments

jQuery for Asp.net MVC preview 3

So, I finally got a chance to sit down and finish up the jQuery for Asp.net Mvc preview 3. If you haven’t already read Scott’s post about preview 3,  you should definitely do so before reading this one.

So, quite some things have changed in preview 3, which made me decide to change a lot in the jQuery for Asp.net mvc (jqmvc) too. To start with most radical one: I ditched the ajax.master. This little ‘framework’ does not rely on a special masterpage in your website from now on. Since things have radically changed, I’ll start all over again on how to use this project.

In this post, I’ll explain how the project works globally. I won’t go into too much details about everything, but I will just explain the things you’ll directly touch in your application. In the next post, I’ll explain how to use the project in your website (until that time you could take a look at the included sample website). That post will explain the Ajax- and HtmlHelpers in this project and it will explain how to do an ajax request using jQuery.

Before my story starts, let’s start with the links and downloads for all those who want to read code rather than stories :

Application and routes

To have a quick go for your application, it’s recommended that you inherit your application from JqueryMvc.JqueryMvcApplication. You can ofcourse easily do this in your global.asax like so:
image
This application sets up the Unity dependency container and will set up the default routes. If you wish to setup a different (or no) dependency container, you should override InitializeContainer() and ConfigureContainer(). JQueryMvc itself is no longer dependent on a dependency container.

I found it hard to set up the default routes in preview 3 if your server does not support wildcard redirects (like visual studio’s webdevserver doesn’t). In that case you need to catch existing files (default.aspx needs to exist, but shouldn’t do anything), but you need to skip the Content folder. I came up with the following solution:
image
So if there’s a default.aspx in the root of your application, it set’s routeExistingFiles to true. If the url matches the RegEx ^Content\/.* the routing will be stopped using the new IgnoreRoute method. Also, your controllers will then be extended with .aspx to be sure the requests passes the asp.net cycle. If you have the option to catch all incoming requests, all you have to do is remove the default.aspx from the root of your website.

Controllers

As you’ve seen in Scott’s post, controllers should now return an ActionResult. The normal way would be to have your controller action return the result of the View() method. With jqmvc you should not call the View() method at the end of your action. Ofcourse, if you’re sure you want to return the normal view, you could still do so. In jqmvc there is a ExtController which you should inherit your controllers from. This baseclass contains a method called Action() which accepts the same attributes as View() but does some magic to decide what type of request the current request is, and will try to return the correct ActionResult for that type of request. This could be either an Ajax-request, a Json request or a normal page-request. Your typical controller with some actions would look like this:

   image

Views

If jqmvc encountered the current request as a normal page request, it will create the default ViewResult, nothing special there.

If jqmvc decided Json should be returned, a JsonResult is created and no further actions will be taken. The rendered view will be the ViewData serialized to Json. The ViewEngine of the controller is also changed to a JsonViewEngine but this is only called upon in case of an Exception.

If jqmvc decided that the request is an Ajax request, it will create a normal ViewResult, but it will change the ViewLocator of the controller.
(Offtopic: I think it’s a design-error to have the viewlocator on the controller. It should be on the ViewResult object, that’s the only object that uses it. Perhaps this will be changed in the next preview…)
This AjaxViewLocator will first look for .ascx files instead of .aspx files. If it can’t find the .ascx it will load .aspx afterall.  You can also create a target-specific .ascx file: if you do an ajax-request through our javascript function jQuery.mvc.request() (or one of the htmlhelper functions), you have to specify a target for the loaded content. Let’s say we want to load /Home/Index into a <div id="dynamiccontent"></div>
The AjaxViewLocator will then look for :

  1. /Views/Home/Index.dynamiccontent.ascx
  2. /Views/Home/Index.acx
  3. /Views/Home/dynamiccontent.ascx
  4. /Views/Home/Index.aspx
  5. /Views/Shared/Index.dynamiccontent.ascx
  6. /Views/Shared/Index.ascx
  7. /Views/Shared/dynamiccontent.ascx
  8. /Views/Shared/Index.aspx

To have the page /Home/index available both as an ajax request as well as normal request, you could create an /Views/Home/Index.aspx which loads the index.ascx like so:image

Things to know

  • We have some HelperClasses to simplify the use of jqmvc

  • The ViewData keys "Messages" and "Errors" are treated specially by jqmvc.

  • Your page should always contain:

    • <%= Html.RegisterJqueryMvc(Page.ClientScript) %> To register the required javascript files

    • <div id="loading"></div>; or something else with id="loading". This will be made visible during ajax-requests

    • <div id="errors"></div> to display errors

    • <div id="messages"></div> to display messages (informational)

  • We have a special contentplaceholder in JqueryMvc.UI.WebControls.ContentPlaceHolder. This will use the AjaxViewLocator to load some default content. Eg, if used like this in your masterpage, and the current request is /Home/Index:
    <jq:ContentPlaceHolder ID="maincontent" runat="server" />
    It will look for for content in:

    1. /Views/Home/Index.maincontent.ascx
    2. /Views/Home/Index.acx
    3. /Views/Home/maincontent.ascx
    4. /Views/Home/Index.aspx
    5. /Views/Shared/Index.maincontent.ascx
    6. /Views/Shared/Index.ascx
    7. /Views/Shared/maincontent.ascx
    8. /Views/Shared/Index.aspx

      This way, your index.aspx could just be an empty page that points to MasterPage with this default-content-loading contentplaceholer.image

  • Your ascx files should inherit from JqueryMvc.Mvc.ExtViewUserControl to have access to the AjaxHelper (bug in asp.net mvc!) and to have the messages & errors parsed by jqmvc.
  • In case of an error the default controller will look for a rescue page in /Views/[ControllerName]/Rescues/error.aspx or /Views/Shared/Rescues/error.aspx (or .ascx in case of an ajax request). You can specify your own rescues for different types of errors by using the RescueAttribute of
  • You should remove default.aspx if your application-server supports wildcard mappings.


kick it on DotNetKicks.com

.net & Ajax & asp.net mvc & c# Chris van de Steeg 13 Jun 2008 10 Comments

Next Page »

Recommended: Buy movies online.