-1

I have a series of classes that have a number of analogous Shared methods that I want to ensure are implemented in a consistent manner in each class. These Shared methods are called via reflection in various areas of code, and up to this point I’ve been keeping them in synch manually. As the number of classes grows, ensuring synchronization is becoming a bit tedious – as is the process to ensure, when I add a new Shared method to the paradigm, that it is indeed implemented in all of the classes to prevent run-time errors when reflection comes looking for them.

I’d initially thought this would be an ideal use of interfaces, but per various threads (such as Why we can not have Shared(static) function/methods in an interface/abstract class?) you cannot define Shared methods as part of an interface. I then tried to define a common base class and mark these methods MustOverride, but you cannot do that either. I’ve looked into delegates (which I’ve not used before), but it appears to allow the opposite of what I’m trying to do (namely, delegates seem to allow a common implementation of a common need from multiple classes instead of a unique implementation of a common need from multiple classes).

As such, is there an approach in VB.Net to ensure at compile-time that each of my classes implements a set of common Shared methods and functions in a consistent manner?

As a side note, the classes in question are currently all extensions of Entity Framework classes and are being blended into the EF classes.

EDIT: As a alternative to a language feature (which may not exist), I'd also be open to suggestions on any refactoring-type tools that provide this sort of functionality.

Community
  • 1
  • 1
JimMSDN
  • 484
  • 4
  • 16
  • Could template design pattern help you? "Define the skeleton of an algorithm in an operation, deferring some steps to subclasses." [link]http://www.dofactory.com/Patterns/PatternTemplate.aspx[link]. I've used it to create an "outline" of similar steps (such as validation and update), where the exact validation and update to be done differs by class. – SeraM Apr 25 '14 at 18:25
  • @phaedra Hehe...that's exactly what I'm doing. The portions that I'm using reflection to invoke (and trying to template) are indeed the small steps that handle very specific functionality. Most are less than 10 lines long. – JimMSDN Apr 25 '14 at 18:49
  • This is an XY question. Surely the only reason you are asking this is because you are fretting over the Reflection code failing at runtime without any good way to let the compiler help you avoid it. You do so by **not** using Reflection. Which is entirely the point of using an ORM framework. Like Entity Framework. Use the tool properly. – Hans Passant Apr 26 '14 at 13:08
  • @Hans In several (but not all) of the cases the templated code doesn’t touch EF, so whether I’m “using EF correctly” isn’t always a germane question. I mentioned it in passing simply to advise if it would affect possible solutions (e.g., in defining a common base class which I’d then need to get between System.Data.Objects.DataClasses.EntityObject and my own class in the inheritance hierarchy). – JimMSDN Apr 27 '14 at 12:51
  • @Hans As with most anything of interest in software engineering, I am balancing two opposing constraints: the risk of run-time errors by using focused instances of reflection, versus the risk inherent in maintaining in this particular app analogous sets of UI and business logic. Respectfully, more useful are solutions/answers (including “there’s really no way to do what you want in VB.Net”, if that’s the case) rather than simply saying “use EF properly” when that’s not really the crux of the issue. – JimMSDN Apr 27 '14 at 12:52

1 Answers1

0

Here is sort of the skeleton of code I'm using:

Public MustInherit Class AbstractUpdateItem
  Public MustOverride Function ValidateFields(ByRef dto As BusinessDTO) As Boolean

End Class

Public Class UpdateSomeFields
    Inherits AbstractUpdateItem

    Public Overrides Function ValidateFields(ByRef dto As BusinessDTO) As Boolean
      ' some code here to do the real stuff
    End Function   

End Class

When I comment out one of my Overrides Function (ValidateFields), I get a compile time error: Class x must either be declared 'MustInherit' or override the following inherited 'MustOverride' member(s): ValidateFields. So that would seem to ensure that they implement a set of methods. They aren't declared as shared, but will that do what you want?

SeraM
  • 487
  • 4
  • 10
  • Unfortunately your solution doesn't work when the method/function you are trying to template is defined as "Shared", which is the crux of the problem. If they were not Shared, I could indeed have used interfaces or (as you suggest) inheritance. – JimMSDN Apr 26 '14 at 12:27