2006-09-27

Spring.Net Schema

Its easy to get intellisense for your spring object files, as resources, by copying the spring-objects.xsd file into %ProgramFiles%\Microsoft Visual Studio 8\XML\Schemas.

2006-09-25

Mem Usage vs VM Size

When viewing my task manager, I always make sure that the VM Size column is turned on. This shows the actual memory footprint that the process has, rather than what is currently taken. For instance, if you minimise an application, usually the Mem Usage column will drop, but the VM Size doesnt. Though it didnt matter with my memory leak program, both the numbers filled all my available ram.

Design Time Master Page

Robin provides a neat solution to design time masterpage pattern. Although Server.Transfer and Server.Redirect wont work, but that should be fixed in IIS 7.

GC.Collect not the solution

Many times i have come to situations where the memory usage in my program spirals out of control. Always i think it is a problem with anything but my program. I test the cleanups of the 3rd party libraries, and every other little thing. Then i resort to put GC.Collect statements around, thinky that it will magically free memory.
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

Then i go searching for how to debug these things:
http://blogs.msdn.com/tess/archive/2006/09/06/742568.aspx
http://blogs.msdn.com/tess/archive/2006/08/11/695268.aspx

Finally i get stuck into reviewing my code with a fine tooth comb, and find one thing that was causing the leak, static array on a singleton, used in temporary processing, that didnt get cleared.

2006-09-23

HttpContext Items Strongly Typed

I have just refactored my nHibernate session store in HttpContext.Items to use this as the base class. This fits perfectly with the spring wiring and means that i can also use it in other projects, for other items that i want strongly typed access to the object. I forgot the dispose method so i was having bad memory leaks. See also the default(T) keyword, interesting.

    public abstract class AbstractObjectCacher<T> {
        #region IoC
        private IObjectCache<T> cache;
        public IObjectCache<T> Cache {
            set { cache = value; }
            get { return cache; }
        }
        #endregion
        public T Get() {
            return cache.Get();
        }
        public void Set(T obj) {
            cache.Set(obj);
        }
        public void Dispose() {
            T obj = Get();
            if(obj != null) {
                Set(default(T));
                if(obj is IDisposable) {
                    ((IDisposable)obj).Dispose();
                }
                obj = default(T);
            }
            cache.Clear();
        }
    }

So for instance, the thread store i use for unit testing will look like this:

    public class ThreadObjectCache<T> : IObjectCache<T> {
        #region IoC
        private string key;
        public string Key {
            set {
                key = value;
                ThreadStore.Initialise(key);
            }
            get { return key; }
        }
        #endregion
        public T Get() {
            return (T)ThreadStore.Get(key);
        }
        public void Set(T obj) {
            ThreadStore.Set(key, obj);
        }
        public void Clear() {
            ThreadStore.Set(key, null);
        }
        public void Dispose() {
            ThreadStore.Dispose(key);
        }
    }

And the HttpContext store will look like this:

    public class HttpContextCache<T> : IObjectCache<T> {
        #region IoC
        private string key;
        public string Key {
            set { key = value; }
            get { return key; }
        }
        #endregion
public T Get() {
            return (T)HttpContext.Current.Items[key];
        }
        public void Set(T obj) {
            HttpContext.Current.Items[key] = obj;
        }
        public void Clear() {
            HttpContext.Current.Items[key] = null;
        }
        public void Dispose() {
            HttpContext.Current.Items.Remove(key);
        }
    }

2006-09-20

Changes are not allowed while code is running

How often have you got this message when you try to edit your source while debugging? "Changes are not allowed while code is running"


Notice the lock icon next to the filename, showing its read only.

This is a feature of VS that allows you to change the code and continue running with the modified code.
It seems to be only useful for desktop applications, not web apps that we develop.
We know that if we change code in the binary then we will need to recompile anyway, so how do we turn off this annoying message.
To remove the message, turn off Edit and Continue by:
Going to options

Go to Debugging, Edit and Continue and uncheck the top box



By turning off Edit and Continue, you still can create and remove debug points why the process is paused.
And you can still edit aspx’s, ascx’s, and their code besides (in the web application) without problems.

db4o 5.7

A new developement release of db4o has been released, which is supposed to be much faster than the previous versions. 5.7 is a couple of releases ahead of my current version 5.4.8 and the object browser has been updated too. This weekend if i get a chance i will work on intergrating this into the repository wrapper.

2006-09-19

Updating Atlas

