.NET 系统架构设计中涉及多个模型和聚合的良好实践是什么

发布于 2024-09-01 08:38:10 字数 1757 浏览 4 评论 0原文

我正在设计一个更大的企业架构,我对如何分离模型并设计它们感到疑问。 有几点我希望得到建议: - 模型定义 - 定义模型的方式

目前我的想法是定义:

  1. 核心(域)模型
  2. 存储库 从 a 获取数据到该域模型 数据库或其他存储业务
  3. 逻辑模型将包含 业务逻辑、验证逻辑和 形式的更具体版本 数据检索方法
  4. 查看为专门格式化而准备的模型 将被解析的数据输出 不同类型的视图(网络、 银光等)。

对于第一个模型,我对使用什么以及如何定义模式感到困惑。 该模型实体是否应该包含集合以及以什么形式? IList、IEnumerable 或 IQueryable 集合? - 我正在考虑 IEnumerable 所属的不可变集合,但我想避免庞大的数据集合,并通过 LINQ 表达式为我的业务逻辑层提供访问权限,以便查询树在数据级别执行并仅检索情况下真正需要的数据就像我在数千或数十万个元素中检索非常特定的元素子集时一样。

如果我的商品有数千个出价怎么办?我不能只创建模型上的 IEnumerable 集合,然后在某些存储库方法甚至业务模型方法中检索项目列表。 它应该是 IQueryable 以便我实际上将查询从业务逻辑模型层一直传递到存储库吗? 我应该避免在域模型中使用集合吗?我应该只作废一些收藏吗?

我应该将领域模型和业务逻辑模型分开还是将它们集成?

数据将通过使用域模型类的存储库进行处理。是否应该仅使用域模型中的类(例如数据容器)直接使用存储库?

这是我的想法的一个例子: 架构图1 http://img199.imageshack.us/img199/7364/arch1p.jpg

因此,我的 Domain 对象看起来像(例如),

