应该在哪里域对象和服务类对象放置在何处? [英] Where should domain objects and service class objects be located?

查看:113
本文介绍了应该在哪里域对象和服务类对象放置在何处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尽我所能去学习DDD,但我不能确定的一些很基本的概念;哪里是你的域对象和服务定​​位,总体布局是如何组织的?我有个习惯,好不好我不知道,这就是为什么我要求;做一个计划的类,它包含域的集合,域对象和服务对象。就像下面的例子。

I'm doing my best to learn DDD, but I'm unsure about some really basic concepts; Where are your domain objects and services located, and how is the overall layout organized? I have a habit, good or bad I don't know, that's why I ask; to make a "Program" class, which holds domain collections, domain objects and service objects. Like example below.

这是一个好或坏的做法?如果不好,有什么办法?我至今只在与WinForms的项目,所以这是我在这个时候主要关注的问题。

Is this a good or bad approach? And if bad, what are alternatives? I'm so far just working with winforms projects, so that's my main concern at this time.

此外,如果你能有我所用静态在这里对象的一些评价?我常常不能确定,如果用静态的,而且往往只是把它如果我知道这是一个单独的对象(如果这是正确的称呼)。

Also if you could include some evaluation of my use of "static" objects here? I'm often unsure if to use static, and tend to just throw it on if I know it's a singleton object (if that's the correct term).

修改 为了解释一点,我使用的是程序类的新方法来协调不同的域对象,并从数据库等运行初始加载,在程序启动。所以,如果你建议我摆脱这个类的话,我应该引入一些其他的服务,而不是处理程序启动?

To explain a bit more, I'm using the New method of the "Program" class to coordinate the different domain objects, and run initial loading from database etc, on program startup. So if you suggest me to get rid of this class, then should I introduce some other services instead to deal with program startup?

例如:

class Form1
{
    private MainClass main;
}

class MainClass
{
    public static UserService UserServ;
    public static UserAccountService UserAccServ;
    public static List<UserAccount> UserAccounts; 
}

class UserService
{
}

class UserAccountService
{
}

class UserAccount
{
    public UserData User;
}

class UserData
{
}


编辑: 应对大卫:


Response to David:

谢谢!为了解决你的观点:

Thanks! To address your points:

1)我的例子只是一个粗略的类型,我使用单独的文件为每个类,而我的项目其实已经10,000行的code,虽然它可能不健全喜欢它。

1) My example was just a rough type, I do use separate files for each class, and my project is in fact already 10,000 lines of code, although it may not sound like it.

2)明白了在接口上的提示。

2) Understood tips on interfaces.

3)明白了约assembleys。

3) Understood about assembleys.

4)我使用的库(和FluentNHibernate),我计划让服务依赖于它们肯定。目前,我有xxxManager课,我知道是不好的,并会努力将其更改为xxxServices。

4) I'm using Repositories (and FluentNHibernate), I plan to have services depend on them yes. At the moment I have xxxManager classes, which I know is bad, and will work to change them to xxxServices.

5)我的理解DI是非常受欢迎的,它的好,你做了这一点,所以我知道这是一个关键的解决我的问题。我会学习的。所以,据我了解,你建议我直接注入服务到窗体类?但是对于域对象?他们住在哪里?如果他们都生活在某种类型的服务?如果是那样的话,你创造什么类型的服务,处理程序的启动逻辑?我理解服务的基本概念进行持久化数据,使用存储库等,但也有可能是其他类型的普通服务类的,我是不知道的,有关我的问题?

5) I understand DI is very popular, and it's good you make this point, so I know this is a key resolve for my question. I will be learning that. So as far as I understand, you suggest me to inject the services directly to the form class? But what about domain objects? Where do they "live"? Should they all live in some type of service? If that's the case, what type of services do you create to deal with program-startup logic? I understand the basic concepts of services for persisting data, using repositories etc, but there are probably other type of common service classes that I'm not aware of, related to my question?

PS:我不应该有(错误)命名我的班程序,因为这已经是一类在C#项目。我同时使用C#和VB,所以实际上在C#中,我通常把它叫做MainClass。所以我编辑这一点。

推荐答案

你所描述的是如何组织你的依赖(在这种情况下,你的表格项目依赖在服务)。

What you are describing is how to organise your dependencies (in this case, your forms project depends on the services).

有关上述code的几点:

A few points about the above code:

  • 看起来你把所有的类在同一个文件。一般来说,你应该把每一个新的类在其自己的文件。
  • 您的服务类都应该执行接口 IUserService IUserAccountService ) - 这使您可以嘲笑你的依赖单元测试,交换的实现,更好地分离应用程序
  • 您的服务类最好应在不同的组件(新的Visual Studio项目),因为如果您的域的实体( UserAccount 的UserData )。
  • 您可以考虑创建一个或多个库来处理简单地获取数据到你的应用服务(应用服务会对存储库的依赖,版本库会的不可以取决于服务)。
  • 您有您的服务在静态字段(不是单身,那就是它采用静态域在C#中,以确保只有一个对象将应用程序的生命周期内创建的模式)。这将是更好地为您使用 Program.UserService ...等使用DI(见下文),而不是你的表格。
  • It looks like you have all of your classes in the same file. Generally you should place every new class in its own file.
  • Your service classes should both implement an interface (IUserService and IUserAccountService) - this enables you to mock your dependencies for unit tests, to swap implementations and to better decouple your application.
  • Your service classes should ideally be in a different assembly (new visual studio project), as should your domain entities (UserAccount and UserData).
  • You might consider creating one or more repositories to deal with simply getting the data to your application services (application services would have a dependency on the repository, the repository would not depend on the service).
  • You have your services in static fields (not a singleton, that is a pattern which uses static fields in C# to ensure that exactly one object will be created for the lifespan of the application). It would be better for you to use DI (see below) instead of your forms using Program.UserService...etc.

一旦这样做了,那么你是在一个位置,你可以看看使用的依赖注入(DI)以您的应用程序提供的服务(而不是您的应用程序负责创建他们自己)。 Ninject 将是一个不错的选择使用。这里的<一个href="http://stackoverflow.com/questions/4129092/how-to-use-ninject-in-a-windows-forms-application">an例如如何实现它但你可能有更多的运气在 Ninject维基

Once that's done then you're in a position where you can look at using dependency injection (DI) to provide your services to your application (as opposed to your application being responsible for creating them itself). Ninject would be a good choice to use. Here's an example of how to implement it but you might have more luck on the Ninject wiki.

这篇关于应该在哪里域对象和服务类对象放置在何处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