假设我有以下课程:
class Camera
{
public Camera(
double exposure,
double brightness,
double contrast,
RegionOfInterest regionOfInterest)
{
this.exposure = exposure;
this.brightness = brightness;
this.contrast = contrast;
this.regionOfInterest = regionOfInterest;
}
public void ConfigureAcquisitionFifo(IAcquisitionFifo acquisitionFifo)
{
// do stuff to the acquisition FIFO
}
readonly double exposure;
readonly double brightness;
readonly double contrast;
readonly RegionOfInterest regionOfInterest;
}
...和一个 DTO,用于跨服务边界 (WCF) 传输相机信息,例如,用于在 WinForms/WPF/Web 应用程序中查看:
using System.Runtime.Serialization;
[DataContract]
public class CameraData
{
[DataMember]
public double Exposure { get; set; }
[DataMember]
public double Brightness { get; set; }
[DataMember]
public double Contrast { get; set; }
[DataMember]
public RegionOfInterestData RegionOfInterest { get; set; }
}
现在我可以添加一个方法Camera
来公开其数据:
class Camera
{
// blah blah
public CameraData ToData()
{
var regionOfInterestData = regionOfInterest.ToData();
return new CameraData()
{
Exposure = exposure,
Brightness = brightness,
Contrast = contrast,
RegionOfInterest = regionOfInterestData
};
}
}
或者,我可以创建一个方法,该方法需要传入一个特殊的 IReporter,以便相机向其公开其数据。这消除了对 Contracts 层的依赖(Camera 不再需要知道 CameraData):
class Camera
{
// beep beep I'm a jeep
public void ExposeToReporter(IReporter reporter)
{
reporter.GetCameraInfo(exposure, brightness, contrast, regionOfInterest);
}
}
那我应该怎么做?我更喜欢第二种,但它要求 IReporter 有一个 CameraData 字段(由 更改GetCameraInfo()
),这感觉很奇怪。另外,如果有更好的解决方案,请与我分享!我仍然是一个面向对象的新手。