我的 DDD 工厂类应该使用哪些方法? [英] What methods should go in my DDD factory class?

查看:21
本文介绍了我的 DDD 工厂类应该使用哪些方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力理解我的工厂类应该在我的 DDD 项目中做什么.是的,应该使用工厂来创建对象,但它到底应该做什么.考虑以下工厂类:

I am struggling to understand what my factory class should do in my DDD project. Yes a factory should be used for creating objects, but what exactly should it be doing. Consider the following Factory Class:

    public class ProductFactory
    {
        private static IProductRepository _repository;

        public static Product CreateProduct()
        {
            return new Product();
        }

        public static Product CreateProduct()
        {
            //What else would go here?
        }

        public static Product GetProductById(int productId)
        {
            //Should i be making a direct call to the respoitory from here? 
            Greener.Domain.Product.Product p = _repository.GetProductById(productId);
            return p;
        }
    }

我应该从工厂内部直接调用存储库吗?

Should i be making a direct call to the repository from within the factory?

从数据库中检索数据时,我应该如何管理对象创建?

How should i manage object creation when retriving data from a database?

我需要什么来完成这个类,我还应该有哪些其他方法?

What do i need to make this class complete, what other methods should i have?

我应该使用这个类从域和存储库中创建 Product 对象吗?

Should i be using this class to create the Product object from the domain and repository from right?

请帮忙!

推荐答案

我应该直接打电话给来自内部的存储库工厂?

Should i be making a direct call to the repository from within the factory?

不,不要在检索东西时使用工厂,只有在第一次创建时才使用工厂.

No, don't use a factory when your retrieving stuff, use a factory only when you are creating it for the first time.

我应该如何管理对象创建从数据库中检索数据时?

How should i manage object creation when retriving data from a database?

如果对象的初始创建需要该数据,则将该数据传递到工厂.

Pass that data into the factory, if it is required for the object's initial creation.

我需要什么来制作这门课完成了,我应该用什么其他方法有吗?

What do i need to make this class complete, what other methods should i have?

许多工厂甚至不是单独的类,它们只是提供对象创建的方法.如果您觉得它只是调用无参数构造函数,您可以将工厂方法折叠到另一个类中.

Many factories are not even individual classes, they are just methods that provide object creation. You could fold the factory method into another class, if you felt like it was just going to call a parameterless constructor.

我应该使用这个类来创建域中的 Product 对象和来自正确的存储库?

Should i be using this class to create the Product object from the domain and repository from right?

存储库用于获取(在某种意义上创建)现有对象,工厂是您第一次创建对象.

The repository is for getting (in a sense creating) existing objects, the factory is for the first time you create an object.

最初许多工厂除了调用构造函数外不会做太多事情.但是一旦您开始重构和/或创建更大的对象层次结构,工厂就会变得更加相关.

Initially many factories won't do much except call a constructor. But once you start refactoring and/or creating larger object hierarchies, factories become more relevant.

说明和示例:

例如,在我正在处理的项目中,我有一个 excel 处理器基类和许多实现该基类的子类.我使用工厂获取正确的一个,然后在其上调用方法,不知道返回的是哪个子类.(注意:我更改了一些变量名称并删除/更改了很多代码)

For instance, in the project I'm working on I have an excel processor base class and many subclasses implementing that base class. I use the factory to get the proper one, and then call methods on it, ignorant of which subclass was returned.(Note: I changed some variable names and gutted/altered a lot of code)

处理器基类:

public abstract class ExcelProcessor
{
      public abstract Result Process(string ExcelFile);
}

处理器子类之一:

public class CompanyAExcelProcessor : ExcelProcessor
{
     public override Result Process(string ExcelFile)
     {
      //cool stuff
     }
}

工厂:

 public static ExcelProcessor CreateExcelProcessor(int CompanyId, int CurrentUserId)
 {
      CompanyEnum company = GetCompanyEnum(CompanyId);
      switch (company)
      {
           case CompanyEnum.CompanyA:
                return new CompanyAExcelProcessor();
           case CompanyEnum.CompanyB:
                return new CompanyBExcelProcessor();
           case CompanyEnum.CompanyC:
                return new CompanyCExcelProcessor(CurrentUserId);
           //etc...
      }
 }

用法:

ExcelProcessor processor = CreateExcelProcessor(12, 34);
processor.Process();

这篇关于我的 DDD 工厂类应该使用哪些方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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