views:

41

answers:

1

I have found myself countless times reorganizing our solution (72 + projects), in efforts to reduce code. A lot of times it involves pulling out common types/libraries and moving them around.

It always involves a reptitive compiler error so i am having to fix the same compiler error in a lot of different places. I am looking for a guide to the VS api so i can do some code generation/automated fixing.

Even if you dont have the exact solution, can you please point me to where i should look? Is there a compiler api? refactor api? Any suggestions on how i can do either one?

Use Cases:

So first question (easiest of the two):

Automated fixing when i move a file from one project to another. This process would be to find the error

"The type or namespace name 'MovedClass' could not be found.

Then remove the old namespace, add the new, and add the project reference.

Case Two... refactoring to a pattern.

Example:

Original Base VO Class:

   public class BaseVO{
      public AuditInfo { get;set;}

   }
   public class BaseKey{

   }

All of our Business Objects(each in individual class).

   public class ExampleBusinessVO : BaseVO{
      public AuditInfo { get;set;}
      public ExampleBusinessKey BusinessKey { get;set;}
   }

   public class ExampleBusinessKey{


   }


Now assume we want to add an explicit link between BaseVO and Base Key, and force all to implement Equals/HashCode

New Base VO Class:

   public abstract class BaseVO<TKey>{
      TKey BusinessKey{get;set;}
      public abstract override bool Equals(object obj);          
      public abstract override int GetHashCode();

   }
   public class BaseKey{

   }

All then i must cascade these changes to all VOs. (ouch )

   //Add generic
   public class ExampleBusinessVO : BaseVO<ExampleBusinessKey>{
       //Remove old business key  

        //Implement methods
       public override bool Equals(object obj){

       }
   }

   public class ExampleBusinessKey: BaseKey{


   }

Now i recognize i can't fix all of the errors, but it would be nice to be able to write something to check a class for the compile errors. Then i could develop a pragmatic solution:

For each of the VOs if error: "Using the generic type ... requires 1 type argument", then read the class file and find the class that implements BaseKey, and then add it to the template method.

Please note i am looking for an automated solution not a refactoring tool

+3  A: 

Personally, I would recommend ReSharper as it provides functions for most of the refactoring work you might need.

Moving types (even between projects), renaming types, extracting methods, reordering method signatures, etc...

It is a paid solution, but well worth your money (or your employer's). You can download a free trial from the site that is fully functional to get a feel for it.

EDIT:

Ok, so I just tested this out, and ReSharper will take care of all the work needed to move one type to another project, but it doesn't automatically add references for you. However, it does give you fair warning that new references will be needed, and adding those references is pretty darn simple using ReSharper:

Add Reference With Resharper

Josh
So if i move a type from one project to another it will find and add references to new project?
Nix
I don't do this very often, but I am going to assume yes. I say this because ReSharper is smart enough to give you the option of adding a reference to a type that hasn't been referenced the first time you use it. So if I am in Project A, and use a type from Project B without a reference to that project, ReSharper will say "Hey, do you want to add a reference to Project B"
Josh
if i have to click through all files and add the reference, its still a pain.
Nix
resharper is heavy, and it does not automate anything. I am looking to be able to write something to handle refactoring.
Nix