Vault of Thoughts

2006-06-12

Please visit my new blog at vaultofthoughts.net

ClassPropertiesBuildProvider

Some time ago I have posted an article about using Build Provider feature of ASP.NET 2.0 to build a strongly typed classes for accessing the files found in the application virtual directory. I myself have been using this provider ever since for every Response.Redirect(). I hope that it serves you as well.


I have found yet two more ways of using the Build Providers to make your applications safer (the second one I will describe in few days). Today I present to you, the ClassPropertiesBuildProvider. This time we get a strongly typed way to access name of the properties of our objects. When this could be usefull? Of course everywhere where we have to specify the name of the properties using a plain string. And where could that be? Mostly in data-binding expressions such as Eval and Bind.


Take for example a sample data-binding scenario where we bind a GridView control to an array of Client objects. Each client has Age, FirstName and LastName:




As you can see we get the full intellsense over available properties. Keep in mind that those properties actually return a string containing the name of the property - exact thing needed to use the Eval or Bind. Notice also that in case some property changes its name, or is deleted, we get an instant compile time error as oposite to the runtime exception you get when you use a not strongly typed Eval("Age").


Using the ClassPropertiesBuildProvider is similiar to the PathsBuildProvider. Just add extension=".cpbp" type="ClassPropertiesBuildProvider.ClassPropertiesBuildProvider" to a web.config file and put one or more file with .cpbp extension in the App_Code directory. In this file the first line declares a namespace under which the classes will be generated ("Classes" in this example). On the following line put the name of the assemblies you would like to have analyzed.


The solution is available as a source and binary here and here.


As with PathsBuildProvider, the provided solution is for demonstration purposes and as such you should be aware that it might not always work as expected. If you find some bugs, or wish to provide some improvements, please feel free to do it, and contact me so I too can benefit from it :-)


kick it on dotnetkicks.com

If you liked this article why not support its author by making a donation?

8 Comments:

  • Any idea how we can debug and set a breakpoint in the custom BuildProvider code?

    By Anonymous Anonymous, at 5:53 PM  

  • Now THAT is THE question!
    When I start building a build provider the breakpoints work. Then, for unknown reason they stop never to start working again :-(.
    Try building a provider from scratch and see if it works for you the same way.
    Additionally given the problems with debugging I mostly test the output of my providers by calling methods on the helper class that does the actual work - hence it is public and not internal.

    By Blogger Mikeon, at 8:58 AM  

  • I got it. I openned another instance of Visual Studio 2005 and select "attach process" in the tools menu. Select the other instance of VS where you are compiling the buildprovider. Open buildprovider source code with the second instance of VS and set the breakpoint. To hit the breakpoint, you can recompile with the 1st instance of VS or edit/save any file with the extencion registered with the buildprovider.

    By Anonymous Anonymous, at 10:26 AM  

  • Using CodeDom classes is not a simple task. Is it possible to generate a string containing C# instructions, compile it and inject it in the assembly you are just compiling, like we do with the buildprovider?

    By Anonymous Anonymous, at 10:33 AM  

  • As for debugging. Its nice you have found a way to do it, but still the question remains - why it works and then stops?
    As for the injecting the the string containing C# code, I'm almost sure it is possible. My friend was doing something like that and if you want I'll post some examples.

    By Blogger Mikeon, at 10:10 PM  

  • Please, if you could post the examples it would be great.

    By Anonymous Anonymous, at 2:57 PM  

  • Ok so I have looked into parsing the C#. What the build provider needs is a CodeCompileUnit object. This can be obtained by means of CodeDom namespace as shown in the example. It is (or rather may be) possible to get the CodeCompileUnit by parsing a string of text containing C# class definition. To do it, you use the Parse method of CodeDomProvider class. The catch is that this method is not implemented in the .NET Framework. You have to resort to third party libraries.

    By Blogger Mikeon, at 10:14 PM  

  • Who knows where to download XRumer 5.0 Palladium?
    Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!

    By Anonymous Anonymous, at 2:32 PM  

Post a Comment

<< Home