简体   繁体   中英

Data Repository Interface in C#

So I have the following basic interface for request handlers:

public interface IRequestHandler<T> where T : EventArgs
{
    event EventHandler<T> EventHandler;

    void Start();
    void Stop();
}

These RequestHandlers are different ways to handle incoming requests, whether these come from a SMS, an Email or whatever.

Now my problem is, each of these handlers saves their data in different repositories, such as a Database, flatfile or online storage. I'd like to make it easier for myself to manage the data of these various repositories by injecting the same data storage into the various handlers that shares such a repository. However, these repositories uses different connection parameters etc.

What kind of pattern is the most suitable for this? Factory pattern seems like what I'm after, but I'm not entirely sure.

I think you are looking for dependency injection where every handler will use the same dependency injection container to get whatever resource it needs for themselves.

Going a bit simpler, without using full DI, you could make a “repository” that manages access to the actual repositories. So you would have a RepositoryRepository (feel free to invent a better name) that has methods GetDatabase() , GetOnlineStorage() etc. which return the appropriate repositories.

During construction of the handlers, you would then just need to pass the single repository repository to it.

I would say you create another IDataRepository interface that handles the saving operations and for that you'd have multiple implementations (DB, file or whatever). And then your Request handlers should depend on that interface. And every repository implementation should care of the connection strings for themselves. Something like this:

public class SmsHandler : IRequestHandler<EventArgs>
{
    private IDataRepository dataRepository;

    public SmsHandler(IDataRepository dataRepository)
    {
        this.dataRepository = dataRepository;
    }

    public event EventHandler<EventArgs> EventHandler;
    public void Start()
    {
        // save your data or something.. 
        dataRepository.Save(new object());
        // do start
    }

    public void Stop()
    {
        // do end
    }
}

public class EmailHandler : IRequestHandler<EventArgs>
{
    private IDataRepository dataRepository;

    public EmailHandler(IDataRepository dataRepository)
    {
        this.dataRepository = dataRepository;
    }

    public event EventHandler<EventArgs> EventHandler;
    public void Start()
    {
        // do start
    }

    public void Stop()
    {
        // do end
    }
}


public interface IDataRepository
{
    // or whatever you need here
    void Save(object data);
}

public class FlatFileDataRepositories : IDataRepository
{
    public void Save(object data)
    {
        // save in file
    }
}

public class DatabaseRepository : IDataRepository
{
    public void Save(object data)
    {
        // save in the database
    }
}

And then you configure Dependency Injection container to provide correct implementation of the repository. This way your Handlers are not coupled with connection strings in any way - giving you better testability and application architecture.

And don't use factories here - this pattern is getting pretty much replaced by DI containers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM