What methods should go in my DDD factory class?

后端 未结 7 1386
终归单人心
终归单人心 2021-02-02 17:38

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

7条回答
  •  孤独总比滥情好
    2021-02-02 18:05

    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();
    

提交回复
热议问题