如果实体实现的接口? [英] Should entities implement interfaces?

查看:145
本文介绍了如果实体实现的接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我个人没有我的实体实现的接口。对于工作类,我不会有 ITask 是刚上定义相同的属性。



我见过它做了几次了,所以我想知道这个建议的来源,以及什么好处,你从它那里得到。



如果您使用的是ORM那么说:我可以改变我的数据访问是文不对题的说法,所以有什么其他原因是有这样做呢?



更新:结果
好​​点在约 INotifyPropertyChanged的所作的评论。那不是我的观点,但 - 我说的是有这样的:

 公共接口ITask 
{
INT标识{搞定;组; }
字符串描述{搞定;组; }
}

公共类任务:ITask
{
公众诠释标识{搞定;组; }
公共字符串描述{搞定;组; }
}


解决方案

我就沿着这条道路一次(接口值对象)。这是一个皇家疼痛的背部,我建议反对。它常见的参数是:



嘲讽:
他们是值对象。元老院嘲笑。加嘲弄最终成为一个大的痛苦比任何写一个生成器(在Java中)或使用C#中的命名参数的东西。



只读访问量:
我必须承认,我还是喜欢做的东西在默认情况下不可变的,只有使其可变的,如果绝对必要。



隐藏功能:
一般范围已覆盖这一块我


I personally don't have my entities implement interfaces. For a Task class I wouldn't have ITask that just had the same properties defined on it.

I've seen it done a few times though, so I'm wondering where that advice comes from, and what benefits you get from it.

If you're using an ORM then the argument that says "I can change my data access" is irrelevent, so what other reason is there for doing this?

UPDATE:
A good point was made in the comments about INotifyPropertyChanged. That wasn't my point though - I'm talking about having something like this:

public interface ITask
{
    int Id { get; set; }
    string Description { get; set; }
}

public class Task : ITask
{
    public int Id { get; set; }
    public string Description { get; set; }
}

解决方案

I went down this road once (interfaces for value objects). It was a royal pain in the backside, I recommended against it. The common arguments for it are:

Mocking: They are value objects. Nought to mock. Plus mocking ends up being a large pain than either writing a builder (in Java) or using the named arguments stuff in C#.

Readonly views: I must admit I still prefer to make something immutable by default, only making it mutable if absolutely required.

Hidden functionality: Generally scope has covered this one for me.

这篇关于如果实体实现的接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