Blog of Developer Mikkel Ovesen
This blog is actually meant for me only... I will use it mainly to help me remeber code snippets, usefull add-ins and links and other information that I regards as important in my work as a developer

Ayende explains cascading with NHibernate

December 18, 2008 21:12 by mikkel@ovesens.com

To continue my little "post with good links"... here is a good article by Ayende, explaining cascadin with NHibernate:

http://ayende.com/Blog/archive/2006/12/02/NHibernateCascadesTheDifferentBetweenAllAlldeleteorphansAndSaveupdate.aspx

Here is the basics:

Here is what each cascade option means:

  • none - do not do any cascades, let the users handles them by themselves.
  • save-update - when the object is saved/updated, check the assoications and save/update any object that require it (including save/update the assoications in many-to-many scenario).
  • delete - when the object is deleted, delete all the objects in the assoication.
  • delete-orphans - when the object is deleted, delete all the objects in the assoication. In addition to that, when an object is removed from the assoication and not assoicated with another object (orphaned), also delete it.
  • all - when an object is save/update/delete, check the assoications and save/update/delete all the objects found.
  • all-delete-orhpans - when an object is save/update/delete, check the assoications and save/update/delete all the objects found. In additional to that, when an object is removed from the assoication and not assoicated with another object (orphaned), also delete it.

 


Be the first to rate this post

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

Tool for Mockup and Wireframes

December 11, 2008 14:58 by mikkel@ovesens.com

Take a look here

http://balsamiq.com/


Be the first to rate this post

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

Jeff Richter Video on Asynchronous Programming and his Power Threading Library

December 10, 2008 14:26 by mikkel@ovesens.com

Jeff Richter Video on Asynchronous Programming and his Power Threading Library:

 http://blogs.msdn.com/charlie/archive/2008/12/03/jeff-richter-video-on-asynchronous-programming-and-his-power-threading-library.aspx


Be the first to rate this post

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

Learning NHibernate

November 17, 2008 20:30 by mikkel@ovesens.com

NHibernate is a port of Hibernate Core for Java to the .NET Framework. It handles persisting plain .NET objects to and from an underlying relational database. Given an XML description of your entities and relationships, NHibernate automatically generates SQL for loading and storing the objects. Optionally, you can describe your mapping metadata with attributes in your source code.

Update 18-11-2008, well it turs out there infect is a NHibernate 2.0documentation here:

http://nhforge.org/doc/nh/en/index.html

 

A very good place to start is here:

http://www.hibernate.org/hib_docs/v3/reference/en-US/html/

Properties

Chapter 5 explains setting up properties in the mapping file, tags and attributes.

http://www.hibernate.org/hib_docs/v3/reference/en-US/html/mapping.html

Collections

Chapter 6 is about collections and lists

http://www.hibernate.org/hib_docs/v3/reference/en-US/html/collections.html

Many-to-many relations

Handling many-to-many relations can be difficult, especially when it comes to cascading and data integrity. This blog post explains one way of handling it.

http://codebetter.com/blogs/peter.van.ooijen/archive/2008/05/29/nhibernate-many-to-many-collections-or-mapping-is-not-one-table-one-class.aspx


Be the first to rate this post

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

Visual Studio 2008 - ItemTemplatesCache missing

November 10, 2008 19:57 by mikkel@ovesens.com

The issue 

My Visual Studio 2008 opened the solution file with no problem. However, when I was to add a new class, user control, aspx file or similar, I received the error:

Could not find part of "c:\Program Files\Microsoft Visual Studio 9.0\...\ItemTemplatesCache..."

[and the path to the ItemTemplatesCache in Visual Studio 2008 folder].

The solution

Well, one slution would be to reinstall or repair installation of Visual studio 2008. However, after searching for a while I found a solution that is much easier:

  1. Open "Visual Studio 2008 Command Prompt" as an Administrator
  2. Run the command: "devenv /installvstemplates"
  3. That's it

 

It takes a while but it completely rebuild your item template cache if it is corrupt or completely missing like me.


Be the first to rate this post

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

Compressing Viewstate

November 9, 2008 02:55 by mikkel@ovesens.com

Original post:

http://mikemason.ca/blog/?p=24

 

This article is written by guest author John Finlay. Thanks John!

Perhaps the primary statistic by which clients measure web applications is response time. Obviously, web pages that take too long to appear, or at least start to appear, frustrate end-users.

