Is it correct that this DataStore service depends on the Serialization service?

Viewed 25

There is a IDataStore interface which has this contract:

public interface IDataStore
{
    Task<T> CreateEntity<T>(string entityName, string entityId, T entity);
    Task<T> GetEntity<T>(string entityName, string entityId);
}

One of it's implementation is the FileSystemDataStore:

public class FileSystemDataStore : IDataStore
{
    private readonly ISerializer _serializer;
    public Task<T> CreateEntity<T>(string entityName, string entityId, T entity)
    {
        var obj = (object?)entity;

        /*
         * Convert entity to obj ot type Object
         * Serialize obj which the _serializer service 
         * and store it in the file system under entityName/entityId.myExtension
         */

        return Task.FromResult(entity);
    }

    public Task<T> GetEntity<T>(string entityName, string entityId)
    {
        throw new NotImplementedException();
    }
}

Note the commented Code

FileSystemDataStore now has ISerializer injected, so when configuring the DI container we must explicitly provide the ISerializer implementation.

In a large application with a big dependency graph, isn't this a high risk of a mess of circular dependencies ? What is the correct way of solving this simple problem which can be applied on large projects?

1 Answers

It looks like FileSystemDataStore some type of implementation of repository pattern. It is better to keep without any logic. It should have only CRUD logic. Why? Because repositories are to execute only some CRUD logic.

So I would suggest to move serialization logic into business layer instead of mixing Serialization with DAL.

Related