Search code examples
c#asp.netrepository-patterninfluxdbunit-of-work

Is it necessary to implement Unit of work patterns when using time series database.(ex: influxDb)


I am working on project which uses EF for sql database and infulx data access for historic data. I have implemented unit of work patterns for the part of sql: _context.Save(); but i was not sure if its a good idea to implementing unit of work on the project that access data from influxDb. Influx uses converter and client action, and has not such saveChanges() method.

I have just tried a repository pattern without unti of work.


Solution

  • Repository can work without Unit Of Work, so it can also have Save method.

    The Unit Of Work pattern is employed in scenarios where multiple repositories, possibly using different data contexts, are involved. It allows tracking all changes made within a transaction until the Commit method is invoked to persist those changes to the database (or file, in this case).

    When you interact with the Repository by using Add, Update, or Remove operations, the Unit Of Work solely modifies the status of the entity by marking it as Added, Removed, or Dirty. However, the actual persistence of these changes occurs only when the Commit method is called. At this point, the Unit Of Work iterates through the repositories and executes the necessary persistence actions.

    The Unit Of Work's behavior depends on whether repositories share the same data context or have different ones. When repositories share the same data context, the Unit Of Work can directly work with that data context, leading to higher performance when accessing and updating the database or file.

    On the other hand, if repositories have different data contexts, representing different databases or files, the Unit Of Work handles the persistence by invoking the Save method for each repository within the same TransactionScope, ensuring that all changes are committed in a consistent manner.