如何使用简单的注入器在运行时更改依赖项

拉贾巴尔

我是简单注射器的新手。我有依赖于Force Client的数据访问层。我已经注册了ForceClient依赖项。我想替换ForceClient一次用户登录到应用程序的默认值

请让我知道,我如何在运行时更改默认值。

Ioc.ServiceContainer.Register(() => new ForceClient(
    "test",
    "test",
    "test"));

这是有关要求的完整详细信息。我在Xamarin项目中拥有DAL,该DAL使用Developerforce.Force API从销售人员中检索数据。我正在编写使用MOQ来测试DAL的单元测试用例。

DAL代码。

public CustomerRepository(IForceClient client)
{
    _client = client;                       
}

public async Task<long> GetTotalContacts()
{
    string totalContactCountQry = "some query"

    var customerCount =  await _client.QueryAsync<ContactsTotal>(totalContactCountQry);
    var firstOrDefault = customerCount.Records.FirstOrDefault();

    return firstOrDefault != null ? firstOrDefault.Total : 0;
}

单元测试用例代码。

[SetUp]
public void Init()
{
    forceClientMock = new Mock<IForceClient>();
    forceClientMock.Setup(x => x.ForceClient(It.IsAny<string>(), 
        It.IsAny<string>(), It.IsAny<string>(), It.IsAny<HttpClient>()))
        .Return(new ForceClient(It.IsAny<string>(), It.IsAny<string>(), 
            It.IsAny<string>(), It.IsAny<HttpClient>()));              
    forceClientMock.Setup(x => x.QueryAsync<ContactsTotal>(It.IsAny<string>()))
        .ReturnsAsync(new QueryResult<ContactsTotal>());
    forceClientMock.Setup(x => x.QueryAsync<ContactsTotal>(It.IsAny<string>()))
        .ReturnsAsync(new QueryResult<ContactsTotal>() { Records=new List<ContactsTotal>() });            
}

[Test]
public void GetTotalContacts()
{              
    ICustomerRepository customerRepostory = new CustomerRepository(forceClientMock.Object);
    Assert.AreEqual(customerRepostory.GetTotalContacts().Result,0);
}

应用程序初始化时使用简单的Injector注册表

container.Register<IForceClient>((() => new ForceClient(
    UserState.Current.ApiBaseUrl,
    UserState.Current.AuthToken.AccessToken,
    UserState.Current.ApiVersion)), Lifestyle.Transient);

我正在注册表中创建的ForceClient实例正在使用所有默认值UserState创建。用户登录到应用程序后,便会分配实际值。

除了ForceClient实例,我在登录后具有更新的值以访问销售人员以检索数据,但是该程序在DAL行下方给出了错误

var customerCount =  await _client.QueryAsync<ContactsTotal>(totalContactCountQry);

原因是forceClient仍然包含默认值。我如何确保登录后创建FoceClient实例以使用UserState的实际值

汤玛士

您可以通过使用完成您想要的Func<T>

除了IForceClient在课堂上,您还可以插入Func<IForceClient>

public CustomerRepository(Func<IForceClient> clientFunc)
{
    _clientFunc = clientFunc;
}

public async Task<long> GetTotalContacts()
{
    string totalContactCountQry = "some query"

    // calling _clientFunc() will provide you a new instance of IForceClient
    var customerCount =  await _clientFunc().QueryAsync<ContactsTotal>(totalContactCountQry);
    var firstOrDefault = customerCount.Records.FirstOrDefault();

    return firstOrDefault != null ? firstOrDefault.Total : 0;
}

简单的喷油器注册:

// Your function
Func<IForceClient> fonceClientFunc = () => new ForceClient(
    UserState.Current.ApiBaseUrl,
    UserState.Current.AuthToken.AccessToken,
    UserState.Current.ApiVersion);

// the registration
container.Register<Func<IForceClient>>( () => fonceClientFunc, Lifestyle.Transient);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用依赖项注入在运行时确定实现

使用Google Guice在运行时注入依赖项

如何在运行时动态更改依赖项

如何在运行时控制OSGi中的依赖项注入

ASP.NET Core依赖项注入:服务在运行时使用Func委托解析

简单注入器:在运行时替换注册类型

如何实现春天依赖注入在运行时?

如何在运行时使用依赖注入创建新对象

在运行时更改注入的对象

简单注入器-在运行时基于指定的泛型类型注入服务

C#简单注入器,我可以在运行时注入不同的类吗

温莎城堡在嵌套类中注入依赖项,根接口在运行时解析

如何使用 Dagger 2 在运行时注入字段?

当依赖项需要运行时值时,如何注入依赖项?

在运行时注入/管理,使用实体框架,依赖注入,工作单元和存储库模式来更改连接字符串

在运行时初始化依赖项

Firebase依赖项在运行时导致错误

使用Gunicorn运行时如何在运行时更改flask配置变量

使用Typhoon在运行时注入关闭项时,EXC_BAD_ACCESS

Guice运行时依赖项参数重新注入

Google Guice运行时依赖项注入

如何在运行时根据用户选择安装依赖项?

在运行时进行简单的喷油器更改注册

在运行时从Angular注入器检索实例

使用spring在运行时注入BasicDataSource

如何使用Google Guice在运行时基于注释注入接口实现

如何在运行时使用dagger.android从捆绑中注入参数?

如何在运行时使用JavaScript为网页注入控件?

在运行时更改记录器