2

I have classes that inherit from an abstract class FlowData. FlowData implements an interface IFlow

My problem, I have a factory class that parses a file and returns a list of IFlow type objects depending on what kind of IFlow objects can be created from the file. I have two different kinds of classes inheriting from FlowData and I want to make sure each object contains a static definition for a property FlowType. There is scope to add many other classes that inherit from FlowData

The factory logic is as follows

  1. Find lines in file beginning with FlowType key word
  2. Iterate through IFlowData classes in FlowTypes namespace in class library
  3. If FlowDataClass.FlowType ==FlowTypeKeyWord create a IFlowData object of the class

This logic relies on each class that inherits from FlowData to contain its own unique definition of FlowType. Interfaces do not support static members and I can't override a static member in the child class. Is there a clean way to ensure each child class

  1. Contains the property Flowtype
  2. Can implement a unique value for it.

At the moment I am achieving this by defining an interface IFlowType and implementing this in my classes that inherit from FlowData. My factory class creates a FlowDataClass object and checks its FlowType. I am just wondering if there is a way to do this without instantiating a FlowDataClass object Thanks

  • Have you considered creating / using an [Attribute](https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/attributes), that you can apply to each implementation to specify the `FlowType`? This by itself would not ensure uniqueness though, but you could check for uniqueness when getting / loading all the types and their applied attributes (and just make sure there is only 1 distinct `Type` for each `FlowType`, and otherwise throw an exception). – bassfader May 20 '18 at 10:13
  • You posted an edit after it was closed. If you beleieve that you have a question that is not addressed by any of the duplicates then be very specific about that part, and state why the duplicates are different. – H H May 20 '18 at 10:19
  • I have not used attributes before but this looks like it will solve my problem and give me something new to play around with. Thank you very much :D – Andrew Murphy May 20 '18 at 10:25

0 Answers0