简体   繁体   English

如何从另一个房产获得房产价值?

[英]How to get property value from another property?

I have the following interface: 我有以下界面:

public interface IReport
{
    int ReportId {get; set;}
}

I have an entity that has identity column property: 我有一个具有标识列属性的实体:

public int PaymentReportId {get; set;}

I need the PaymentReport to implement IReport 我需要PaymentReport来实现IReport

Inside my PaymentReport.cs I did: 在我的PaymentReport.cs里面,我做了:

public int PaymentReportId {get; set;}

public int ReportId {
    get => PaymentReportId; 
    set {} //no need to ever set this;
}

Otherwise the compiler was complaining about no setter implemented. 否则编译器抱怨没有实现setter。

Is there a cleaner way to do this? 有更清洁的方法吗?

I removed, the set from the ReportId of IReport and then implemented in the class. 我从IReport的ReportId中删除了该集合,然后在该类中实现。

public interface IReport
{
    int  ReportId { get;  }
}

public class PaymentReport : IReport
{
    public int PaymentReportId { get; set; }
    public int ReportId
    {
        get => PaymentReportId;
    }
}

If you try to adhere to SOLID there is principle called interface segregation. 如果您尝试遵守SOLID,则称为接口隔离原则。

The interface-segregation principle (ISP) states that no client should be forced to depend on methods it does not use. 接口隔离原则(ISP)规定不应强迫客户端依赖它不使用的方法。

With your approach the principle is obviously violated as class PaymentReport does have property setter which is essentially not needed. 根据您的方法,显然违反了原则,因为类PaymentReport确实具有基本上不需要的属性设置器。


Consider to split the IReport to IReportRead and IReportWrite and implement only what is necessary. 考虑将IReport拆分为IReportReadIReportWrite并仅实现必要的操作。

public interface IReportRead
{
    int ReportId { get; }
}

public interface IReportWrite
{
    int ReportId { set; }
}

public class PaymentReport : IReportRead {//...}

That way you have clear abstraction and you are not polluting the implementation. 这样你就有了清晰的抽象,而且你没有污染实现。

As others said, the cleanest thing would be to change the interface or split into several interfaces. 正如其他人所说,最干净的事情是改变界面或分成几个界面。 Here is an example of splitting interface into two and use them in both the get scenario and the get, set scenario: 下面是将接口拆分为两个并在get方案和get, set方案中使用它们的示例:

interface IFooGet
{
    int Id { get; }
}

interface IFooSet
{
    int Id { set; }
}

public class FooGet : IFooGet
{
    public int Id { get; }
}

public class FooGetSet : IFooGet, IFooSet
{
    public int Id { get; set; }
}

If that is not possible (maybe you don't own the code for the interface?) you might throw an exception if someone tries to call the property. 如果那是不可能的(也许你不拥有接口的代码?)如果有人试图调用该属性,你可能会抛出异常。

class PaymentReport : IReport
{
    public int PaymentReportId {get; set;}

    public int ReportId {
        get => PaymentReportId; 
        set => throw new NotSupportedException();
    }
}

Just having a empty body in the set may sometime lead to error hiding https://en.wikipedia.org/wiki/Error_hiding if some code in the future tries to call setter and makes the assumption that the setter actually does something meaningful 只是在set有一个空体可能有时会导致错误隐藏https://en.wikipedia.org/wiki/Error_hiding如果将来某些代码试图调用setter并假设setter实际上做了一些有意义的事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM