从Windsor容器解析一个需要数据模型依赖项的对象

时间:2018-12-27 06:12:17

标签: c# dependency-injection inversion-of-control castle-windsor

我有一个with open('E:\Master\master1.csv', 'a') as f: headers = ("Source Link,Company Link,company name") f.write(headers) f.write("\n") currentIndex = 0 for ip in content1: chrome_options.add_argument('--proxy-server=%s' % ip) try: for link in content[currentIndex:]: # start from 0 or continue from last index error try: browser.get(link) browser.implicitly_wait(4) abba = browser.find_element_by_css_selector('.WebToolsetToolWebPart_Cntnr.WebToolsetToolWebPart_Aligned_LEFT.WebToolsetToolWebPart_TxtTool_Cntnr') aas = abba.text aa = aas.replace(",","") print(ip + "," + link + "," + aa) f.write(link + "," +aa+"\n") except NoSuchElementException: aa = "no count available" print(ip + ","+link + "," + aa) f.write(link + "," + aa + "\n") break # stop this inner loop and continue outer loop # current loop is good save the next index currentIndex += 1 # if it last index of "content", reset the index <- minor fix if currentIndex == len(content) - 1: currentIndex = 0 except StaleElementReferenceException: pass 对象,该对象的构造函数中需要两个参数:在AccountViewModel中注册的DataStore对象和一个WindsorContainer数据模型对象。

现在,当用户在帐户列表中选择一个帐户时,我需要使用所选帐户从容器中Accountresolve对象。

但是问题是帐户未在容器中注册,并且在AccountViewModel事件中注册该帐户时,我遇到了重复的注册错误。

我还研究了每种依赖关系的各种生命周期,但是我仍然找不到解决方案(因为我更喜欢自己的工厂类,所以我显然是使用IoC框架的初学者)。

2 个答案:

答案 0 :(得分:2)

按原样保留AccountViewModel,但保留use a factory for dependency injection

public interface IAccountViewModelFactory
{
    AccountViewModel Create(AccountModel model);
}

然后您可以像这样实现工厂(并在您的DI容器中注册):

public class AccountViewModelFactory : IAccountViewModelFactory
{
    public AccountViewModelFactory(IAccountService accountService)
    {
        AccountService = accountService;
    }

    public IAccountService AccountService { get; }

    public AccountViewModel Create(AccountModel model)
    {
        return new AccountViewModel(AccountService, model);
    }
}

假设AccountViewModel看起来像这样:

public class AccountViewModel
{
    public AccountViewModel(IAccountService accountService, AccountModel model)
    {
        AccountService = accountService;
        Model = model;
    }

    public IAccountService AccountService { get; }
    public AccountModel Model { get; }
}

答案 1 :(得分:1)

从构造函数中排除数据对象,并通过初始化方法传递数据。

public class AccountModel
{
    public int Id { get; set; }
    // some more properties
}

public interface IAccountService
{
    Task<AccountModel> GetByIdAsync(int id);
}

public class AccountViewModel
{
    public AccountViewModel(IAccountService accountService)
    {
        AccountService = accountService;
    }

    protected IAccountService AccountService { get; }

    private Task LoadFromModelAsync(AccountModel model)
    {
        Id = model.Id;
        _originalModel = model;
        return Task.CompletedTask;
    }

    private AccountModel _originalModel;
    public int Id { get; private set; }

    public async Task InitializeAsync(object parameter)
    {
        switch (parameter)
        {
            case null:
                await LoadFromModelAsync(new AccountModel());
                break;
            case int id:
                {
                    var model = await AccountService.GetByIdAsync(id);
                    await LoadFromModelAsync(model);
                    break;
                }
            case AccountModel model:
                await LoadFromModelAsync(model);
                break;
            default:
                throw new InvalidOperationException();
        }
    }
}