One of the many reasons a web page can be slow is merely the quantity of information that must be sent to a user’s browser; pictures, video, maps, and huge quantities of text can lead to poor response times, even though the server and all processes behind it are running efficiently.

ASP.NET has a page state management mechanism known as “ViewState”, by which changes to page markup (altered text, colors, dynamic formatting etc.) are retained. On a postback that encoded ViewState data is made available to the server, and the .NET environment automatically updates the state of web control properties to represent the combination of the original markup and the ViewState-retained changes. ASP.NET provides an optional SessionPageStatePersister to retain the ViewState in the user’s Session data, and other persisters can be written whereby the ViewState may be stored anywhere you desire. By default ASP.NET persists ViewState as base-64 encoded text in a hidden input field in the HTML delivered to the browser.

Developers need to understand how ViewState works, and what it does (and does not do), to suss how their web pages actually function. For an excellent and detailed writeup on ViewState, see Dave Reed’s article Truly Understanding ViewState.

One issue we experienced on a current project in Calgary was with excessive ViewState size. For example, we frequently use a custom control to generate a customized scrollable table of data on a web page, and these tables frequently contain hundreds of rows and many columns of data. Unless switched off, ViewState is maintained automatically for all this generated data, in effect causing the same data to be transmitted twice to the user’s browser. Often we want the ViewState retained as a necessary evil, however, to support client-side sorting of the data rows.

Sometimes these large tables are inside an UpdatePanel or an AJAX TabContainer, both of which retain their own ViewState plus all the ViewState of all the controls inside them. Now we have even more ViewState to cope with, and for tables contained in multiple tabs in a TabContainer, the actual ViewState size can get unexpectedly large; we have seen a relatively benign popup page with almost 6MB of ViewState! Response time was rather unpleasant for locally connected users, and downright unacceptable for those at remote sites.

Rather than refactoring individual pages, we took a more global approach: compressing the ViewState before delivery. This approach was very successful, primarily because of what the ViewState contains. Remember that it represents all data not explicitly coded in the HTML markup, which includes all dynamic formatting and placement specifications. For a large table, there is massive repetition of various property settings, ideal data for compression algorithms.

Our implementation was simplified by the fact that all our code-behind partial classes inherit a project-wide BasePage, which is where we placed the compression and decompression logic. In ASP.NET, the System.Web.UI.Page class contains two overrideable methods that were ideal for our compression logic: SavePageStateToPersistenceMedium() and LoadPageStateFromPersistenceMedium().

Sample compression code is shown below, and requires the SharpZipLib compression library.

   1:  using ICSharpCode.SharpZipLib.Zip.Compression;
   2:  using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
   3:  ...
   4:  private const int BUFFER_SIZE = 65536;
   5:  private int viewStateCompression = Deflater.NO_COMPRESSION;
   6:   
   7:  public int ViewStateCompression
   8:  {
   9:      get { return viewStateCompression; }
  10:      set { viewStateCompression = value; }
  11:  }
  12:   
  13:  protected override void SavePageStateToPersistenceMedium(Object state)
  14:  {
  15:      if (ViewStateCompression == Deflater.NO_COMPRESSION)
  16:      {
  17:          base.SavePageStateToPersistenceMedium(state);
  18:          return;
  19:      }
  20:   
  21:      Object viewState = state;
  22:      if (state is Pair)
  23:      {
  24:          Pair statePair = (Pair) state;
  25:          PageStatePersister.ControlState = statePair.First;
  26:          viewState = statePair.Second;
  27:      }
  28:   
  29:      using (StringWriter writer = new StringWriter())
  30:      {
  31:          new LosFormatter().Serialize(writer, viewState);
  32:          string base64 = writer.ToString();
  33:          byte[] compressed = Compress(Convert.FromBase64String((base64)));
  34:          PageStatePersister.ViewState = Convert.ToBase64String(compressed);
  35:      }
  36:      PageStatePersister.Save();
  37:  }
  38:   
  39:  private byte[] Compress(byte[] bytes)
  40:  {
  41:      using (MemoryStream memoryStream = new MemoryStream(BUFFER_SIZE))
  42:      {
  43:          Deflater deflater = new Deflater(ViewStateCompression);
  44:          using (Stream stream = new DeflaterOutputStream(memoryStream, deflater, BUFFER_SIZE))
  45:          {
  46:              stream.Write(bytes, 0, bytes.Length);
  47:          }
  48:          return memoryStream.ToArray();
  49:      }
  50:  }

