Hi I'm trying to make application in accordance with the DDD. I'm have the following entities:
public class Item { public Category Category { get; protected set; } ... } public class SpecificItem : Item { ... } public class Category { public static int IdOfCategoryForSpecificItem = 10; public int Id { get; set; } }
And now I would like to create factory with method that create object of SpecificItem type. But this specific item must be in specific category. So I created factory like this:
public class ItemFactory { public static SpecificItem CreateSpecificItem(object someArguments) { IRepository<Category> repository = null // How to get repository? return new SpecificItem { Category = repository.FirstOrDefault(i => i.Id == Category.IdOfCategoryForSpecificItem), // additional initialization }; } }
And now my questions:
Rewrite your ItemFactory
to use dependency injection and inject the IRepository<Category>
using constructor injection. After doing that, the ItemFactory
will look like this:
public class ItemFactory
{
private readonly IRepository<Category> repository;
public ItemFactory(IRepository<Category> repository)
{
this.repository = repository;
}
public SpecificItem CreateSpecificItem(object someArguments)
{
return new SpecificItem
{
Category = this.repository.FirstOrDefault(i =>
i.Id == Category.IdOfCategoryForSpecificItem),
// additional initialization
};
}
}
This way you moved the responsibility of retrieving an implementation of IRepository<Category>
to the caller. Now you can do the same for all types that need the ItemFactory
. Inject the ItemFactory
as dependency in the constructors of these types. Do this all the way up to the top of your application's type hierarchy and compose the types their (the composition root).
Especially IoC/DI frameworks will get very handy to automate the creation of types for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With