Files in the domain model

What are the best practices for working with binaries in a domain model? I often have to associate images and other files with business objects, and a simple byte [] is not suitable even for the simplest case.

Files:

  • It does not have a fixed size and can be quite large, thus:
    • Have to be streamed or buffered, preferably asynchronously;
    • It must be cached both on the server and on the client to avoid excessive transfer;
    • In untrusted connections, data transfer can be easily interrupted and must be resumed - therefore, the transfer can begin not from the beginning of the file, but from an arbitrary position.
  • Processed differently than the rest of the data:
    • In web applications, part of the page content is not included, but downloaded separately by the browser;
    • There may be a black box that is processed by third-party software;
    • For performance reasons, they may not even be stored in the database.

How are we going to express such files in a domain model (or, more specifically, in model classes)? If the rest of the model is transmitted through the DTO and WCF web services and stored in NHibernate in the database, but the files are not necessarily how to make the file processing transparent, part of the overall transaction, where applicable, but does not support everything that is necessary for their consumption only in web applications, but also in regular desktop applications.

For WPF and ASP.NET, the file object must expose some form of the Url property, which can be attached to data in WPF elements or used in IMG or HTML tags. Downloading a file is much more complicated. Preferably, appropriate presentation and content methods such as MVVM should be supported there.

I am really lost here, as none of my previous decisions satisfy me. What would you suggest?

+4
source share
1 answer

You have to be careful not to try to use too many functions in one class, your wording sounds just like you need one “file” that will do everything, this is not a good idea.

You will need to have an idea of ​​the file representation, which can be transmitted everywhere as you defined, but it should be a little more than an identifier and, possibly, a name, then for individual components you need to decide how they handle it, for example, HTML the page can use the File json object and infer that jsFile.Id needs to be obtained using ftp: // xxx / uploads / {id} or something else, while the WCF service can get the file identifier to display additional related information look for information in the database.

It probably makes sense to have the FileAttributesDTO class, or some, to distinguish it from when you are dealing with a physical file. You must take into account the division of problems and nail as many cases as you can before proceeding with the action. For example, you really need more information, or a simple wrapper around the FTP service will give you everything you need.

0
source

All Articles