May 22 2009

Lack of MetaData on code and .NET Framework libraries


Although .NET Reflecton offers a decent look at the Types you build, it is pretty generic in its offering. For example, it does not yet provide us with capabilities to control and secure how Reflection can inspect our Types. If you do not want a Type to be Instantiated via Reflection, or if you would like to restrict certain Types from being visible at all. Further there is no idea for extension or change. One cannot simply plug into Reflection and override behaviour.

High abstractions and higher flexibility

Many more wrappers are needed, at a higher level, which would sit on top of .NET Reflection. For example, architectural dependencies. Agile mechanisms, if code had enough metadata attached, one could alter the code via reflection/refactoring in a much more dynamic way. Consider the following two web service methods in two different companies.
  public class PersonService
    {
        public void AddPerson(string lastname, string firstname, 
            string birthdate)
        {
        }
    }

    public class PersonWebService
    {
        public void AddNew(string firstname, string surname)
        {
        }
    }

You can see that they are completely different contracts. Interface and contract speaking they are like apples and sand. However, to your mapping, calculating, schema, metadata, tagging and understanding brain, its the same thing.

What we need, is to remove ourselves from Contracts to something more like guidelines.

  public class PersonService
    {
        [Operation(Schema:Person, "Add")]
        public void AddPerson([schema(LastName)]string lastname, 
             [schema(FirstName)]string firstname, 
             [schema(DateOfBirth)] string birthdate)
        {
        }
    }

    public class PersonWebService
    {
        [Operation(Schema:Person, "Add")]
        public void AddNew([schema(FirstName)]string firstname, 
             [schema(LastName)]string surname)
        {
        }
    }

Tags: , , ,

Comments

1.
W.Meints W.Meints Netherlands says:

As much as you might want to allow this stuff in .NET, it simply isn´t going to help developers in the long run.

Having more abstractions on top of .NET reflection will only make applications slower. This something typical for all .NET constructs that offer the functionality you are describing.

.NET is actually a runtime that was made with statically typed languages in mind. Making the languages do the dynamic stuff you describe will make them really slow, because everything needs to broken down in to static pieces again, no matter how dynamic it is.

Also you can´t check for type mismatches at compile time. Simply because you don´t know what type of object a reference will point at up until the method call is actually performed. So instead of leaving the problem of wrong code with the developer you move it over to the user of the application and give him a feeling that the application is rubbish.

I personally feel that with the current state of C# and .NET developers might have to pitch a bit more effort to get stuff working, but the quality and performance of applications will also be better.

2.
Jonathan Jonathan says:

Although your point about making things slower is valid, I have to point out that the same arguments have been put forward for most new layers.

We have accepted the Framework library layers above the Win32 API, although the Win32 API is by far more efficient (most of the time). The reward for having the .NET Framework Libraries are because it adds a greater value for developers.

I do think that we are faced at a point where we have to begin defining what is an open or closed in terms of layers. Reflection could be an open layer. I don't think it needs to be obfuscated away, just wrapped for a different purpose.

At the moment, most Assemblies are just packaged and bundled together. I am not sure of how they decide what goes where, but its not exactly solidly defined, and the grain is still too coarse.

With regards to your point on dynamic vs statically Typed, I think our future lies in much more dynamic environment. There will always be a place for contracts and static behaviour, but power comes from being dynamic. The pitfalls and problems though that dynamic environment bring, must be addressed.

Take for example, the Factory Pattern. Our tools have no idea what the Type will be when returned, except for it being of a certain base type (in polymorphic terms). That level of abstraction is currently not catered for properly by our contract driven tools. I think we need to get past this love affair with contracts and push for dynamic-ness too, but with tool support.

btw. I do not favour dynamic over static by default, it depends on the solution required for a given problem.