关于构建领域驱动设计命名空间的一些问题

发布于 2024-10-06 00:49:53 字数 1086 浏览 2 评论 0原文

我有一些关于框架设计的一般性问题。

我正在 C#.NET(框架 3.5)中为 iPhone 应用程序构建 API,& SQL 2008(使用 LINQ)。我遵循了领域驱动设计模式(在一本书中)并且 有以下文件夹结构:

Core
- DataAccess
--Impl
-Domain
-Impl

Core 是我的核心 API 库 - 我的 DLL。 DataAccess 包含数据访问接口 DataAccess.Impl 包含存储库(LINQ to the DB) 域包含我的大部分数据类型和属性。 Impl 包含我的服务(即 AccountService.cs、EmailService.cs)

现在,作为练习,我已向该项目添加了一个 Windows 服务 并尝试从此服务中的 DLL 调用功能。 我的问题是,我应该将哪一层暴露给其他应用程序 什么应该隐藏?

  • Impl 文件夹中的服务类应该是程序员看到的吗?
  • 或者来自 DataAccess.Impl 的存储库?
  • 或者,我应该把它全部展示给程序员看吗?正如它看起来 现在,这似乎有点令人困惑。

当我开始阅读 DDD 时,我假设存储库是 由服务类隐藏和访问,但我发现我需要调用 我的客户端中的功能。难道是我设计错了?

我的另一个问题与名称空间命名有关。当 Windows 服务 从我的核心库调用功能,我必须这样进行包含:

using Company.Product.ProductCore.Core.DataAccess.Impl 
using Company.Product.ProductCore.Core.Domain 
using Company.Product.ProductCore.Core.Impl

这看起来很罗嗦。看看微软的DLL,他们似乎保持着两层结构 约定 - (System.Linq、System.Text 等)。拥有 Company.Product.ProductCore.Core.Impl 看起来很混乱,并没有真正告诉程序员这个名称空间的作用(但它 这是我读过的例子所建议的)。这里有最佳实践吗?

我们非常感谢您的建议(以及任何示例)。

谢谢。

I have some general questions about framework design.

I am building an API for an iPhone application in C#.NET (framework 3.5), & SQL 2008 (using LINQ). I have followed the Domain-Driven-Design pattern (in a book) and
have the following folder structure:

Core
- DataAccess
--Impl
-Domain
-Impl

Core is my core API library - my DLLs.
DataAccess contains the data access interfaces
DataAccess.Impl contains the repositories (LINQ to the DB)
Domain contains most of my data types and properties.
Impl contains my services (i.e. AccountService.cs, EmailService.cs)

Now, as an exercise, I have added a Windows Service to this project
and am attempting to call functionality from the DLLs in this service.
MY QUESTION IS, what layer should I be exposing to other applications
and what should stay hidden?

  • Should the service classes from the Impl folder be what programmers see?
  • Or the Repositories from DataAccess.Impl?
  • Or, should i just lay it all out for the programmers to see? As it looks
    now, this seems sort of confusing.

When I started reading about DDD I was assuming that the repositories would be
hidden and accessed by the service classes, but I am finding I need to call
functionality from both in my client. Have I designed this wrong?

My other question has do do with namespace naming. When the Windows Service
calls functionality from my core library, I have to do my includes as such:

using Company.Product.ProductCore.Core.DataAccess.Impl 
using Company.Product.ProductCore.Core.Domain 
using Company.Product.ProductCore.Core.Impl

This seems wordy. Looking at Microsoft's DLLs, they seem to keep with a two-tier
convention - (System.Linq, System.Text, etc). Having Company.Product.ProductCore.Core.Impl
seems messy and doesn't really tell the programmer what this namespace does (but it
is what was suggested by the example I read). Is there a best practice here?

Your suggestions (and any examples) are seriously appreciated.

Thanks.

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

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

发布评论

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

