开发者

Pattern for public and internal interfaces on business objects

I would like to design a domain model using internal interfaces and then create restricted public interfaces (for users of the assembly)

Whilst this is possible in CSharp I keep running into messy code related to parallel interfaces:

public interface IAccount { IList<ITran> Transactio开发者_开发技巧ns { get; } }
internal interface IAccountInternal : IAccount { IList<ITranInternal> Transactions { get; } }
internal class Account : IAccountInternal { }

here the implementation of Account gets very messy as there are many collections which need dual interfaces etc.

Furthermore I would like to guarantee that the public interfaces are implemented using the internal interfaces (as opposed to directly accessing the concrete classes)

This must be a common scenario, can anybody recommend a clean approach?


using generic interface covariance in dot net 4.0 I managed to clean things up (note the "out" modifier on the T parameter)

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

this lets me return collections of internal objects typed as public objects because:

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

now this works:

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


This seems like a bad idea, that model seems to be very complex.

My first thought is to reduce the need for internal interfaces. If you're using them for the purpose of unit testing, then I'd recommend only testing the external (public) interface, and essentially forgoing the use of internal interfaces altogether.

Other than that, if you can elaborate on the need for the internal domain model it might help others answer your question better.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