New answers tagged

3

Given a variable which needs to have a scope beyond a single function call, yet be accessible by only one function, I believe it's okay to not introduce a new scope just for that one function as long as you don't make the variable global. In other words, I'd put this variable at module scope. You should already have some kind of module system which prevents ...


1

If your goal is to limit variable scoping, you are making the issue more difficult than necessary. Consider this function that achieves the same goal with a much more limited scope, both for the internal variables and the function complexity: function doIt(x) { var theAnswer = 42; return x * 42; } It does the same thing yours does, but without the ...


0

Your method GetHousesWithinSameDistrict makes use of a house repository, and an address repository, and has its own logic to select amongst the entities returned by these repositories. As such I would suggest the simple solution is to make use of mocking, and mock those repositories to return entities of the appropriate type, such that you can test your ...


0

The simple answer is: use reflection. For example, the following code creates an Address object and sets District: var address = new Address(); typeof(Address).GetProperty("District").SetValue(address, "A district", null); How you do that in your test code depends on what _addressRepository is (eg, is it declared as an interface?) and how easily it is ...


7

Usually you should do it this way: This is the most canonical way of solving your problem. In general you add behaviors and states when subclassing


5

At the moment, I've inherited the class as normal and overridden Member3 and Member4 to throw NotSupportedException and made Member5 read-only and return 0 all the time. I'm not convinced this is the best idea as Member3 and Member4 are irrelevant to ChildClass3. It isn't the best idea: this is a classic example of violating the Liskov Substitution ...



Top 50 recent answers are included