开发者

How do you test for a renamed partial method?

开发者 https://www.devze.com 2023-03-16 17:58 出处:网络
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 ho

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 m开发者_StackOverflow社区eans 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?


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)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号