3

I am having a problem in my web application Design. I am using Web API (ASP.NET Core 1.0) and I will implement a Generic Repository using Entity Framework.

My models are:

Planet, Coordinate, Territory, Building, Mine, Resource

A planet has a coordinate and 4 territories. Each territory has multiple buildings,mines and resources.

This entities will have their own repository (PlanetRepository, TerritoryRepository, MineRepository, BuildingRepository, ResourceRepository).

I want also to implement a service layer where each service has its own repository.

So, when I say "create me a planet with all his basic information", my application should create a planet, 4 territories associated to that planet, with 3 mines each and 2 buildings and 3 resources. This should be persisted on database, because I want to create all this info, because it is a game and by default I will create multiple planets that are automated and play by itself.

In this action I am working with 4 different entities and 4 repositories.

Because it is REST the url's should be like (this are examples):

GET/POST api/Planet - get all planets / create new planet

GET api/Planet/{planetId}/Territory - get all territories from planet id

GET api/Territory/{territoryId}/Mine - get all mines from territory id

GET api/Territory/{territoryId}/Building - get all buildings from territory id

GET api/Territory/{territoryId}/Resource - get all resources from territory id

So, has you can see, my TerritoryController has dependencies from 3 different services (MineService, BuildingService, ResourceService) and in the future could have more.

My Territory model will have a lot of information, it can have more child entities than now, so this means that my controller will have a lot of dependencies and I want to abstract all of this! I still cannot find the best solution...

So, what is(are) the best(s) option(s) or pattern(s) to solve my problem?

André Azevedo
  • 227
  • 1
  • 4
  • 11
  • It sounds to me like you should look into dependency injection: http://stackoverflow.com/questions/130794/what-is-dependency-injection – JPG Jan 11 '17 at 23:23
  • So are you tell me that I will have my controller's constructor with many services with dependency injection? Imagina I have 4 ou 5 services to use in that controller, it will not be a problem or anti pattern? – André Azevedo Jan 12 '17 at 09:11
  • @JPG forgot to reference you – André Azevedo Jan 12 '17 at 15:49
  • That is correct! That is, indeed, one of the problems with dependency injection; but, for your purposes, it will be okay unless you have some really heavy services hiding behind there. The command pattern is a particularly complicated design that solves this problem by moving your dependencies into individual classes. Look into that if you would really like to make your application great. – JPG Jan 18 '17 at 22:41

0 Answers0