ReSharper destabilizing my runtime? Huh?

I’m adding some new features to an older ASP.Net Web Forms app we have.  Fortunately I can take advantage of .Net 3.5, though not MVC because it’s on a Windows 2000 server with IIS 5.  Anyway, this allows me to use LINQ, which has been incredible.  Creating the mapping and using LINQ queries is leaps and bounds more convenient and less prone to error than our older data readers and such we used in the past.

I am more in the TDD camp and like to manage my own data bindings so I don’t like to use the LINQ model designer.  Instead of dragging and dropping tables onto the designer and letting LINQ create the classes for me in the background I create my own concrete classes and then put attributes on the classes themselves.  I know, some may not like putting attributes in their code because they argue that it makes their objects dependent on the database framework rather than being completely database agnostic.  However, 100% of our apps make use of Microsoft SQL and I don’t see that ever changing in the near future.  So, rather than go through the overhead and complexity of a truly database agnostic ORM setup I’ll happily add a few attributes to my code that plainly show exactly what’s going on.

In this particular case we have parents and students that will be filling out a survey based on when they graduated from our programs.  Because we serve multiple districts, each with multiple schools, I ask the user to select which district and school they graduated from.  Thus, I have a SchoolDistrict class which contains a List of School classes.  In LINQ you can map a relationship using the Association attribute.  I got the details and examples from Microsoft’s MSDN article here: http://msdn.microsoft.com/en-us/library/bb386950.aspx

Here was my initial code for SchoolDistrict:

1: using System.Collections.Generic;
2: using System.Data.Linq;
3: using System.Data.Linq.Mapping;
4:
5: namespace App_Code.Entities
6: {
7: [Table(Name = " vw_Table_D_Current_SchoolDistricts ")]
8: public class SchoolDistrict
9: {
10: private EntitySet<School> Schools;
11:
12: [Column(IsPrimaryKey = true )]
13: public string districtCdsCode { get ; set ; }
14:
15: [Column]
16: public string name { get ; set ; }
17:
18: [Association(Storage = " Schools ", OtherKey = " districtCdsCode ")]
19: public List<School> schools
20: {
21: get { return new List<School>(Schools); }
22: set { Schools.Assign( value ); }
23: }
24: }
25: }

 

Here was my initial code for School:

1: usingSystem.Data.Linq.Mapping;
2:
3: namespaceApp_Code.Entities
4: {
5: [Table(Name = " vw_Table_D_Current_Schools")]
6: public classSchool
7: {
8: [Column(IsPrimaryKey = true)]
9: public string schoolCdsCode { get ; set ; }
10:
11: [Column]
12: public string name { get ; set ; }
13:
14: [Column]
15: public string districtCdsCode { get ; set ; }
16: }
17: }

 

Everything worked great and I was rolling.  Then I had ReSharper clean up a few things and I took some of its suggestions on optimizing my code.  I made a few other changes and, then all of a sudden, I got this when I ran the code:

System.Security.VerificationException: Operation could destabilize the runtime.

Ugh!  What’s that???!!

After a lot of Googling I had come up with nothing.  Most of the fixes I saw related to mapping IEnumerables to IQueryables, covariance vs contravariance, etc.  Nothing seemed to fit my particular scenario and none of the fixes worked.  I went over my code with a fine tooth comb making sure my mappings were still correct, making sure the capitalization in my attributes were not fouling things up, etc.  I came up with nothing.

 

So, I went back to square one.  I looked at the Microsoft example again and I found one modifier that was different.  ReSharper had noted that I could mark my Schools EntitySet in my SchoolDistrict class as readonly.  I removed this modifier and, voila, everything worked.  Sure enough, ReSharper again started suggesting that I mark this as readonly.  I did and my code broke again.  I had found the culprit.

I had not seen this mentioned anywhere on Google and I guess, in hindsight, most people wouldn’t mark this as readonly

I don’t always take ReSharper’s suggestions but this is the first time that ReSharper actually broke my code.  :(  Truth be told, ReSharper is a tool and it’s only as good as the one wielding it.  If I let ReSharper perform invalid operations that’s not ReSharper’s fault but my own.  However, I still didn’t like that I may not remember this key piece of information in the future.

So, I told ReSharper to ignore this in the future and wrote a comment as to why.  That cluttered up my code and it now looks like this:

1: // ReSharper disable FieldCanBeMadeReadOnly.Local
2: // If set to read only this causes an "Operation could destabilize the runtime" exception when the query is evaluated
3: privateEntitySet<School> Schools;
4: // ReSharper restore FieldCanBeMadeReadOnly.Local
5:

Wow, that is ugly!  I hate having to put comments in my code that are because of dependencies to frameworks.  Now I have a comment in my code that’s dependent on an IDE tool, not even something required for the app itself!!!  That’s just plain ugly and a major code smell if you ask me.  However, that’s just the way it is for now.  The app works and I’ll go on with my life.  It’s not perfect but I don’t want to spend a day figuring out a better way.  If someone posts a better suggestion or I find something in the future then I’ll revamp it, but for now, I’ll get the app out the door and life will go on.

I hope that this helps someone else out there that may run into this issue.

In the end ReSharper is one of the best tools I have in my toolbox, number 2 right behind Visual Studio itself.  It’s incredible and I can’t imagine coding without it.  It truly has helped make me a better developer than any other tool I’ve used to date (again, after VS).

Take care all and happy developing!

One Reply to “ReSharper destabilizing my runtime? Huh?”

Comments are closed.