Automatically hide generated partial classes

Automatically hide generated partial classes

Old forum URL: forums.lhotka.net/forums/t/1991.aspx


CaymanIslandsCarpediem posted on Sunday, December 17, 2006

I’m very excited about the new partial class support in .NET 2 especially so that I can use CodeSmith to generate my basic CSLA classes as a partial class and then have my actual business logic in a separate partial class. 

 

As excited as I am about this I found it pretty annoying that there is no easy way to “hide” partial classes behind the main file like VS automatically does for its GUI stuff like forms.  Basically to accomplish this it seems you have to manually edit the project file for each partial class you want to “hide”.  For example:

 

If I have a file “Employee.cs” which stores my actual business logic and another file “Employee.Gen.cs” which stores the CodeSmith generated CSLA “template” for that class you see both of those files in VS just like any other files.  In this case I’d like to have “Employee.Gen.cs” hidden beneath “Employee.cs” just like VS will automatically do with “Form1.resx” and “Form1.Designer.cs” behind “Form1.cs” when it adds a form.  Perhaps I’m just really picky, but it annoys me to no end to see all those extra “Gen” files which I should never really edit anyway.  I’d like them collapsed under the “main” file just like VS does.

 

It turns out this is pretty easy to do but it means manually editing the project’s project file for each file you want to hide.  Again, I’m probably picky but I certainly didn’t feel like going through that for every file so I built a little application to automate this for me.  Now I’m pretty new to manipulating XML files manually via .NET and don’t have any deep understanding of .NET project files so I’m sure there are better way to do what I’ve done but I thought I’d share it in case anyone else was having the same issues I was.  It is attached with full source code (C#) so if anyone wants to use it (or improve anything about it) please feel free.

 

To use the app, you just point to a project file and then enter what the string marks a file as one that should be hidden behind its “parent” file.  So in the example above I’d just point to the project file and enter Gen (this is case sensitive) as the “Dependent Text”.  This will then look at each entry in the proper parts of the project file to see if it can find any files with .Gen. (I add the periods because that’s my naming convention and I didn’t want to accidentally return files which just had Gen embedded somewhere in the name).  For each, match it finds it will strip out the “Dependent Text” which in the example case would give “Employee.cs”.  It then checks to make sure the project file has a reference to such a file and also checks that the “Employee.Gen.cs” file doesn’t all ready have a DependentOn child node (so you can run the tool multiple times on the same file without getting a bunch of duplicates).  If all the above is true it will add the required entry to hide “Employee.Gen.cs” behind “Employee.cs”.

 

I’ve only given this minimal testing but it works well for me.  Please note things could certainly go wrong and if they do, they could mess up your project file so if you do use this tool please be sure to backup the project file first.  Also, if your project file is under source control (or is read-only for any other reason) the app will probably crash (I haven’t done any real exception handling, etc).

 

Anyway, it is attached for you use if you’d like.

cds replied on Sunday, December 17, 2006

I too like the idea of hiding the generated code inside VS, but I went about it a simpler way. Since I wrote my own code generator, I generate my pairs of classes as, for example Customer.cs and Customer.Designer.cs. When these are included in my project they appear as Customer.cs with the Customer.Designer.cs hidden underneath.

Since I already have a GUI designer for my BO classes anyway, the generated partial classes really are "Designer" classes anyway so it all makes sense, and without any editing of the VS.NET project files.

Maybe you can get the CodeSmith templates to generate "Designer.cs" as your generated class suffix.

Another thing I do, and I'm not sure you can easily do this with the CodeSmith templates, is to group my BO classes together in subfolders, and sub-namespaces according to the Use Case they're servicing. My current project has about 130 BO classes at last count across about 20 use cases, so these are all nicely organised inside folders inside VS.NET. Beautiful (at least I think so! Smile [:)]) Much easier to manage than having all the classes in one big folder.

Craig

CaymanIslandsCarpediem replied on Sunday, December 17, 2006

I should have asked that question here I guess before I created this Smile [:)].  All I'd been able to find via Google searches was that you had to do it via the project file.  Didn't realize if you called it Designer.cs it automatically did this Embarrassed [:$]  Oh well, at least this little app didn't take long!

cds replied on Sunday, December 17, 2006

Hey, you probably learned some new stuff along the way, which I consider is always worthwhile!

Copyright (c) Marimer LLC