After updating the AtlasControlToolkit to the latest version 1.0.60914.0, i ran into a bunch of problems. The first was the removal of the third AtlasControlExtenderer binary, whichwas easy to fix, just had to update references to the AtlasControlToolkit and namespaces.
The other issue i had was i was gettting a debugger alert client side about
PageRequestManagerID not being an attribute on draggablelistitem. This is a custom drag item i have implemented, so i just went digging through AtlasControlToolkit and found something similar to what i wanted to do DragPanelProperties and it has go this code:

        [EditorBrowsable(EditorBrowsableState.Never), 
  Browsable(false), 
  DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string PageRequestManagerID {
            get { return null; }
            set { throw new NotImplementedException(); }
        }


So i just copied that into mine, and things worked. Its weird that there is no need to have the "new" keyword on this method. And why didnt it work in the first place.

ASX Partition Table

Once upon a time, i had a application that graphed asx data (australian stock exchange). It got imported from some csv files in sql server. For performance reasons i had each code as a seperate table. But that was before i knew about indexes.
Now there may be a better way, by using Partitioning. I will be able to have the same tables, and then a view on top of them that i can query. The partitioning will happen on the stock code. With sql server 2005 this will happen automatically. Nice.

http://weblogs.sqlteam.com/dmauri/archive/2005/07/18/7147.aspx
http://searchtechtarget.techtarget.com/originalContent/0,289142,sid91_gci1207038,00.html
http://www.google.com/search?hl=en&lr=&q=partition+mssql+2005

Unicode Kanji Characters

This table is awesome, if i need to match a character i do a copy on the page, then find on this one. Then get the number and convert it to &#123

Javascript Triple Equals (===)

I download Script# today, and had a browse through the script files, and in the first couple of lines was this triple equal operation (===). Now i have been writing javascript for nearly 5 years and hadn't ever seen this.

http://www.flashinsider.com/2005/07/25/null-equals-undefined-but-null-doesn-t-strictly-equal-undefined/
http://blogs.msdn.com/ericlippert/archive/2004/07/26/197302.aspx
http://www.ejball.com/EdAtWork/PermaLink.aspx?guid=1a946f91-d37d-4104-a1d3-63e633afafba

So it does a comparison of the value, including the datatype. I don't know if i will ever use i, considering i have never used it, but it might come in handy later.

Output Stream Filtering

I was debating today whether to use an output filter to replace special macros in the my site templates at runtime. Apart from the issues outlined in this article, where the filters dont run if End() is called and in some transfer and redirect methods, the process wouldn't work if i was using the visual studio design editor. Say for example that i was replacing the application root path with a macro like [[AppRoot]].
My prefered method would be to have a helper static class that references HttpContext.Current in runtime and something else a design time. Then use <%= HtmlHelper.AppRoot %> in templates instead.
Remember to wrapper the script tags in an asp:PlaceHolder if you get the can't add to controls collection error.

2006-09-18

Programming Index

So how does your programming language rank in terms of this index...

Delete All Rows

Many a time i have wanted to delete all the rows from all the tables in the database. It usually involves a script getting all the names of the tables and deleting one at a time. This script is awesome where you can do it in one line:

EXEC sp_MSforeachtable @command1 = "TRUNCATE TABLE ?"

How many other things will this make easier.

2006-09-14

Random Selects

The simple things are often the best. Like this way of selecting a random row from a table.

SELECT *
FROM MyTable
ORDER BY NEWID()

2006-09-11

.Net 2.0 RSA Encryption

Even though the new framework makes some tasks much easily, like openning a file in one line, other tasks, like encrypting data still require helper functions.
The classes all exist in the System.Security.Cryptography namespace:
http://msdn2.microsoft.com/en-us/library/system.security.cryptography.aspx
I have choosen to use RSA asymmetric encryption:
http://msdn2.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider.aspx

Many examples helped over the basic msdn documentation:
http://www.codeproject.com/dotnet/RSACryptoPad.asp
http://www.codeproject.com/dotnet/SimpleEncryption.asp
http://www.eggheadcafe.com/articles/20020630.asp
http://www.codeproject.com/dotnet/ComboEncryption.asp
http://blogs.msdn.com/lcris/archive/2006/05/08/592868.aspx

One of the annoying things to remember is that on a shared hosting server, you will most likely not have access to the underlying COM object directories. I thought this would be a show stopper, but this only restricts you from decrypting the values, you can still encrypt items fine. So if your requirement is to collect data and store it securely with a public key, then download it remotely and process it with the private key, this should work.

Some issues that may arise:
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q322/3/71.asp&NoWebContent=1

.Net Serializers

I had originally implemented a serialization using BinaryFormatter because i needed the result as a byte array. It all work well when running in nUnit, but when i moved it to my medium trust web application it failed. This was due to apparently, the use of reflection in the BinaryFormatter when it serializes all of the object graph properties, including private fields. The easy way to make it work was to change to using the XmlSerializer.

Interestingly though, the XmlSerializer does not implement the same interface as the BinaryFormatter, so i can't just sub in the constructor. The serialise method has the same parameters which is strange.

Heres the msdn doc on the XmlSerializer:
http://msdn2.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

And this explains the trust issue:
http://haacked.com/archive/2006/07/09/ConfiguringLog4NetWithASP.NET2.0InMediumTrust.aspx

I really thought there might be a way to set the properties that get serialized, but it looks like a LinkDemand and Security assert is performed right at the start of the internal serialization class, so theres no hope of ever getting this to work.

2006-09-06

Inline Server Script in Header

Have you ever wanted to have inline script tags in your header control and add controls programatically. You will propably be getting an error about not being able to do this. This is especially true when you have a control that writes scripts into the header and then you also wish to have inline style sheets with the proper application path. The simple way to get around this problem is to wrap the inline scripts in an asp:PlaceHolder.

<html>
  <head runat="server">
    <title></title>
    <asp:PlaceHolder runat="server">
      <link href="<"%= SomeVariable %>/style.css" rel="stylesheet" type="text/css" />
    </asp:PlaceHolder>
  </head>
</html>


This way you can still add controls to the header by doing:
Page.Header.Controls.Add();