4

I'm using Linq to SQL, which generates partial classes and partial methods. You then extend that generated code by implementing your your customizations manually in another partial class. One of the hooks L2S gives you is the ability to implement partial methods that get called when a property changes. For example, if you have a property named "MyProp", then you can implement a partial method like so:

' Given to you in the generator
Partial Private Sub OnMyPropChanged()
End Sub

' Manually implemented in my custom class
' I cannot specify that this is an implementation of a Partial, even though it is...
Private Sub OnMyPropChanged()
   Console.WriteLine("My prop changed... do something here")
End Sub

The problem I'm having is the name of "MyProp" has now changed to "MyNewPropName", so now the partial in the generator creates Partial Private Sub OnMyNewPropNameChanged(), but my version of the partial method still has the old name. Effectively, I now have an orphaned private method that never gets called which means my code is broken at runtime. How would you test for something like this, or even better - is there a way to specify that my version of OnMyPropChanged() is an implementation of a partial method such that the I get a compile time breakage if there isn't a corresponding partial in the generated code?

mattmc3
  • 17,595
  • 7
  • 83
  • 103

2 Answers2

2

By using static anlysis (code analysis) you will get a warning/error when your code contains any internal (private, freid, internal) members that are never accessed or only ever set. this may help finding such constellations. (IIRC the concerning error code is CA1811)

eFloh
  • 2,098
  • 20
  • 24
0

In my opinion I dont think you can.

In changing the name of a property in the designer you are causing the auto generated code to be recreated.

The best approach is to design your entities before you start implementing business logic. Create unit tests in visual studio that test for the partial implementation being executed. The unit testing in visual studio will even give you code coverage statistics.

Hope this helps

jaywayco
  • 5,846
  • 6
  • 25
  • 40
  • You say "The best approach is to design your entities before you start implementing business logic", but saying that you'll have a perfect up front design or that things will never change is just not very realistic. You also mention unit testing, but can you elaborate on how you would test whether a private partial method is called? – mattmc3 Jun 30 '11 at 19:04
  • These methods are designed to enable you to change the state or customize your business object in some way. So in your implementation of this method you will know the effect or outcome of calling that method. In your unit test you will know what action will trigger the extensibility/partial method and you know what result your method will have. Test for the result – jaywayco Jul 01 '11 at 12:27