I'm somewhat new to Domain Driven Design and have coded up our core business processes and objects in our Domain Layer. The domain objects are being persisted through Repositories, and everything fits really well. The domain model and services are exposed to the front end via web / WCF services.
I'm starting to work on parts of the front end, and have some front end settings and concepts that need to be persisted, like saving user layouts, views, and preferences. Originally, I just had these models coded up in the front end assembly (I'm using WPF). I'm trying to figure out how these application objects fit into the existing domain model and repositories. Other than using the same User object, the application model concerns seem completely unrelated to the existing domain model. I'm wary of overengineering the solution, but it feels like I should create a sort-of mini-domain model and respository for the application objects. Should I create a separate set of assemblies/libraries to manage the application models or is there a better approach? Thanks!
Absolutely. Keep your core domain small and focused.
Related
When using MVC5 I add a domain layer and storage layer using dependency injection for all the business data. But I have always left the ApplicationDbContext in the main MVC application layer project.
I've read a great many posts on SO and see many people recommend moving the ApplicationDbContext out of the MVC project. I'd like to understand why ApplicationDbContext should or should not be moved. Are there any reasons to not move this context?
On the one hand, ApplicationDbContext uses a data model which suggests it should be moved to the storage layer but that will require large DTO's. On the other hand, ApplicationDbContext really relates primarily to application access and I have a separate roles/permissions functionality for the business data anyway. Several SO posts also suggested checking roles in the application layer, not the domain layer, but that seems suspect; don't we want to check business roles in the domain layer?
So I'm confused and before I go to the work to move the ApplicationDbContext to a different layer, I'd like to make sure I'm making a sound and informed decision.
It is not a must, but it is advisable if you are doing DDD and your project tends to grow large.
Although DDD is not about implementation details, DDD asks for a clear separation of concerns. Then you should separate domain logic from infrastructure.
You could achieve that separation in many ways. One way would be to have a single project with folders for Domain, Application and Infrastructure, and have your DbContext reside in the Infrastructure folder. This is suitable for small projects.
In large projects, however, I would recommend you to evaluate the Clean Architecture, which will take this separation to project level.
I'd like to understand why ApplicationDbContext should or should not be moved. Are there any reasons to not move this context?
It can be moved, there's no rule against it. But the tooling will then require you to specify both startup and database projects as parameters, like this:
dotnet ef database update --project <path> --startup-project <path>
But since you're using MVC5, you're probably not using EF Core. In case of EF 6 or lower, you'll be using the Package Manager Console (PMC) to manage migrations and database-updates, which will make your life easier in that regard, since you can mark you MVC project as the Startup Project from the context menu, and select the target project from a dropdown control in the PMC.
Several SO posts also suggested checking roles in the application layer, not the domain layer, but that seems suspect; don't we want to check business roles in the domain layer?
Yes, roles are related to permissions, which are business rules. People probably recommend checking that in the application layer because you need to pull this data from the database, but you could do it like this:
Use the Specification Pattern to represent roles and permissions as an specification in the Domain Layer. But the IRepository interfaces would best be defined in Application Layer, as they represent infrastructure (which will be concretely implemented in the Infrastructure Layer). So you would start the role-checking in the Application Layer by using repositories to retrieve data, but the actual permission validation would be done by an Specification in the Domain Layer.
That would be one way to do it.
I am trying to understand the concept of layered architecture correctly. I think I understand most of the concepts, but there is one or two things that are still unclear to me...
So I understand the purpose of each individual layer: Presentation layer is for UI and user interaction, application layer coordinates application logic and domain objects, domain layer is where all of the business logic goes, infrastructure layer is for persistence and access to external services such as network capabilities etc.
I understand that each layer should only depend on the layers below it, and that layers should be separated with interfaces. What I have a hard time understanding though is the interaction between domain layer and the infrastructure layer...
I am developing a database centric desktop application. My initial approach was to pull all of the information out of the database into an in-memory domain model. The model would then live there for the duration of the application, and then be persisted to the database only once the user saves. I am having my doubts about this approach though... From what I have read, it seems that I should only pull data out of the database when as needed. In other words, pull a information into an object, edit the object, and push the object back into the database once done. What confuses me though, is that the objects become disconnected from their state once they leave the database. So lets say I pull object A out of the database, and edit it. Before I commit it back to the database, another part of the application also pulls object A out of the database. The same two objects are now living in my domain layer, with different references, and with different states. This just seems like a nightmare to me. Am I missing something? Should I only allow one transaction at a time to avoid this issue?
Was my initial approach of pulling all of the data into an in-memory model wrong? My application could have as many as 10 000 records, so I suppose it could lead to memory issues?
A side note: I am not using any ORM at this stage.
Any help or insight would be appreciated!
infrastructure layer is for persistence and access to external services such as network capabilities etc.
Persistence layer is for persistence and infrastructure layer is usually a cross-cutting layer (i.e. it spans vertically across different layers) which may include stuff like logging or what you mentioned.
Your concerns regarding data updates are a classic concurrency problem, not specific to a layered architecture. There are many solutions, a typical one is optimistic concurrency control, which checks that the old values haven't been modified before updating with new values. A similar approach is using an auto-incremental value instead the old values.
I am quite new to DDD and have come across a scenario that i'm not to sure how to handle.
I have an application that is used to track vehicles. This application is what will be implementing the "core" of the domain for the business i am working for. Not only is this application going to be used, there will be other utility programs that must be created and used in order to help this "core/main" application function.
for example:
there is an windows service needed that will perform configured queries on a database and return results to an external database that my routing application will use. This windows service has the concept of a QuerySettings class that can be created and is then executed by this application.
Question1:
What do you call utility applications like the above described in DDD? ( it definitely isn't the main core of the domain but it's needed in order for the core application to work )
QUestion2:
Is QuerySettings a domain model? if not what is it and where should it be placed within following the onion architecture?
For question1: You may have a look at Bounded context, I think Bounded context contains a group of Domain models that represent concepts in a subdomain(or a core domain). You may need to map or share domain models in different bounded contexts to handle your business, this depends on your bounded context strategy, share-kernal, anti-corruption-layer(to name a few).
For question2: I have little information of how QuerySettings works but in general it is a domain model but in a generic subdomain, not in your vehicle tracking core domain. In core domain's view, it maybe an infrastructure concept.
If I am developing an application using DDD, where do the infrastrucure and behavior components go? For example, user management, user specific configuration, permissions, application menuing, etc.
These components really have nothing to do with the business requirements being fullfilled by my domain, but they are still required elements of my application. Many of them also require persistance.
It's pretty normal to have non-domain components along with the domain in your project - after all not everything is business domain oriented. Where they belong actually depends on how you structure your solution. In most cases I tend to follow Onion Architecture, so all of my logic is provided by Application Services, regardless if it's domain or non-domain oriented.
Well if you find that your usecases rarely demands information from your core domain joined with application specific, you can probably split that into a separate database. Access this information through Application Service layer, since this layer is suppose to serve your application needs. If that includes user profile persistence etc, that's fine.
But you remember that if you got infrastructural failure and you want to do a rollback with some transaction logs or database backups, you'd probably want all persisted data be roll-backed. So then it's easier to have these domains share a database. Pros and cons - always compromise...
If I know that this application would have minor interaction with it's environment, I would put this in one database and let the application service layer interact with clients.
If I know that there will be several applications/clients I may consider to split database so that Webb application user specifics are stored in separate database. Very hard to say, since I have no overview of all the requirements.
/Magnus
We have a composite application built using the Composite UI Application Block (CAB)/Smart Client Software Factory (SCSF). To date, each module in our composite app has used its own set of DTO's, and business logic has been duplicated throughout the module, both in the UI layer and the Service layer. I would like to pursue more Domain-Driven approach in order to encapsulate business logic in a domain layer that can be distributed to the UI tier and the Service tier, and (ideally) across modules.
We have multiple modules in our composite application under development at one time, and we need to be able to deploy them in any order. Ideally, I would like for our modules to share a common domain model, but I'm afraid that when we deploy a new version of the domain model along with a module, that we will need to regression test the other modules against the domain model.
The alternative seems to be duplicating the domain model in each module, but all that code duplication smells funny to me. Has the industry developed any best practices for this type of situation?
I've used a single domain model, but one which allows versioning on every individual definition. Code generation provides both the interfaces per-service, and mapping code that can cross service and version boundaries.