Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do I implement file storage for a domain model containing files

I am trying to adhere to domain driven design for the server side code of a web application. This application allows the user to upload files and then processes them. The user can then download the original file, a log file of the process or a file containing errored records.

I have a domain model called import which has a FileName property and where that file physically exists depends on the user id etc i.e. in a folder structure built dynamically.

Obviously the domain model should not know about the file storage as that is infrastructure, so I am thinking of creating an Infrastructure Service which takes an import domain model and retrieves the various files.

This then separates the file storage from the domain model but prevents me from asking the domain model for its files, i.e. I would have liked to have asked the import domain model to give me it's error file or it's original file etc.

Am I correct in using an infrastructure service or is there a better way to use the domain model for this, i.e. injecting in a class which handles the File reading etc. or is there a better / more correct way of doing this?

Regards, Gary

like image 608
Gary F Avatar asked Oct 18 '22 09:10

Gary F


1 Answers

If you want the files themselves be part of the domain, but not the knowledge about the storage specifics, an appropriate approach would be the dependency inversion principle:

Define a domain service interface that is able to return the files given an Import domain object. Make sure you design your aggregates in a way that the files are clearly part of an aggregate, i.e. avoid them being free-floating.

Now you can implement that interface in your infrastructure layer and put all storage logic in there. Wire the implementation to the interface with by means of your DI mechanism.

like image 184
theDmi Avatar answered Oct 28 '22 20:10

theDmi