Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将运行时数据注入依赖项_C#_Dependency Injection - Fatal编程技术网

C# 将运行时数据注入依赖项

C# 将运行时数据注入依赖项,c#,dependency-injection,C#,Dependency Injection,我有一个web应用程序,它有一个相当复杂的过程,首先从数据库中获取一组数据以在整个过程中使用(这个过程和数据的生命周期是一个请求)。数据是从几个不同的表中提取的,并且基于运行时传入的一些数据 此流程的入口点从使用此数据的依赖项调用方法。但是,它感觉不太正确,因为看起来依赖项应该真正依赖于DataAccessor,而不是在entry方法(DoStuff)中初始化它,然后在整个过程中传递它,但是由于DataAccessor的状态基于传递到DoStuff方法的数据,所以我不能真正做到这一点 publi

我有一个web应用程序,它有一个相当复杂的过程,首先从数据库中获取一组数据以在整个过程中使用(这个过程和数据的生命周期是一个请求)。数据是从几个不同的表中提取的,并且基于运行时传入的一些数据

此流程的入口点从使用此数据的依赖项调用方法。但是,它感觉不太正确,因为看起来依赖项应该真正依赖于DataAccessor,而不是在entry方法(DoStuff)中初始化它,然后在整个过程中传递它,但是由于DataAccessor的状态基于传递到DoStuff方法的数据,所以我不能真正做到这一点

public class Service {

    private readonly IRepository repo;
    private readonly Analyzer analyzer;
    private readonly Validator validator;     

    public Service(IRepository _repo, Validator _validator, Analyzer _analyzer){
        repo = _repo;
        validator =_validator;
        analyzer = _analyzer;
    }

    public void DoStuff(string sourceId, AnalysisConfig config, List<Item> items){

        // go get data used for this process based on the runtime data
        // it's going to be a subset of the database data based on the runtime data passed into this method
        DataAccessor data = BuildDataAccesor(config, items);

        foreach(var item in items){
            ProcessItem(sourceId, config, data, item);
        }

    }

    //this method doesn't really need all these parameters, I'm only passing them in so they make it through to the other dependencies
    public void ProcessItem(string sourceId, AnalysisConfig config, DataAccessor data, Item item){

        //do some stuff

        //then
        analyzer.AnalyzeItem(sourceId, config, data, item);
        validator.ValidateItem(sourceId, data, item );

   }


    // this goes an gets some data from the database to store in memory for the length of the DoStuff method
    public DataAccessor BuildDataAccesor(AnalysisConfig config, List<Item> items){
        DataAccessor data = new DataAccessor();
        data.FooCollection = repo.GetSomeData(config);
        data.BarCollection = repo.GetSomeOtherData(config);
    }


}
另一个呢

public class Validator {

    public void ValidateItem(string sourceId, DataAccessor data, Item item){
       //do some stuff with the item looking up stuff from the data blob
    }

}

这将比这更复杂,但我认为这抓住了要点。有没有办法重构它,这样我就可以用运行时数据初始化DataAccessor并注入到服务的依赖项中?

听起来你可以使用一个
IDataAccessorFactory
,你可以调用
DataAccessor Create()
打开它。但是,让它成为主类依赖项的最佳方法是什么?他们不能将工厂作为依赖项,因为工厂需要来自DoStuff方法的运行时数据来初始化DataAccessor
public class Validator {

    public void ValidateItem(string sourceId, DataAccessor data, Item item){
       //do some stuff with the item looking up stuff from the data blob
    }

}