Custom Rule: Don't Allow Certain Methods to be used

Jul 4, 2011 at 5:58 PM

We are using the Repository design pattern in our application and using CodeSmith to auto generate the code.  The code it generates "fits" perfectly into our overall design philosophy except for the code that selects data from the DB.  We are using custom classes to handle selects.  I wont bore anyone by going into the particulars.

I've already come across one of my developers NOT doing what he was told, aka. ONLY using the custom select methods.  Instead he has used some of the auto generated GetAll() and GetByPrimaryKey() methods. Therefore, I'm trying to add some custom style cop rules to flag these cases and display a warning.  So here is the basic pattern I would be looking for:

repo.DB.dbo.DBTableName.GetAll() or repo.DB.dbo.DBTableName.GetByPrimaryKey() and these would both appear (I'm guessing) as statements within Elements?   but obviously there are all kinds of ways to look for certain ElementTypes or Expression tokens or Fields, etc. etc. and I'm a little confused what I want to do and look for.

Any help would be appreciated, including small sample code.

Jul 6, 2011 at 11:30 PM

Such a rule is much better suited, in my opinion, to a custom FxCop (Code Analysis) rule. StyleCop is geared for style and formatting issues at the source level, whereas FxCop is perfect for finding problems in the compiled code.