为每个域对象创建一个接口是个好主意吗?

发布于 2024-08-31 18:25:30 字数 190 浏览 3 评论 0原文

我只是查看了一个使用 nHibernate 的现有项目的源代码,发现为每个实体类创建了接口。例如,ICustomer 代表Customer 类。我只是想知道这种模式的优点是什么,因为 ICustomer 主要包含属性和很少的方法。

I was just looking into the source code of an existing project which uses nHibernate and found that there are interfaces created for each entity classes. E.g ICustomer for Customer class. I was just wondering what can be the advantage of this pattern as ICustomer contains mainly the properties and very few methods.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静赏你的温柔 2024-09-07 18:25:30

我会说不。接口将行为与实现分开,以便可以在不影响接口客户端的情况下更换后者。

如果您的域对象不需要不同的实现,我认为不需要接口。仅当需要动态代理生成或方面或更改实现时才引入它们。

I would say no. Interfaces separate behavior from implementation so that the latter can be swapped out without affecting clients of the interface.

I don't see a need for interfaces if your domain objects won't require different implementations. Only introduce them if dynamic proxy generation or aspects or changing implementations are necessary.

末蓝 2024-09-07 18:25:30

我并不反对之前的评论……尽管如此,结合控制反转(IoC)和依赖注入等模式,它使得将这些层彼此隔离变得更加容易。这简化了单元测试、模拟,并且还可以导致更松散耦合的架构。这仍然可以在没有接口的情况下实现,尽管您需要确保不密封您的类并使成员虚拟,这样您仍然可以模拟,生成可拦截的代理等...最后,使用接口迫使您放弃与任何相关的假设一种具体的实现,而不是关注接口定义所代表的契约。

I don't disagree with the previous comments... Although, combined with patterns like Inversion of Control (IoC) and Dependency Injection it makes isolating such layers from each other much easier. This simplifies Unit Testing, Mocking, and can also lead to more loosely coupled architectures. This can still be acheived without interfaces although you need to make sure you do not seal your classes and make members virtual so you can still mock then, generate interceptable proxies, etc... Finally, using interfaces forces you to drop assumptions related to any one concrete implementation and instead focus on the contract represented by the interface definition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文