English 中文(简体)
Elegant design of simulating a read-only object
原标题:

I am currently developing an GUI to an embedded system. (I am using GUI to descripe my app opposed to interface to avoid confusion with the progamatic meaning)

Context

I have created a class which simulates the embedded system. I do all my communications through a Connection class I have designed which can communicate via TCP/Serial or a virtual connection between two Connection objects.

All this works nicely and as expected, my simulated object and GUI communicate via the virtual connection.

I have a Diagnostic packet which is sent from the device (real or simulated) and read by the GUI. This fits nicely into a class which I have called ControllerDiagnostic.

Question

My question relates to the ControllerDiagnostic class. On the GUI side all properties should be read-only (DeSerialization is done via reflection on the private fields directly). On the simulation side they clearly need to be mutable. So how do I handle this elegantly? I ve considered:

  • Creating a completely new class which mimics ControllerDiagnostic but exposes setting of the fields.
  • Creating all fields in ControllerDiagnostic protected then inheriting into a private nested class which provides accessors to these fields within the simulation class.
  • Creating an assembly with just two classes and using an Internal setter.
  • Leave the properties as read/write and document the GUI should not modify them.
  • Re-writing C# so I could use friend in the same way I can in C++.

None of these seem ideal to me.

Thanks

问题回答

You could write an interface which exposes all properties of your class with getters and setters and a second interface which only exposes the getters for all your properties. Then have a single class implement both interfaces and use the getter-only interface in the GUI.

In code that could look somewhat like this:

public interface IReadWrite
{
    string SomeString { get; set; }
}

public interface IReadOnly
{
    string SomeString { get; }
}

public class TestClass : IReadWrite, IReadOnly
{
    public string SomeString { get; set; }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签