Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Castle Windsor Resolve at run time

I have been trying to fix this issue for quite a while and I am still none the wiser. I have got the following method:

public IResult Parse(string[] args)
{
    var argumentOption = new ArgumentOption(_dataModelBinder);
    var boundArgumentOption = argumentOption.Bind(args);

    var bindingResults = boundArgumentOption.Validate(_argumentOptionValidator);

    // AREA OF INTEREST
    if (bindingResults.Any())
    {
        return new ErrorResult();
    }

    return new CreateReportResult(
        _resultActioner
        , boundArgumentOption.OutputFilePath
        , boundArgumentOption.PatientId
        , "database");
}

The code I'm having trouble with involves the return values which I'm newing up, which ideally I'd like to leave to Castle Windsor to deal with. So, what I then did was to create an Abstract factory:

public interface IResultFactory
{
    IResult Create(int numOfErrors);
} 

public class ResultFactory : IResultFactory
{
    private readonly IWindsorContainer _container;

    public ResultFactory(IWindsorContainer container)
    {
        _container = container;
    }

    public IResult Create(int numOfErrors)
    {
        if (numOfErrors > 0)
        {
            return _container.Resolve<IResult>("ErrorResult");
        }

        return _container.Resolve<IResult>("CreateReportResult");
    }
}

and my Parse method now becomes:

public IResult Parse(string[] args)
{
    var argumentOption = new ArgumentOption(_dataModelBinder);
    var boundArgumentOption = argumentOption.Bind(args);

    var bindingResults = boundArgumentOption.Validate(_argumentOptionValidator);

    IResult result = _factory.Create(bindingResults.Count());

    return result;
}

What I'm having a great deal of problem with is how to do the registration and dynamically pass in the parameters because the constructor for CreateReportResult is:

public CreateReportResult(IResultActioner resultActioner, Uri filePath, string patientId, string dataSource)

So the question is how do I set up my registration code in my WindsorContainer installer and how do I then pass in the required parameters? I am using Castle Windsor 3.2.

Here is the code I have in my registration:

container.Register(
  Component
    .For<IResult>()
    .ImplementedBy<ErrorResult>()
    .Named("ErrorResult")
    .LifeStyle.Transient
    , Component.For<IResultFactory>()
    .AsFactory()
);

container.Register(
  Component
    .For<IResult>()
    .ImplementedBy<CreateReportResult>()
    .Named("CreateReportResult")
    .LifeStyle.Transient
    , Component.For<IResultFactory>()
    .AsFactory()
);
like image 983
DavidS Avatar asked May 08 '13 09:05

DavidS


1 Answers

I think you should be looking at the typed factory facility in windsor. The paramaters are passed along, as long as they have the same name in the implementation as in the registration.

like image 181
Martin Avatar answered Nov 20 '22 04:11

Martin