评论(3

合久必婚 2024-10-13 00:49:53

在我看来,域绝对不是您正在寻找的东西,您的数据访问层也不是。

以我的拙见,必须公开的内容尚未存在,即静态类,比方说,如果我们考虑 Façade 设计模式,它公开了您的库子系统的特性和功能。

alt text

外观设计模式解释:

  1. 外观设计模式-(四人帮);
  2. 外观模式(维基百科)

那么最后,你的代码应该做什么?只需公开您应该了解的必要内容,因为您是唯一了解您正在开发的系统的人。

简而言之,我经常使用 Façade 模式,这样我就可以将我的类、实现和几个相关的子系统隔离在外观的背后。假设我们是一家全新的汽车经销商。这个立面将是巨大的窗户,让您可以看到陈列室中暴露的汽车。你必须想一想这个外观暴露了什么。它只暴露汽车吗?

在我看来,这个立面暴露了汽车,你可以购买汽车,借钱购买,修理汽车,购买其他相关配件等。配件是要暴露的,但只是需要暴露的。汽车等其他物品也是如此。也就是说,您可能只想公开一个接口,并为自己保留实现,以便通过您的外观,当您必须返回 ICarIAccessory 时,您必须通过实现对象类实例化它们,然后通过外观返回接口实例。也就是说,用户不需要知道引擎盖下发生了什么,但只要他想要一辆车,他就必须通过你的外观订购它。就像你不会去买马自达3去买奔驰一样。您将使用正确的外观。话又说回来,不同的汽车经销商可能只是子系统,因此是某种工厂。然后,您向外观请求一辆具有这种或那种规格的汽车,并且外观应该知道要返回什么ICar实例来交换您要求它提供给您的东西。

无论如何希望这有帮助! =)

The domain is definitely not what you're looking for, in my opinion, neither is your data access layer.

In my humble opinion, what would have to be exposed is not yet there, that is, a static class, let's say, if we consider the Façade design-pattern, which exposes your library subsystems features and functionalities.

alt text

The Façade design pattern explained:

  1. Façade Design-Pattern - (Gang of Four);
  2. Facade pattern (Wikipedia).

So in the end, what is your code supposed to do? Just expose what is necessary, which you ought to know, since you're the only one to know about the system you're developing.

In short, I often use the Façade pattern so that I can isolate my classes, implementations and several related subsystems under the hood of a façade. Let's consider we are a brand new car dealer. This façade will be the great windows which let you see the cars exposed in the show room. You got to think of what this façade exposes. Does it expose only cars?

In my opinion, this façade exposes cars, which you will be able to buy, to borrow money to buy, to repair the car, to buy other related accessories, etc. The accessories are then to be exposed, but only what needs to. The same with the other items such as the car. That is, you might want to expose an interface only, and keep the implementation for yourself, so that through you façade, when you got to return an ICar or an IAccessory, you have to instantiate them through your implementation object class, then return the interface instance through your façade. That said, the user doesn't need to know what is going on under the hood, but only that if he wants a car, he has to order it through your façade. Just like you won't go buy a Mazda 3 to Mercedes Benz. You'll work with the right façade. Then again, the different car dealers might only be subsystems, hence some kind of factories. Then you ask the façade for a car with this and that specifications, and the façade should know what ICar instance to return to exchange for what you're asking it to provide you with.

Hope this helps anyway! =)

宁愿没拥抱 2024-10-13 00:49:53

如果我没记错的话,您会问两个问题:

  1. 如何构建 DDD 应用程序?
  2. 那些长命名空间名称怎么样?

我的答案相当冗长——我冒昧地将其分成两个答案。

这是第二个问题的答案:

2。那些长命名空间名称怎么样?

我不认为长命名空间名称一定是混乱的。
恕我直言,你的名字看起来很混乱的是:

  • 重复使用“Product”和“Core”一词
  • 通用术语“Impl”

第一个改进可能是:

// The Domain objects go here;
MyCompany.MyProduct.Core.Domain;  
// DataAccess interfaces go here:
MyCompany.MyProduct.Core.DataAccess;
// a Linq implementation of the DataAcces interfaces go here:
MyCompany.MyProduct.Core.DataAccess.LinqImpl; 
// The Core service go here:
MyCompany.MyProduct.Core.Services;
// Some general purpose infrastructure goes here (e.g. emailing code)
Mycompany.MyProduct.Infra;

此外,我发现使用商业产品名称(例如as MyProduct)在代码结构中很糟糕(如果营销选择不同的名称怎么办?);我喜欢使用逻辑子系统的名称。
让我们假设您正在构建汽车租赁系统。那么CarRental将被视为此应用程序的核心功能。然后我将使用以下命名空间结构(Serra 是我公司的名称):

