Saving media files in CQRS/Event Sourcing architecture

2,060 views
Skip to first unread message

Nikita Sushkov

unread,
Oct 22, 2012, 6:18:29 AM10/22/12
to ddd...@googlegroups.com
Hi everybody!

There are several operations in my current project which include adding media content (user adds userpic, etc). Now I see two ways how to implement file persistence in command->commandhandler->eventhandler pipeline:

1) Add byte array to Command and push it through Domain Object (User for example) and finally make it a part of generated event which will be persisted to read db (mongodb in my case). Why I don't like this approach:
    a) Aggregates become very heavy (imagine that we have about 30 images and we should load all them to make some action on aggregate root) 
    b) Storage space needed to persist files doubled 
    c) Duplication of files if we want to add one file to several roots

2) Save file to special file storage (in Command handler or even before command processing), get file id (ObjectId in Mongodb GridFS) and work with file id in DomainObject and EventHandlers. Why I don't like this:
    a) It breaks the rule that we update our read model in EventHandler
    b) It adds new component to persist files

So what is the best way in CQRS/EventSourcing projects to persist files? Thanx in advance for your suggestions.   

Greg Young

unread,
Oct 22, 2012, 6:20:48 AM10/22/12
to ddd...@googlegroups.com
Generally I put in a document/distributed file system and include a
link to it on the event.
--
Le doute n'est pas une condition agréable, mais la certitude est absurde.

Nikita Sushkov

unread,
Oct 22, 2012, 10:41:42 AM10/22/12
to ddd...@googlegroups.com
Thanx, Greg

I also think, that's a better way then storing byte arrays in events.   

понедельник, 22 октября 2012 г., 14:20:50 UTC+4 пользователь Greg Young написал:

Jeff Doolittle

unread,
Oct 23, 2012, 12:23:49 PM10/23/12
to ddd...@googlegroups.com
Greg,

Do you mind giving a smidge more detail?  Do you put a byte array on the command?  Do you pass that byte array down through your domain aggregates?  Or do you store the byte array higher up and pass link to that resource down to the aggregate? Something else entirely?

Thanks,

--Jeff

João Bragança

unread,
Oct 24, 2012, 3:25:52 PM10/24/12
to ddd...@googlegroups.com
Depends. Are you simply uploading the asset somewhere? Or does the command handler / aggregate care about what might be in there (maybe something like facial recognition ala Facebook) If you are just uploading then do that in the controller action (Nancy route etc) and put the link on the command too.

davidz

unread,
Oct 24, 2012, 9:16:08 PM10/24/12
to ddd...@googlegroups.com
This image (if it works) is what I'm doing (what greg said). 

 

Jeff Doolittle

unread,
Oct 25, 2012, 12:50:23 PM10/25/12
to ddd...@googlegroups.com
That image is very helpful, but it looks like you are storing the image twice, once to a local file store, and once to OneWeb.  Can you clarify what steps 1 and 2 are for, because in the diagram it looks like you could just start at step 3. Is this for failure recovery and resubmittal in case storage in the cloud fails? Or something else?

Thanks,

--Jeff
Reply all
Reply to author
Forward
0 new messages