Objective C 领域驱动设计
通常,在使用 C# 进行编程时,我会按照领域驱动原则构建项目。我是 iPhone 的 Objective C 编程新手,想知道是否有人有任何使用 Objective C 中的领域驱动设计原则的示例项目或代码。我正在寻找如何使用业务对象等的示例。谢谢。
Normally when programming in C# I architect my project after the domain driven principles. I am new to programming in Objective C for iPhone and was wondering if anyone had any sample projects or code that uses the domain driven design principles in Objective C. I am looking for examples of how Business Objects, etc are used. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能会误解什么是领域驱动设计原则。 DDD 是一套指导方针,它更多的是关于你如何思考以及如何处理问题,它主要与技术无关。如果您编写 Objective C 代码,那么没有什么可以阻止您的设计由领域而不是技术驱动(我个人喜欢您必须命名参数,因为在我看来,它可以使代码更具可读性)。
您的问题可能是关于通常支持(但不定义)DDD 的技术:ORM、DI、单元测试。恕我直言,这部分不太好(基于简短且相对过时的经验)。您通常不使用 ORM,而是使用 Core Data 这是一个对象图持久化在理论上应该更好,因为您不处理“关系”部分。然而,我记得核心数据对我的对象模型施加了某些限制,我希望在其他环境中避免这些限制。不能代表 DI,但单元测试很痛苦(2010 年),我听说 2012 年仍然如此。
最重要的是,你必须详细说明你的问题,将其拆分,然后可能在 Objective-中提问 - C段。
There is a chance that you misunderstand what Domain Driven Design principles are. DDD is a set of guidelines, it is more about how you think and how you approach problems, it is mostly technology agnostic. Nothing prevents your design from being driven by domain instead of technology if you write Objective C code (I personally liked the fact that you have to name arguments because it made for a more readable code in my opinion).
Your question is probably about technologies that usually support (but not define) DDD: ORM, DI, Unit Tests. And this part is not so good IMHO (based on short and relatively outdated experience). Instead of using ORM you usually employ Core Data which is an object graph persister that is suppose to be even better in theory because you don't deal with 'Relational' part. I remember however that Core Data put certain restrictions on my object model that I would wanted to avoid in other environments. Can not speak for DI, but unit testing was a pain (in 2010) and I've heard it still is in 2012.
The bottom line is that you would have to elaborate on your question, split it and maybe ask it in Objective-C section.
令我惊讶的是,没有人提到 CoreData 是多么反 DDD。即使使用 CRUD 风格的应用程序,我也总是尝试编写 DDD 风格的代码。您首先会注意到的事情之一是 CoreData 在整个 Cocoa 中的广泛使用,特别是在 Mac 上。
XCode 和 Cocoa 促进了 CoreData 与绑定的使用。然而,CoreData 迫使您始终对数据库进行编码,而不是对域模型进行编码。事实上,您无法在不强制整个数据库迁移的情况下重命名单个域对象属性。
如果您正在学习 Cocoa,您将必须学习 CoreData,因为许多应用程序和示例代码都依赖于它。后来,随着你对框架的了解逐渐成熟,如果你真的想要一个领域模型,你可能会想完全放弃 CoreData,这就是我通常为自己的项目所做的事情。
I am surprised that no one mentioned how anti DDD CoreData is. Even with CRUD style apps, I always try to code DDD style. One of the first things you will notice is the extensive use of CoreData throughout Cocoa, specially on Mac.
XCode and Cocoa promotes the use of CoreData along bindings. However CoreData forces you all the time coding to the database, not the domain model. Indeed, you cannot rename a single domain object attribute without forcing a whole database migration.
If you are learning Cocoa you will have to learn CoreData because many apps and sample code rely on it. Later on, as your knowledge of the framework matures, you might want to drop CoreData altogether if you really want a domain model, which is what I usually do for my own projects.
我也是 C# 出身,在接触 ObjC 半年之后,我必须说,在 Objective-C 中完全遵循 DDD 原则当然是可能的。部分原因是该语言具有静态/动态混合的smalltalk 性质。
例如,您可以通过重写一些方法来非常轻松地“劫持”发送到对象的消息(也称为拦截方法调用),如果您理解我的意思,这使得 DI 和单元测试变得非常容易。
此外,中央调度使得一个非常好的跨系统的解耦消息发送,您可以使用它来简化域的大部分之间的通信。
最后,键值观察约定基础框架中的功能允许您轻松地将域对象连接到用户界面元素,并实时反映更改。
即,您的控制器不是直接操作服务对象,而是将命令发送到您的域对象之一,以修改其状态。然后,通过让你的控制器观察变化(通过 KVO 机制),这些状态很容易反映回来。
所以我想说,在 ObjC 中完全有可能/甚至更容易遵循 DDD 原则。
至于例子,我还没有见过。但遵循 C# 项目中使用的相同结构当然并不难(不是复制它们,而是类似地建模)。研究我提到的技术也会对你有很大帮助。
I came from a C# background as well and after half a year of getting into ObjC I must say that it is certainly possible to follow the DDD principles fully in Objective-C. Partly because of the static/dynamic hybrid smalltalk-ish nature of the language.
For example, you can 'hijack' message sent to objects (aka intercepting method calls) very easily just by overriding a few methods which makes DI and Unit Testing very easy to do if you understand what I mean.
Additionally, the grand central dispatch makes for a very nice decoupled message sending across systems which you can use to ease communication between big parts of your domain.
Finally, the key-value observing convention in the base framework allows you to easily connects your domain objects to user interface elements and have changes reflected in real-time.
i.e. instead of manipulating service objects directly, your controllers would instead send commands to one of your domain objects to which would modify its state. Those states are then easily reflected back by making your controller observe the changes (via the KVO mechanism.)
So I'd say it's totally possible/even easier in ObjC to follow the DDD principle.
As for examples, I have never seen any. But it certainly is not hard to follow the same structure you use in C# projects (not copy them, but model it similarly). Studying the technologies I mentioned will helps you a lot as well.