The Inquisitive Coder - Davy Brion’s Blog

Thinking outside of the typical .NET box

Archive for the 'Uncategorized' Category


Bye Bye Noma

Posted by Davy Brion on 6th April 2008

As you may have noticed, i’ve been pretty quiet about Noma lately… I haven’t worked on it for weeks (or is it months now?), mostly because the project just doesn’t interest me anymore, and also because i haven’t had a lot of free time lately. When i do have some free time, i like to spend it on learning new things or just experimenting with different libraries or ideas instead of trying to work on something that no longer interests me, or feeling somewhat guilty about not working on it :)
So, the noma project can pretty much be considered dead from now on…

With that said, one of the things i’m playing around with now is setting up a sample application based on my northwind nhibernate mappings. The idea is basically to just use it as a playground to experiment with new techniques or libraries and come up with good design and architecture ideas. Obviously, i plan on posting about these experiments quite a lot ;) … depending on how much free time i’ll have obviously :)

Posted in Uncategorized | No Comments »

A picture says a 1000 words

Posted by Davy Brion on 11th March 2008

Noma’s First Run

i’m finally spending some time on Noma again, and this is a screenshot of the results of trying it out for the first time.

Posted in Uncategorized | No Comments »

Noma Checks Preview

Posted by Davy Brion on 4th January 2008

This is just a small preview of how to implement Noma checks. The classes you’ll see below work, but i still have to write the engine which runs these checks and feeds them the correct data. But i’m pretty happy with how the implementation of checks looks at the moment, so i wanted to post something about it.

First of all, there’s the ICheck interface which looks like this:

    public interface ICheck {}

pretty meaningless huh? I’m just using this as a base interface type to identify all different kinds of Noma checks. For instance, here’s the interface that you’ll need to implement if you want to perform a check on a ClassMapping:

    public interface ICheckClassMappings : ICheck

    {

        ICollection<ClassMapping> Select(ICollection<ClassMapping> classMappings);

        void Check(ClassMapping classToCheck, MetaDataStore store);

    }

The Select method is passed all known ClassMappings, and you’ll need to return the ones that your check is interested in. Each one of the selected ClassMappings will then be passed to the Check method along with the entire MetaDataStore which you’ll use to look up all the data you need to perform your check.

If you want to write a check for something that belongs to a ClassMapping, you’ll need to implement the generic ICheck interface:

    public interface ICheck<T> : ICheck

    {

        ICollection<T> Select(ICollection<T> mappings, ClassMapping parentClassMapping);

        void Check(T mappingToCheck, ClassMapping parentClassmapping, MetaDataStore store);

    }

The type parameter will be the kind of mapping you want to check, like a PropertyMapping for example. The Select method will then receive a collection of all these mappings per ClassMapping, along with a reference to the ClassMapping the mappings belong to. You’ll then need to return a collection of all the mappings you want to perform the check on. Each of these will then be passed to the Check method, along with the reference of the ClassMapping it belongs to, and a reference to the MetaDataStore.

To make it more clear, i’ll show you two examples. First of all, there is the TableExists check:

using System.Collections.Generic;

using System.Linq;

 

using Noma.NHibernate.Mappings;

 

using NUnit.Framework;

 

namespace Noma.Runner.Checks.MappingToDatabase.Class

{

    public class TableExists : ICheckClassMappings

    {

        public ICollection<ClassMapping> Select(ICollection<ClassMapping> classMappings)

        {

            return classMappings.Where(c => !string.IsNullOrEmpty(c.Table)).ToList();

        }

 

        public void Check(ClassMapping classToCheck, MetaDataStore store)

        {

            Assert.IsNotNull(store.GetTable(classToCheck.Schema, classToCheck.Table),

                “Table [{0}].[{1}] was not found”, classToCheck.Schema, classToCheck.Name);

        }

    }

}

Pretty simple right? It selects each ClassMapping that has a Table defined, and then in the Check method we use the familiar NUnit Assert class to verify that the referenced table actually exists in our MetaDataStore.

And this is how the ColumnExists check looks like:

