I'm using asp.net mvc with entity framework and starting to learn DDD. I'm working on project that contains surveys. Here's my domain model:
public class Survey
{
public int? SurveyID { get; set; }
public string Name { get; set; }
public decimal MinAcceptanceScore { get; set; }
public int UserFailsCount { get; set; }
public IEnumerable<SurveyQuestion> Questions { get; set; }
public IEnumerable<Prize> Prizes { get; set; }
public IEnumerable<SurveyAttempt> UserAttempts { get; set; }
}
I need different parts of surveys for different views so I've created different ViewModels:
public class ShortSurveyViewModel
{
public int? SurveyID { get; set; }
public string Name { get; set; }
public int UserFailsCount { get; set; }
public IEnumerable<SurveyAttempt> UserAttempts { get; set; }
}
public class ShortSurveyWithPrizesViewModel
{
public int? SurveyID { get; set; }
public string Name { get; set; }
public int UserFailsCount { get; set; }
public IEnumerable<SurveyAttempt> UserAttempts { get; set; }
public IEnumerable<Prize> Prizes { get; set; }
}
public class SurveyEditViewModel
{
public int? SurveyID { get; set; }
public string Name { get; set; }
public decimal MinAcceptanceScore { get; set; }
public int UserFailsCount { get; set; }
public IEnumerable<SurveyQuestion> Questions { get; set; }
public IEnumerable<Prize> Prizes { get; set; }
}
What would be the best way to build my architecture if I want my survey repository to get information needed for appropriete view model?
Different solusions that I see:
Repository could return IQueryable to SurveyService and service could return the appropriete view model, but I hesitate that doing this is right because I think view models should be created in UI, not Service layer.
Create three appropriate classes in my domain layer. But now domain will be dependent from representation and with each new view new domain class should be created.
Retrieve full domain object and map just properties that needed for particular view. This is not good because in my example Questions only needed in one representation and it could be heavy collection.
The viewmodel may expose the model directly, or properties related to the model, for data-binding. The viewmodel can contain interfaces to services, configuration data, etc in order to fetch and manipulate the properties it exposes to the view.
Domain driven design:
Survey
and all relations which cannot exist without parent Survey
Survey
class and depending on your requirements just some relations (the really dogmatic DDD would always load whole aggregate but that is not a good approach for stateless web).Survey
and selected relations and fills view models.Onion architecture:
IQueryable<Survey>
- even worse you will use generic repository with CRUD interfaceSimple architecture:
IDbSet<Survey>
directly in your controller as a repository There is no best way. It is always about your goal and about your expectations. For small applications you can live with simple architecture without any issue.
Domain driven design is more complex. The main concept in DDD are domain entities, value objects and their composition. Domain entity encapsulates data and logic executed on those data. DDD does not work with partial data or DTOs - when your domain entities don't have any logic you are doing it wrong (it is called anemic model). The service in DDD is not mediator between application layer and repository. It is used to handle business logic which is not related to single domain entity (so cannot be encapsulate in domain entity). Repository is infrastructure code needed to materialize your aggregates from storage and to persist them in storage. Application logic (controller) can interact with domain entities, services and infrastructure code.
I don't like onion architecture.
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