简体   繁体   中英

Dependency Injection containers - how do I get a specific instance of an object?

For example, I have a Repository class for getting data from a database, and there are several service classes, let's say Service1, Service2, Service3.

I will have multiple Repository instances, for example, for two or three databases. And, you should be able to configure services to work with a specific database.

I can't figure out how to implement these dependencies using the Dependency Injection container.

As far as I understand, I can register the Repository service either as a Singleton, or a new instance will be created for each dependency.

But, I only need two repositories, Repository ("DB1") and Repository ("DB2"), and when creating a service instance, I should be able to choose which database to work with. That is, as an option-Service1(Repository ("DB1")), Service2 (Repository ("DB1")), Service1 (Repository ("DB2")).

For example:

public class Program
{
    static void Main()
    {
        var connectionStringDb1 = "DB1 connection string";
        var connectionStringDb2 = "DB2 connection string";

        var repositoryDb1 = new Repository(connectionStringDb1);
        var repositoryDb2 = new Repository(connectionStringDb2);

        var smsSendService1 = new SmsSendService(repositoryDb1);
        var smsSendService2 = new SmsSendService(repositoryDb2);
        var emailSendService1 = new EmailSendService(repositoryDb1);

        smsSendService1.Run();

        var tasks = new Task[]
        {
            smsSendService1.Run(),
            smsSendService2.Run(),
            emailSendService1.Run()
        };

        Task.WaitAll(tasks);
    }
}

public class Repository
{
    private string _connectionString;
    public Repository(string connectionString)
    {
        _connectionString = connectionString;
    }

    public object GetData()
    {
        // Getting data from the Database
        var data = ...;
        return data;
    }
}

public class SmsSendService
{
    private readonly Repository _repository;
    public SmsSendService(Repository repository)
    {
        _repository = repository;
    }

    public Task Run()
    {
        return Task.Run(() =>
        {
            // Sending SMS in a loop
            while (true)
            {
                var data = _repository.GetData();
                // ...
                Task.Delay(1000);
            }
        });
    }
}

public class EmailSendService
{
    private readonly Repository _repository;
    public EmailSendService(Repository repository)
    {
        _repository = repository;
    }

    public Task Run()
    {
        return Task.Run(() =>
        {
            // Sending Email in a loop
            while (true)
            {
                var data = _repository.GetData();
                // ...
                Task.Delay(1000);
            }
        });
    }
}

尝试看看 autofac 命名实例

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