using System.Collections.Generic;

 

using Noma.Database;

using Noma.NHibernate.Mappings;

using Noma.Runner.Checks.MappingToDatabase.Class;

 

using NUnit.Framework;

 

namespace Noma.Runner.Checks.MappingToDatabase.Property

{

    [RequiresMappingToPassCheck(typeof(TableExists))]

    public class ColumnExists : ICheck<PropertyMapping>

    {

        public ICollection<PropertyMapping> Select(ICollection<PropertyMapping> mappings, ClassMapping parentClassMapping)

        {

            // we need to check all property mappings

            return mappings;

        }

 

        public void Check(PropertyMapping mappingToCheck, ClassMapping parentClassmapping, MetaDataStore store)

        {

            string columnName =

                !string.IsNullOrEmpty(mappingToCheck.ColumnName) ? mappingToCheck.ColumnName : mappingToCheck.Name;

 

            Table table = store.GetTable(parentClassmapping.Schema, parentClassmapping.Table);

 

            Assert.IsNotNull(table.GetColumn(columnName),

                            “Class [{0}] refers to column [{1}] in table [{2}].[{3}] but the column was not found”,

                            parentClassmapping.Name, columnName, parentClassmapping.Schema, parentClassmapping.Table);

        }

    }

}

Notice the usage of the [RequiresMappingToPassCheck]. You can use this to declare dependencies between checks… if one of the checks referenced with the [RequiresMappingToPassCheck] attribute already failed for a particular mapping or its parent ClassMapping, the check will not be executed for that specific mapping. In this case, we depend upon a check that only checks ClassMappings. So the ColumnExists check will not be executed for each ClassMapping that already failed the TableExists check.

In the Select method, you can see we simply return all the PropertyMappings we’ve received since we need to check them all. And in the Check method, we look up the table the ClassMapping refers to, and verify that the referenced column is actually there.

I’ve only just come up with this approach… The checks themselves work already, but as i mentioned earlier, i still need to write the engine to execute these checks, feed them the correct data, and resolve the dependencies between the checks. That should be some pretty interesting code :)

Obviously, you will be able to plug in your own checks which can also have dependencies between them.

Posted in Uncategorized | No Comments »

YANSU

Posted by Davy Brion on 12th December 2007

That’s right, Yet Another Noma Status Update… anyway, there’s finally some progress to report. The hibernate mapping parsing is finally done so i can now start working on the rules engine.

At the moment, this is what I have in mind: each rule would have to implement a very simple interface which will probably only have 2 methods… the first would allow the rule to register which kind of ClassMappings it’s interested in, the other would actually execute the rule on each of the selected ClassMappings.

For instance, if you’d like to write a rule that has to be checked on each class that has one or more many-to-one mappings, you would be able to ‘register’ the data you want to check with something like: return AvailableClassMappings.Where(c => c.ManyToOneMappings.Count > 0);

The other method of your rule would then be called for each of the ClassMappings your rule has registered. Would be pretty simple and easy i think.

Anyways, the goal is to get an alpha release out the door somewhere in January.

Posted in Uncategorized | No Comments »

using a default xml namespace with the XElement class

Posted by Davy Brion on 2nd December 2007

I just upgraded all of the xml parsing code in Noma to use the new classes in the System.Xml.Linq namespace, which btw are MUCH easier to use than the old-school XmlDocument/XmlElement/… set of classes. Anyway, i have a lot of tests that create xml elements in memory so the parsing can be tested on them. Now, all of those elements should of course use the correct xml namespace.

With the new XElement class, this is already much easier to do than it was with the XmlElement class. But, if you’re creating a hell of a lot of XElement instances that all need to use the same namespace, you can very easily extend the XElement class to set the correct namespace by default:

    public class NXElement : XElement

    {

        public NXElement(string name, params object[] objects)

            : base(Constants.XmlNamespace + name, objects) {}

 

        public NXElement(string name, object parameter)

            : base(Constants.XmlNamespace + name, parameter) {}

 

        public NXElement(string name)

            : base(Constants.XmlNamespace + name) {}

    }

