Skip to content

DevExpress-Examples/XAF_how-to-store-file-attachments-in-the-file-system-instead-of-the-database

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How to: Store file attachments in the file system instead of the database (XPO and EF Core)

Basics

This example contains a FileSystemDataModule - an XAF module that you can reuse in your own applications. You will be interested in two types that allow you to attach a file to an object. Both types do not store the files in the database, but keep them in the file system:

  • FileSystemStoreObject - stores files in a centralized file system location instead of the database. Use the module's static FileSystemStoreLocation property to specify the file location.
  • FileSystemLinkObject - stores soft links to files instead of saving their contents to the database. This type is intended for use in WinForms applications only.

Both types implement the IFileData interface and thus work with the DevExpress File Attachments module.

Refer to the following video to see this functionality in action: http://www.screencast.com/t/Xl1GMfxw.

Steps to implement

  1. Copy the "FileSystemData" project and include it into your solution. Rebuild the solution and make sure the new project was built successfully.
  2. Register the FileSystemDataModule in the YourSolutionName.Module/Module.xx file as described in the following help topic: Ways to Register a Module > Add a Module in Code.
  3. Extend a business class with a property that will hold file attachments. Choose the FileSystemStoreObject or FileSystemLinkObject type for the property. See descriptions above to compare the two types.
  4. Add the following attributes to the file attachment property: Aggregated, ExpandObjectMembers(ExpandObjectMembers.Never) and ImmediatePostData.
  5. Decorate the business class with the FileAttachmentAttribute that links to the new property. This attribute enables additional file management UI commands.
  6. Handle the CustomOpenFileWithDefaultProgram event of the DevExpress.ExpressApp.FileAttachments.Win.FileAttachmentsWindowsFormsModule class. Review the implementation in this example (E965.Win\WinApplication.xx). Move that code to the following file in your project: YourSolutionName.Win/WinApplication.xx.
  7. XPO Only. Make sure that your application and related modules do not override the DevExpress.Persistent.BaseImpl.BaseObject.OidInitializationMode property. The property must return OidInitializationMode.AfterConstruction to ensure the correct operation of this module. (FileSystemDataModule sets this value as needed.)

Important Notes

  1. The FileSystemLinkObject class can be used in WinForms applications only.
  2. The current version of this example does not support the middle-tier scenario. For more information, refer to the following ticket: A problem occurs when file attachments are stored in the file system with middle-tier configuration.
  3. If you plan to migrate existing FileData objects from the database to a file system, use the techniques described in the following topic: Data Manipulation and Business Logic. The article explains how you can read FileData objects and create new FileSystemStoreObject objects based on their content. Since both classes implement IFileData, you can call their LoadFromStream() and SaveToStream() methods to copy data. Even though we do not provide an example for this migration procedure, we hope that the snippet below help you get started:
// Use any other IObjectSpace APIs to query required data.
FileData fd = ObjectSpace.FindObject<FileData>(null); 
FileSystemStoreObject fss = ObjectSpace.CreateObject<FileSystemStoreObject>();
Stream sourceStream = new MemoryStream();
((IFileData)fd).SaveToStream(sourceStream);
sourceStream.Position = 0;
((IFileData)fss).LoadFromStream(fd.FileName, sourceStream);
ObjectSpace.CommitChanges();

Note that you can rework this code to use UnitOfWork instead of IObjectSpace.

Files to Review (XPO)

Files to Review (EF Core)

Documentation

See Also

About

How to store file attachments in the file system instead of the database in eXpressApp Framework

Topics

Resources

License

Stars

Watchers

Forks