views:

151

answers:

5

I have a class that uses filesystem entities to manipulate data. We have several methods specifically designed to (attempt to) cope with some of the issues we face with this approach (file locking, non-existent files, etc.). Ideally I'd like to be able to issue a warning if another developer attempts access the filesystem directly via System.IO rather than using the helper methods.

Is this possible? The behaviour I'm looking for is to effectively mark methods such as File.ReadAllText() as if they were obsolete, but only within this project (NOT solution-wide).

I've done some digging around, and it looks like my only option is "tell them to make sure they use your methods". I'm hoping someone can give me a different, and more helpful answer. :)

--EDIT-- The suggestions of a custom StyleCop or FxCop rule are good, but unfortunately impractical in this scenario (not every developer in the department uses these excellent tools), and the legitimate methods that do the file access do use System.IO. Adding "ignore" attributes to the legit methods is a dangerous idea, too. If someone sees how I've "broken" my own rule, they'll likely copy the attribute to their own method.

+11  A: 

Use a static analysis tool (such as StyleCop or FxCop) with a rule that captures "Do not use System.IO directly." Then integrate it as part of your automated build process and throw up if someone does try to use System.IO directly. No one likes to break the build.

Jason
I like to break the build.
Gabe Moothart
That's great. Hope you like fixing it, too.
Steven Sudit
I like this idea. Unfortunately we (as an organisation) do not yet enforce running StyleCop or FxCop on builds (we should, and I have been pushing for it for a while, but change is a slow process) I guess I could modify the idea and add it to my own ruleset, but then I become a single point of QC for this module.
ZombieSheep
The problem I've found is that all static analysis tools err on the side of caution, so a real app is likely to contain a file with commands to override specific complaints. This leaves you in the position of needing to check the overrides for validity instead of just the code, which moves the problem around without shrinking it.
Steven Sudit
@ZombieSheep: Do you have any code analysis as part of your build process? Could you write a simple script as part of the build process that searches for `System.IO` and throws up if found? Basically hand roll your own static analysis tool for this one rule. That would not be hard and is a baby step towards adoption of a bona fide static anaylsis tool.
Jason
Don't get me wrong, we do use StyleCop and FxCop - but it is by no means across the board. The issue is the throny one of deciding which rules we can safely ignore, and which we must stick by
ZombieSheep
@ZombieSheep: I suggest leaving all the rules on and having the developers disable specific violations that they believe are mistaken. This list then has to be reviewed.
Steven Sudit
I'm going to leave this question open for a few days - there's some good discussion going on. In the mean time, I've just had to resort to the old faithful methods of putting a HUGE COMMENT BLOCK at the top of each file. It'll do for now, since there's just me working in it at the moment.
ZombieSheep
@ZombieSheep: Regardless of what else you do, a comment is a good idea.
Steven Sudit
+1  A: 

You can write custom analysis rule for FxCop/Visual Studio Code Analysis and run these as part of your automated build.

Anton Gogolev
Yep, or make it a requirement for check-in, in TFS.
Steven Sudit
A: 

Hmm. Not tried this myself, but how about forcing people to use your custom file handling classes, by using a namespace alias that "hides" the genuine System.IO. If I remember rightly these are applied at a project level.

ChrisBD
This sounds a bit like the "namespace poisoning" idea. The problem is that there's probably some way to specify the real System.IO even when a fake one blocks the most direct path, so this prevents *accidentally* bypassing the custom file I/O layer without stopping someone who really wants to do it. This includes those who thought the problem was a bug, not an intentional feature (or will claim this).
Steven Sudit
Ah yes I hadn't noticed your comment. Is that what you meant? I don't see how you'd stop people intentionally typing everything out longhand in their code to access System.IO namespace without it though.
ChrisBD
Check out my next comment, where I suggest simply removing the assembly with System.IO from the references list.
Steven Sudit
Still, easy for people to add it back in thinking that it was a bug or had been deleted accidentally.
ChrisBD
A: 

Not sure if either of these suggestions are valid as I've never done them, but some food for thought:

Isn't this what "Enterprise Templates" are designed for? Don't they allow you to craft a policy file that restricts the allowed project references?

Alternatively, while not foolproof, could you add a pre-build event to the project that throws a warning if System.IO is referenced?

Jacob G
A: 

Can you add some custom functionality to a source-control commit hook? It won't find existing violations (if there are any) unless those files are changed but should detect new uses?

Any good?

John