Looking at the “Save” logic, you’ll see that the default setting for compression is to not do any compression at all. Other deflater settings include DEFAULT_COMPRESSION, BEST_SPEED and BEST_COMPRESSION. The ViewStateCompression property must be set before ViewState is retrieved, in the Page_PreInit() or Page_Init() method. ViewState is available as the Second part of a Pair object (the first part references ControlState, which is different than the page’s ViewState. We decided not to compress it due to its limited use and small size. See Bean Software’s ControlState Property Demystified). We grab the ViewState object hierarchy, serialize it using the same System.Web.UI.LosFormatter that ASP.NET uses to serialize ViewState, compress it using SharpZipLib, System.Convert it to a base-64 string again, and hand it to the PageStatePersister to be written out. Use the PageStatePersister to write to the normal __VIEWSTATE hidden field; the AJAX toolkit gets upset if you manually write it to any other field.

The reverse is done on a PostBack:

   1:  protected override Object LoadPageStateFromPersistenceMedium()
   2:  {
   3:      if (viewStateCompression == Deflater.NO_COMPRESSION)
   4:          return base.LoadPageStateFromPersistenceMedium();
   5:   
   6:      PageStatePersister.Load();
   7:      String base64 = PageStatePersister.ViewState.ToString();
   8:      byte[] state = Decompress(Convert.FromBase64String(base64));
   9:      string serializedState = Convert.ToBase64String(state);
  10:   
  11:      object viewState = new LosFormatter().Deserialize(serializedState);
  12:      return new Pair(PageStatePersister.ControlState, viewState);
  13:  }
  14:   
  15:  private byte[] Decompress(byte[] bytes)
  16:  {
  17:      using (MemoryStream byteStream = new MemoryStream(bytes))
  18:      {
  19:          using (Stream stream = new InflaterInputStream(byteStream))
  20:          {
  21:              using (MemoryStream memory = new MemoryStream(BUFFER_SIZE))
  22:              {
  23:                  byte[] buffer = new byte[BUFFER_SIZE];
  24:                  while (true)
  25:                  {
  26:                      int size = stream.Read(buffer, 0, BUFFER_SIZE);
  27:                      if (size <= 0)
  28:                          break;
  29:   
  30:                      memory.Write(buffer, 0, size);
  31:                  }
  32:                  return memory.ToArray();
  33:              }
  34:          }
  35:      }
  36:  }

If no compression were originally applied, we call the base method to do its thing. Otherwise, the state information is derived from the hidden HTML field, and the ViewState portion of it is converted from base-64, decompressed, reconverted to base-64, and deserialized into its original object hierarchy.

Some experimentation should be done to determine the optimal sizes of the various buffers; here we used our elite programming skills to pick a workable size (we guessed). Likewise, likely no single type of compression (default, max or fast) is optimal in all circumstances.

So how does a developer determine whether ViewState compression is required at all? One could view the page source, copy the value of the __VIEWSTATE hidden input field, paste it into an editor and determine the column width. A better approach is to display the size of the ViewState (during development) as part of a FooterInfoControl on the page itself. Our MasterPage.Master displays the footer control, which contains other controls, one of which is the ViewstateSizeControl itself:

   1:  public class ViewstateSizeControl : Label
   2:  {
   3:      private const string SCRIPT = "$('{0}').innerText = document.forms[0].__VIEWSTATE.value.length;”;
   4:   
   5:      protected override void OnLoad(EventArgs e)
   6:      {
   7:          if (Visible)
   8:          {
   9:              Page.ClientScript.RegisterStartupScript(
  10:                  typeof(Page),
  11:                  UniqueID,
  12:                  string.Format(SCRIPT, ClientID),
  13:                  true);
  14:          }
  15:          base.OnLoad(e);
  16:      }
  17:  }

This allows the developers and testers to see how big the ViewState is, both before and after compression, with no extra effort. Quite handy! And how well does this compression mechanism work? In most cases, you should expect at least a 90% reduction in ViewState size. For example, the aforementioned 6MB of data actually compressed to less than 60K. Quite effective!

Demo source code is available to accompany this article: ViewStateCompressionDemo.zip (Visual Studio 2005, C# 2.0 web application)


Be the first to rate this post

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

2 opdateringer til Vista retter de værste fejl

October 16, 2007 23:10 by mikkel@ovesens.com
De to opdateringspakker skal indtil videre installeres manuelt fra Microsofts hjemmeside ved at gå ind under KB938979 og KB938194.

Be the first to rate this post

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