patterncsharpModerate
Empty Interface usage - Is this a code smell?
Viewed 0 times
thisemptyinterfaceusagecodesmell
Problem
I've recently made a set of interfaces/classes to work with converting a spreadsheet into an object but I utilise an empty interface in my design:
I have my interface which defines what a spreadsheet object should have:
As you can see it take a type parameter which must implement IParsedRow which is my empty interface:
Here is an abstract class declaration that implements the interface:
One of the important parts of this class is the
Any concrete implementation of
Here is my abstract class in full (note I use NPOI as a wrapper to easily parse a spreadsheet. NPOI is an open source spreadsheet framework for .NET):
```
///
/// Abstract, generic class that only accepts a type parameter which implements IParsedRow for use in its internal row collection.
///
///
public abstract class AbstractParsedSpreadsheet : IParsedSpreadsheet, IEnumerable where TEntity : IParsedRow, new()
{
public int Columns { get; set; }
public int Pages { get; set; }
pub
I have my interface which defines what a spreadsheet object should have:
public interface IParsedSpreadsheet where TEntity: IParsedRow
{
int Columns { get; set; }
int Pages { get; set; }
Dictionary Map { get; set; }
List ColumnHeaders { get; set; }
List RowList { get; set; }
List ParseSheet(IFileStorage storage);
List ObtainColumnHeaders(IFileStorage storage);
}As you can see it take a type parameter which must implement IParsedRow which is my empty interface:
public interface IParsedRow
{
//Marker Interface
}Here is an abstract class declaration that implements the interface:
public abstract class AbstractParsedSpreadsheet : IParsedSpreadsheet, IEnumerable where TEntity : IParsedRow, new()One of the important parts of this class is the
Map. This has a list of column headers that should appear in a spreadsheet and also the index of the column they appear in.Any concrete implementation of
IParsedRow will have a number of properties each of which must be named after a column header in the spreadsheet it will represent. I use the Map along with reflection to determine that the spreadsheet headers match up to the TEntity's properties exactly to let me know that the uploaded spreadsheet is valid for the map it is being put against.Here is my abstract class in full (note I use NPOI as a wrapper to easily parse a spreadsheet. NPOI is an open source spreadsheet framework for .NET):
```
///
/// Abstract, generic class that only accepts a type parameter which implements IParsedRow for use in its internal row collection.
///
///
public abstract class AbstractParsedSpreadsheet : IParsedSpreadsheet, IEnumerable where TEntity : IParsedRow, new()
{
public int Columns { get; set; }
public int Pages { get; set; }
pub
Solution
It may appear to some as code smell, but it's a practice in use in the .net framework defined as 'marker interfaces'.
Specifies that the target HTTP handler requires only read access to
session-state values. This is a marker interface and has no methods.
So, to answer your question, is this a code smell? perhaps it depends who you ask. In my opinion, if it's good enough 'pattern' for the .net framework, it's good enough for me.
http://msdn.microsoft.com/en-us/library/system.web.sessionstate.ireadonlysessionstate(v=vs.110).aspx
IReadOnlySessionState is one of these and as per the documentation:Specifies that the target HTTP handler requires only read access to
session-state values. This is a marker interface and has no methods.
So, to answer your question, is this a code smell? perhaps it depends who you ask. In my opinion, if it's good enough 'pattern' for the .net framework, it's good enough for me.
http://msdn.microsoft.com/en-us/library/system.web.sessionstate.ireadonlysessionstate(v=vs.110).aspx
Context
StackExchange Code Review Q#43288, answer score: 15
Revisions (0)
No revisions yet.