And now i can create nested XML elements that use the correct namespace just like this:

            XElement element = new NXElement(Elements.List,

                                            new NXElement(Elements.OneToMany,

                                                           new XAttribute(Attributes.Class, “ClassName”)));

Posted in Uncategorized | No Comments »

Visual Studio 2008 Training Kit

Posted by Davy Brion on 25th November 2007

I try to avoid posts that only link to other content but this is worth it… It is the Visual Studio 2008 Traning Kit which contains presentations, labs, demos on a lot of the new .NET stuff. Imagine that, a Microsoft link actually worth checking out ;)

Posted in Uncategorized | No Comments »

Noma’s hibernate mapping parsing

Posted by Davy Brion on 23rd September 2007

Time for a little status update. I’m still working on parsing the hibernate mapping files but the end is finally near. The following elements are now supported: array, bag, class, column, composite-element, discriminator, element, generator, hibernate-mapping, id, index, index-many-to-many, key, list, many-to-many, many-to-one, map, one-to-many, one-to-one, parent, property, set, timestamp and version. Supported meaning that these elements are parsed from xml and that there are easy to use classes for all of these elements and the data they hold. Not supported yet: any, composite-id, component, dynamic-component, idbag, import, joined-subclass, primitive-array and subclass. Support for these will be added soon. If you can think of an element i forgot, let me know so i can include it.

Now, some of these elements are parsed completely (meaning each attribute and child element), and some of them are only being parsed partially (meaning only the attributes and child elements that i think are relevant to Noma). Elements and attributes that my parser doesn’t understand are simply ignored. But the parser is very easy to extend and/or modify so adding support for other elements/attributes is fairly easy and not a lot of work… but it’s boring work though :) (which is why it’s progressing so slowly)

Once the parser is finished, i need to gather metadata from your entity classes and also provide all of that information in an easy to use object model. That should be fairly easy since it’ll just use reflection to inspect all of the types the classes hold. And then, i’ll finally be able to start working on the rule engine and the whole point of this stuff: the rules :)

If you want, you can always browse the code online or get it all through subversion.

I’ll try to post a weekly status update on Noma from now on… Not sure if this is interesting to any of you, but hopefully it’ll force me to keep spending time on it :P

Posted in Uncategorized | 2 Comments »

Noma status update

Posted by Davy Brion on 1st September 2007

In case you were wondering how far along i am developing Noma, the answer is: not far at all. I really need to spend more time on this. Anyway, so far all i have is a way to retrieve the database meta data from SQL Server and store it in a database-independent and easy to use object model.

Right now i’m working on parsing the nhibernate mapping files and putting the data in an easy to use object model. For a while, i figured i’d just use the nhibernate code that parses the mapping files but i wasn’t very comfortable with that code, and the nhibernate team are in fact doing some heavy refactoring in that area of their codebase. So i decided to just parse it all myself… that code is pretty boring and tedious to write, but in the end i will have an easy to use object model representing the nhibernate mappings.

My goal is basically to make writing noma rules incredibly easy based on these object models (one for nhibernate mapping date, one for database metadata, and one for .NET class data). But i have no idea when i’ll actually have something that works, let alone something usable. And no, i’m not making any promises as to when i’ll have something i can release. But if you are interested, you can always keep an eye on the subversion repository :)

Posted in Uncategorized | No Comments »

The Birth Of Noma

Posted by Davy Brion on 30th July 2007

As i mentioned earlier, i will be developing a tool to analyze NHibernate mapping files. Somewhat like how FxCop analyzes code, this tool will analyze your mapping files and compare them to database metadata and class metadata to spot errors, show warnings, or suggest improvements.

The SourceForge people finally activated my project so i guess it’s somewhat official now… The tool will be called Noma. Yes that’s right… Noma. And no, the name has no meaning whatsoever. If anything, it’s merely a testament to my inability to come with a good project name.

The project page can be found here and the SVN repository can be found here. There’s very little code in there so far, but that should improve soon :)

Posted in Uncategorized | No Comments »