gpt4 book ai didi

c# - 业务对象的公共(public)和内部接口(interface)模式

转载 作者:太空宇宙 更新时间:2023-11-03 14:17:11 24 4
gpt4 key购买 nike

我想使用内部接口(interface)设计领域模型然后创建受限制的公共(public)接口(interface)(为程序集的用户)

虽然这在 CSharp 中是可能的,但我一直遇到与并行接口(interface)相关的困惑代码:

public interface IAccount { IList<ITran> Transactions { get; } }
internal interface IAccountInternal : IAccount { IList<ITranInternal> Transactions { get; } }
internal class Account : IAccountInternal { }

这里 Account 的实现变得非常困惑,因为有许多集合需要双接口(interface)等。

此外,我想保证公共(public)接口(interface)是使用内部接口(interface)实现的(而不是直接访问具体类)

这一定是一种常见的情况,有人可以推荐一种干净的方法吗?

最佳答案

在 dot net 4.0 中使用通用接口(interface)协变我设法清理了一些东西(注意 T 参数上的“out”修饰符)

public interface IListReadOnly<out T> : IEnumerable<T> {
int Count { get; }
T this[int index] { get; }
}//class

这让我可以返回类型为公共(public)对象的内部对象集合,因为:

public interface IPublic { }
internal interface IPrivate : IPublic { }

现在可以了:

private IListReadOnly<IPrivate> list = ...
public IListReadOnly<IPublic> List { get { return list; } }

关于c# - 业务对象的公共(public)和内部接口(interface)模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6320977/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com