public class Item
    {
        public string ItemName { get; set; }
        public int Price { get; set; }
        public bool Available { get; set; }
        private IList<Bid> _bids;
        public IQueryable<Bid> Bids
        {
            get { return _bids.AsQueryable(); }
            private set { _bids = value; }
        }
        public AddNewBid(Bid newBid)
        {
            _bids.Add(new Bid {....
        }
    }

其中 Bid 将被定义为普通类。

存储库将被定义为数据检索工厂,用于将数据获取到另一个(业务逻辑)模型,该模型将再次用于将数据获取到 ViewModel,然后由不同的消费者呈现。

我将为所有聚合集合定义 IQueryable 接口,以获得灵活性并最大限度地减少从实际数据存储中检索的数据。

或者我应该使用纯数据存储实体和为业务逻辑模型定义的所有集合来使域模型“贫血”?

最重要的问题之一是,哪里有 IQueryable 类型集合? - 从存储库到业务模型的所有方式,或者根本不公开,仅公开存储库中的可靠 IList 和 IEnumerable,并处理业务模型内更具体的查询,但在存储库内具有更细粒度的数据检索方法。

那么,你觉得怎么样?有什么建议吗?

I'm designing a larger enterprise architecture and I'm in a doubt about how to separate the models and design those.
There are several points I'd like suggestions for:
- models to define
- way to define models

Currently my idea is to define:

  1. Core (domain) model
  2. Repositories to
    get data to that domain model from a
    database or other store
  3. Business logic model that would contain
    business logic, validation logic and
    more specific versions of forms of
    data retrieval methods
  4. View models prepared for specifically formated
    data output that would be parsed by
    views of different kind (web,
    silverlight, etc).

For the first model I'm puzzled at what to use and how to define the mode.
Should this model entities contain collections and in what form?
IList, IEnumerable or IQueryable collections? - I'm thinking of immutable collections which IEnumerable is, but I'd like to avoid huge data collections and to offer my Business logic layer access with LINQ expressions so that query trees get executed at Data level and retrieve only really required data for situations like the one when I'm retrieving a very specific subset of elements amongst thousands or hundreds of thousands.

What if I have an item with several thousands of bids? I can't just make an IEnumerable collection of those on the model and then retrieve an item list in some Repository method or even Business model method.
Should it be IQueryable so that I actually pass my queries to Repository all the way from the Business logic model layer?
Should I just avoid collections in my domain model? Should I void only some collections?

Should I separate Domain model and BusinessLogic model or integrate those?

Data would be dealt trough repositories which would use Domain model classes. Should repositories be used directly using only classes from domain model like data containers?

This is an example of what I had in mind:
architecture figure 1 http://img199.imageshack.us/img199/7364/arch1p.jpg

So, my Domain objects would look like (e.g.)

public class Item
    {
        public string ItemName { get; set; }
        public int Price { get; set; }
        public bool Available { get; set; }
        private IList<Bid> _bids;
        public IQueryable<Bid> Bids
        {
            get { return _bids.AsQueryable(); }
            private set { _bids = value; }
        }
        public AddNewBid(Bid newBid)
        {
            _bids.Add(new Bid {....
        }
    }

Where Bid would be defined as a normal class.

Repositories would be defined as data retrieval factories and used to get data into another (Business logic) model which would again be used to get data to ViewModels which would then be rendered by different consumers.

I would define IQueryable interfaces for all aggregating collections to get flexibility and minimize data retrieved from real data store.

Or should I make Domain Model "anemic" with pure data store entities and all collections define for business logic model?

One of the most important questions is, where to have IQueryable typed collections? - All the way from Repositories to Business model or not at all and expose only solid IList and IEnumerable from Repositories and deal with more specific queries inside Business model, but have more finer grained methods for data retrieval within Repositories.

So, what do you think? Have any suggestions?

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

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

发布评论

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

评论(1

热血少△年 2024-09-08 08:38:10

我认为首先在企业的每个应用程序中强制进行这样的设计并不是一个好主意。这是非常具体的。您如何预测各个应用程序的需求是什么?

其次,您所呈现的实际上不是领域驱动的设计。它看起来像是 Microsoft 祝福的 n 层方法。您拥有的是所有 3 层的数据结构。为什么要将“领域模型”与业务模型分开?包含数据和行为的丰富域模型对于大多数复杂系统来说应该足够了。视图模型可以构建在此模型之上(简单、经典的解决方案),也可以直接从数据库构建(CQRSish 解决方案)。后者似乎在复杂的场景中效果更好。

您询问的商品有数千个出价。好吧,也许 bid 是一个聚合根(您缺少的一个域概念),并且不应该包含在任何集合中。

我的建议是,如果您想要领域模型,请坚持 DDD 建模实践(Eric Evans、Jimmy Nilsson),不要严格遵循 App Archi Guide。如有必要,请在您的解决方案中添加一点 CQRS,以清除模型中与 UI 相关的问题。这样您就可以避免与集合处理相关的各种问题。

I don't think that forcing such design in each and every application in the enterprise is a good idea in the first place. It is very specific. How can you predict what will be the needs of individual applications?

Second, what you presented is really not a domain-driven design. It looks like Microsoft-blessed n-tier approach. What you have is data structures in all 3 layers. Why separating your 'domain model' from business model? Rich domain model containing both data and behavior should be enough for most of complicated systems. View model can be built on top of this model (simple, classic solution) or directly from database (CQRSish solution). The latter seems to work better in complicated scenarios.

You ask item has thousands of bids. Well, maybe bid is an aggregate root (a domain concept you are missing) and shouldn't be contained in any collection.

My advice would be, if you want domain model, stick to DDD modeling practices (Eric Evans, Jimmy Nilsson) and don't follow App Archi Guide literally. If necessary, add a little bit of CQRS to your solution to clean the model from UI related concerns. This way you avoid all sorts of problems related to collection handling.

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