// For classes Customer, Account, Car
Serra.CarRental.Domain;    
// I use Dao as a general abbreviation for "Data Access Objects"
// Dao interfaces go here:
Serra.CarRental.Dao;     
// Linq implementation for Dao interfaces  
Serra.CarRental.Dao.Linq;
// For Services specific to the car rental domain: 
// AccountService and RentalService and CarAvailabilityService
Serra.CarRental.Services;  
// For UI objects (not relevant in you situation?)
Serra.CarRental.UI;
// general service code; ignorant of the CarRental domain (e.g. an EmailService)
Serra.Infra.Service;       

If I'm not mistaken, you are asking two questions:

  1. How to structure a DDD application?
  2. What about those long namespace names?

My answer is rather lengthy - I took the liberty of splitting it into two anwsers.

This is an answer to the second question:

2. What about those long namespace names

I don't think long namespace names are necessarily messy.
IMHO what looks messy in your names are:

  • the repeating of the words "Product" and "Core"
  • The use of the general term "Impl"

A first improvement could be:

// The Domain objects go here;
MyCompany.MyProduct.Core.Domain;  
// DataAccess interfaces go here:
MyCompany.MyProduct.Core.DataAccess;
// a Linq implementation of the DataAcces interfaces go here:
MyCompany.MyProduct.Core.DataAccess.LinqImpl; 
// The Core service go here:
MyCompany.MyProduct.Core.Services;
// Some general purpose infrastructure goes here (e.g. emailing code)
Mycompany.MyProduct.Infra;

Furthermore, I find that the use of a commercial product name (such as MyProduct) in the code structure is bad (what if marketing chooses a different name?); I like to use the name of logical subsystems instead.
Let's assume your building car rental system. Then CarRental will be considered the core functionality of this app. Then I'd use the following namespace structure (Serra is the name of my company):

// For classes Customer, Account, Car
Serra.CarRental.Domain;    
// I use Dao as a general abbreviation for "Data Access Objects"
// Dao interfaces go here:
Serra.CarRental.Dao;     
// Linq implementation for Dao interfaces  
Serra.CarRental.Dao.Linq;
// For Services specific to the car rental domain: 
// AccountService and RentalService and CarAvailabilityService
Serra.CarRental.Services;  
// For UI objects (not relevant in you situation?)
Serra.CarRental.UI;
// general service code; ignorant of the CarRental domain (e.g. an EmailService)
Serra.Infra.Service;       
只是偏爱你 2024-10-13 00:49:53

我怀疑您的应用程序的用户会看到这一点。
您首先需要的是功能。这就是你最后要卖的东西。

您还需要尽可能降低维护费用。这就是您的代码的组织方式。
第二件事 - 最大限度地减少维护费用。

因此,要决定如何组织您的解决方案,您应该回答一些问题:

  1. 选择的结构将如何帮助我
    进行维护并添加新的
    特征?
  2. 对新人有什么帮助
    开发人员开始在我的内部编码
    解决方案?
  3. 就是这样吗
    程序集/文件夹/类名称是
    可识别且具有描述性?
  4. 我忘记的问题。

这就是你的目标。但不是您正在寻找的规则。
如果新开发人员能够理解它们,您可以选择任何名称。

也许(作为一种启发式方法,如果您使用一些重构工具,如 Resharper 或 Refactor! Pro),您可以从单个程序集和单个名称空间开始。在开发过程中,您将看到如何更改项目结构以更好地满足您的需求。然后重构它。

请观看演讲,时间为 38:18-39:45(约 1.5 分钟)。关于如何回答一些问题有很好的实践。

I doubt that users of your application will see this.
First thing you need is functionality. This is what you will sell at the end.

Also you need to make maintenance expenses as low as possible. And here comes how your code is organized.
Second thing - minimize maintenance expenses.

So to decide how to organize your solution you should answer some questions:

  1. How picked structure will help me
    to do a maintenance and adding new
    features?
  2. How will it help new
    developers to start coding inside my
    solution?
  3. Is all that
    assembly/folder/class names are
    recognizable and descriptive?
  4. Questions that I forgot.

This is your goals. But not the rules you are looking for.
You may pick any names if new developer will understand them.

Probably (as a heuristic and if you use some refactoring tools like Resharper or Refactor! Pro) you can start with single assembly and single name space. During development you will see how you may change structure of your project to better meet your needs. Then refactor it.

Go watch this talk at 38:18-39:45 (~1.5 minute). There are good practice about how to answer some questions.

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