I can feel that something is wrong with my code. Here's an excerpt where I think the code smell is...
internal Hazen(IRawDataReceiver rawDataReceiver) : base(rawDataReceiver)
{
SerialPortConnection = rawDataReceiver as SerialPortConnection;
}
private SerialPortConnection _serialPortConnection;
[DataMember] private SerialPortConnection SerialPortConnection
{
get
{
return _serialPortConnection;
}
set
{
if(value != null)
{
_serialPortConnection = value;
RawDataReceiver = value;
}
}
}
So basically, Hazen
inherits from an abstract class. Notice how I pass the rawDataReceiver
to its base class
when Hazen
's constructor
is called. But during deserialization
, the constructor
is bypassed, thus I included the RawDataReceiver = value
to the set
accessor of the DataMember property
. That is where the code smell is. Is there a better way to do it and am I violating any good practices (such as DRY
)?
As regards to unit testing
. As you can see, Hazen
has a property
of type
SerialPortConnection
as opposed to having it as IRawDataReceiver
since you can't serialize
an interface
. Now, I can still test this class since the base class
exposes everything I need for testing. But going back to the previous paragraph, I feel like I'm doing something wrong here.
EDIT: Here's a snippet of the base class
internal TideDevice(IRawDataReceiver rawDataReceiver)
{
RawDataReceiver = rawDataReceiver;
}
private IRawDataReceiver _rawDataReceiver;
protected internal IRawDataReceiver RawDataReceiver
{
get
{
return _rawDataReceiver;
}
protected set
{
_rawDataReceiver = value;
RawDataReceiver.RawDataReceived += RawDataReceiver_RawDataReceived;
}
}
EDIT: I have extracted the Task
to another method to remove the responsibility of the set
accessor. But I'm still in a roadblock on fixing the code smell I initially stated. I editted the code to reflect what I have now.
EDIT2: This code is working really fine whether in unit test
and integration test
, so I'll leave it as is until someone points out that there is something wrong here or until a problem arises. Thank you very much!
mock
theIRawDataReceiver
since thebase class
has thattype
as its member. So it kind of complements the restriction on thesubclass
with respect toserialization
\$\endgroup\$Task
within theset
accessor too. \$\endgroup\$