I'm battling to know what my factory class must do during my DDD project. Yes a factory ought to be employed for creating objects, but just what if it is doing. Think about the following Factory Class:

    public class ProductFactory

    

        public static Product CreateProduct()

        

        public static Product GetProductById(int productId)

        Eco-friendly.Domain.Product.Product p = _repository.GetProductById(productId)

            return p

        

    

Must i make an immediate call towards the repository from inside the factory?

How must i manage object creation when retriving data from the database?

What should i get this to class complete, the other techniques must i have?

Must i be utilising these kinds to produce the merchandise object in the domain and repository from right?

Help!

Be carefull, you will find two good reasons to instantiate a brand new object : Creating it and rehydrating it in the database.

The very first situation is handled through the factory. You are able to provide several techniques to produce an item around the factory. Factory techniques should return valid objects, to help you pass parameters to those techniques to provides needed information.

The factory method may also find the actual type to instantiate according to parameters.

You shouldn't mix this with rehydrating in the database. This type of instantiation should take values in the datarow and instantiate the item by using it. I usualy refer to this as a data builder rather than a factory.

The primary difference would be that the factory will instantiate an item with a brand new identity as the databuilder will instantiate an item by having an already established identity.

Must i make an immediate call to the repository from inside the factory?

No, avoid using a factory whenever your locating stuff, make use of a factory only when you're creating it the very first time.

How must i manage object creation when retriving data from the database?

Pass that data in to the factory, if it's needed for that object's initial creation.

What should i get this to class complete, the other techniques must i have?

Many industrial facilities aren't even individual classes, they're just techniques that offer object creation. You can fold the factory method into another class, should you felt enjoy it only agreed to be likely to call a parameterless constructor.

Must i be utilising these kinds to produce the merchandise object in the domain and repository from right?

The repository is perfect for getting (in this way creating) existing objects, the factory is the very first time you create an item.

Initially many industrial facilities will not do much except call a constructor. But when you begin refactoring and/or creating bigger object hierarchies, industrial facilities be relevant.

Explanation and Example:

For example, within the project I am focusing on I've an stand out processor base class and several subclasses applying that base class. I personally use the factory to find the proper one, after which call techniques onto it, unaware of which subclass was came back.(Note: I transformed some variable names and gutted/changed lots of code)

Processor base class:

public abstract class ExcelProcessor



Among the Processor subclasses:

public class CompanyAExcelProcessor : ExcelProcessor

awesome stuff

     



Factory:

 public static ExcelProcessor CreateExcelProcessor(int CompanyId, int CurrentUserId)

 situation CompanyEnum.CompanyA:

                return new CompanyAExcelProcessor()

           situation CompanyEnum.CompanyB:

                return new CompanyBExcelProcessor()

           situation CompanyEnum.CompanyC:

                return new CompanyCExcelProcessor(CurrentUserId)

           //etc...

      

 

Usage:

ExcelProcessor processor = CreateExcelProcessor(12, 34)

processor.Process()