我的DDD工厂课程应该有哪些方法? [英] What methods should go in my DDD factory class?

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

问题描述

我正在努力了解我的工厂课程在我的DDD项目中应该做什么。是的,工厂应该用于创建对象,但究竟应该做什么。考虑以下工厂类:

  public class ProductFactory 
{
private static IProductRepository _repository;

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

public static Product CreateProduct()
{
//还有什么会在这里?
}

public static Product GetProductById(int productId)
{
//我应该从这里直接呼叫回复吗?
Greener.Domain.Product.Product p = _repository.GetProductById(productId);
return p;
}
}

我应该直接调用存储库在工厂内?



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



需要让这个类完成,还有什么其他的方法我应该有吗?



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

请帮忙!

解决方案



工厂内直接致电
存储库?


不,不要使用一个工厂,当你检索的东西,只有当你第一次创建工厂时才使用工厂。


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


将数据传入工厂,如果需要对象的初始创建。


我需要什么使这个类
完成,还有什么其他方法我
有吗?


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


我应该使用该类从域创建
的Product对象,从
存储库右边?


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



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



说明和示例



例如,在项目中工作我有一个excel处理器基类和许多子类实现基类。我使用工厂来获得正确的值,然后调用方法,不知道返回哪个子类(注意:我更改了一些变量名,并且更改了很多代码)



处理器基类:

  public abstract class ExcelProcessor 
{
public abstract Result进程(字符串ExcelFile);
}

其中一个Processor子类:

  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:
返回新的CompanyCExcelProcessor(CurrentUserId);
//等...
}
}

用法:

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


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?

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

Please help!

解决方案

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.

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.

Explanation and Example:

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)

Processor base class:

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

One of the Processor subclasses:

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

Factory:

 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...
      }
 }

Usage:

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

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

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