Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DDD solution structure

I'm trying to create a nice a Solution structure for a new DDD project. I Created a "Core" project, where I added the Entities, ValueObjects and the repositories Interfaces then I added an "Infrastructure" project that contains the implementation of the previous IRepository.

Now, since my db will be MongoDb I need to add the attributes like "[BsonDateTimeOptions]" in some of the fields of the Entities, this will require to add a reference to the MongoDb driver package in the core project.

Since the core project should not contains any reference to MongoDb, should only contains business logic and it should be reusable in any other project (mobile - Xamarin) what should be the best practice in this situation?

What I'm able to think is:

  • the entities will not contains any reference to MongoDb
  • create in the Infrastructure projects a model for each Entity, that is a copy of the related entity but with the MongoDb attributes.
  • create a layer (in the repository?) that is able to use the model to query in the db, then transform it in entity and so returning the Entity, hiding inside the repository the model object.

This approach has a problem, that I will have a copy of the entity, the model, which only has the MongoDb attributes, and when adding some fields to an entity, I will have to modify the model too. Is this the correct approach?

Everything started from this solution structure.

like image 362
Davide Quaglio Avatar asked Jan 19 '26 03:01

Davide Quaglio


1 Answers

Instead of using attributes in your domain classes, write configuration code in the Infrastructure layer.

This seems to always be an option with MongoDB.NET, e.g.

BsonClassMap.RegisterClassMap<MyClass>(cm => 
{
    cm.AutoMap();
    cm.MapMember(c => c.DateOfBirth).SetSerializer(new DateTimeSerializer(dateOnly: true));
}

instead of

[BsonDateTimeOptions(DateOnly = true)]
public DateTime DateOfBirth { get; set; }
like image 141
guillaume31 Avatar answered Jan 20 '26 19:01

guillaume31



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!