开发者

Pattern for Resolving Nested Dependencies at Run-Time using an IoC Container

I have a class that coordinates preparation, validation, and cleansing routines for a package for data files. I am struggling to find a pattern that feels right when using an IoC container (Ninject in this case). The problem I am running into is that the depending on what is delivered in the package I could be using one of several validation classes. Here is a rough idea of what is going on:

public class PackageProcessor
{
    private readonly ILog log;

    public PackageProcessor(ILog log)
    {
        this.log = log;
    }

    public void Process(CustomerProfile profile, PackageType type, string path, bool validateOnly = false, bool offlineValidation = false)
    {
        // ...
        // Uncompress package files (if compressed) or copy raw files to woroking folder
        // Ensure working folder contains the required files for the package type
        // Validate package data
        foreach (var packageFile in packageConfiguration)
        {
            var timer = Stopwatch.StartNew();
            var recordCount = 0UL;
            var validator = DependencyResolver.Kernel.Get<RecordValidator>(metadata =>
                metadata.Has("file") &开发者_开发问答;&
                String.Equals(metadata.Get<string>("file"), packageFile.Name, StringComparison.OrdinalIgnoreCase));

            using (var reader = new CsvReader(new StreamReader(Path.Combine(workingFolder.FullName, packageFile.Name)), false))
            {
                while (reader.ReadNextRecord())
                {
                    var recordResult = validator.IsValid(reader);
                    if(!recordResult.IsValid)
                    {
                        // LOG: record error messages
                        // Mark the job as failed
                    }
                    recordCount++;
                }
            }

            // LOG: File appears to be valid. {0} records were found.
            // LOG: File contains invalid records. {0} records were found, {1} were invalid.

            timer.Stop();
            log.Info(m => m(Strings.RecordsProcessed, recordCount, timer.Elapsed, (recordCount / timer.Elapsed.TotalSeconds)));
        }
        // Clean and output the data
    }
}

As you can see I have a need to resolve the validation class dynamically as part of the process. In the past I would have created a Factory class to locate the proper validator and return it. I am leaning towards injecting that factory through the constructor but wanted to put it out there to see if there are any better approaches that you might have encountered for dealing with nested dependency resolution without having to pass around IoC container references.

(There might be a duplicate question, but I'm still trying to work through if we're asking the same thing)


Assuming that the validators and the processor are reusable I'd take a completely other approach. I'd inject all the RecordValidators in the constructor and add a CanHandle(??? packageFile) method to them and select the matching one in the loop or alternatively inject a validator selector having all the validators and get it from this one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