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:
- It is correct way to create factory and use repository?
- How to get repository? I can't use DI because it's static method. I don't like ServiceLocator because it's difficult to unit testing.
- Maybe there are better solutions for this problem.