Thom Lawrence

Entries tagged as ‘nhibernate’

NHibernate-friendly Types

September 11, 2006 · No Comments

It’s probably a combination of laziness and plain bad form to try and map too many BCL types to your database, but if you’re interested, I’ve gone through a few important assemblies and compiled a list of classes that:

  • Are visible outside their assembly
  • Are neither abstract nor sealed
  • Are non-generic
  • Have a public or protected parameterless constructor

My assumptions might be wrong here, but I expect this to be enough for NHibernate to be able to save objects of those classes, and more importantly load and proxy them.

This is the list: PersistableTypes.txt. And here’s the code:

class Program
{
        static void Main(string[] args)
        {
            foreach(string assemblyName in GetAssemblyNames())
            {
                Assembly assembly = Assembly.Load(assemblyName);
                Type[] types = assembly.GetTypes();
                foreach (Type t in types)
                {
                    if (IsProxiable(t))
                        Console.WriteLine(t.FullName);
                }
            }
        }

        private static string[] GetAssemblyNames()
        {
            return new string[]
            {
                "System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
                "System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
                "System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
                "System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
                "System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
                "System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            };
        }

        private static bool IsProxiable(Type t)
        {
            return !t.IsAbstract && !t.IsSealed && t.IsVisible && !t.IsGenericType
                && HasProxiableInitializer(t);
        }

        private static bool HasProxiableInitializer(Type t)
        {
            return Array.Exists(t.GetConstructors(),
                IsProxiableInitializer);
        }

        private static bool IsProxiableInitializer(ConstructorInfo ctor)
        {
            return ctor.GetParameters().Length == 0 &&
                (ctor.IsPublic || ctor.IsFamily);
        }
    }

Should be some fun stuff in there - I wonder if a Web Form and the complete hierarchy of its controls can be persisted nicely?

Categories: C# · Code · Programming
Tagged: , , ,