1

Bit of a weird one.. I've just implemented the Unit Of Work pattern in my MVC project and was just debugging it to ensure it works as intended. However, I've noticed a bit of a quirk with either my code, the pattern or visual studio..

Here an example of a repository reference I have in my UoW:

enter image description here

The UoW is created and disposed on each request, so one would assume the private field m_DemonstrationRepository would be null the first time the property is accessed. If I stick a breakpoint on and look at m_DemonstrationRepository it is infact null as shown below:

enter image description here

This is where it gets weird.. If I now hover over the public property DemonstrationRepository it claims it's no longer null (hovering over the public property previously would show it was null).. as shown below:

enter image description here

And finally.. if I now hover back over m_DemonstrationRepository it is no longer null!

enter image description here

As a further test I added some code to the constructor of DemonstrationRepository and put a break point on that to see when it's initialised. The breakpoint will hit every time as expected if I remove all other breakpoints, but if I have a breakpoint in the getter (like the pictures) and I hover over DemonstrationRepository first then the breakpoint inside the constructor won't hit! If I repeat the same request, hit the breakpoint in the getter but DON'T view the public property and just press continue, then the object is initalised as expected and the constructors breakpoint hits.

I hope that's clear enough, this has got me spinning in circles..

Thanks

Murphybro2
  • 2,207
  • 1
  • 22
  • 36
  • 1
    It's almost like hovering over the property calls the `Get` method of the property while ignoring breakpoints. This is similar to enumerating the result of an IEnumerable while debugging. – Ryan Searle Aug 23 '17 at 11:04
  • It does seem that way, I have a few properties for repositories in the UoW class and I can just hover over them all one by one and initialise them! – Murphybro2 Aug 23 '17 at 11:06
  • 1
    Upon further reading it does exactly this, you can disable this in VS but you will no longer be able to view the values of your properties. After all the debugger has to obtain the value somehow. https://stackoverflow.com/questions/32574840/how-to-prevent-the-debugger-from-calling-some-source-code-when-paused – Ryan Searle Aug 23 '17 at 11:08
  • Well how about that... thanks pal.. I was googling for ages but couldn't quite word it right! Wish I'd have seen that before! – Murphybro2 Aug 23 '17 at 11:19
  • 1
    No problem! glad this has confirmed my suspicions. – Ryan Searle Aug 23 '17 at 11:25

0 Answers0