0

I have a problem. I'm implementing a selfhost web api based on Owin and Castle. My interface looks like this:

public interface IFluentStoredProcedureRunner<T> where T : class
{
    FluentStoredProcedureRunner<T> WithName(string storedProcedureName);

    FluentStoredProcedureRunner<T> WithParameters(List<StoredProcedureParameterDefinition> parameterList = null);

    StoredProcedureResult<T> Run();
}

and the implementation looks like this:

public class FluentStoredProcedureRunner<T> : IFluentStoredProcedureRunner<T> where T : class
{
    private readonly IStoredProcedureRunner<T> storedProcedureRunner;

    private string storedProcedureName = string.Empty;

    private List<StoredProcedureParameterDefinition> parameterList = new List<StoredProcedureParameterDefinition>();

    public FluentStoredProcedureRunner(IStoredProcedureRunner<T> storedProcedureRunner)
    {
        this.storedProcedureRunner = storedProcedureRunner;
    }

    public FluentStoredProcedureRunner<T> WithName(string storedProcedureName)
    {
        this.storedProcedureName = storedProcedureName;

        return this;
    }

    public FluentStoredProcedureRunner<T> WithParameters(List<StoredProcedureParameterDefinition> parameterList = null)
    {
        this.parameterList = parameterList;

        return this;
    }

    public StoredProcedureResult<T> Run()
    {
        return this.storedProcedureRunner.Run(this.storedProcedureName, this.parameterList);
    }
}

and the registrations:

public class CoreLibInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(Component.For(typeof(IMapper<>))
            .ImplementedBy(typeof(MapByFieldNameMapper<>))
            .LifestyleTransient());

        container.Register(
            Component.For(typeof(IStoredProcedureRunner<>))
            .ImplementedBy(typeof(StoredProcedureRunner<>))
            .LifestyleTransient());

        container.Register(
            Component.For(typeof(IFluentStoredProcedureRunner<>))
                .ImplementedBy(typeof(FluentStoredProcedureRunner<>))
                .LifestyleTransient());

        container.Register(
            Component.For<IAuthenticationService>()
                .ImplementedBy<ActiveDirectoryAuthenticationService>()
                .LifestyleTransient());

        container.Register(
            Component.For<IStoredProcedureNameResolverService>()
                .ImplementedBy<StoredProcedureNameResolverService>()
                .LifestyleTransient());
    }
}

Here is the stored procedure runner:

public class StoredProcedureRunner<T> : IStoredProcedureRunner<T> where T : class, new()
{
    private const string ConnectionString = "someConnectionString"


    private readonly IMapper<T> mapper;

    private readonly StoredProcedureResult<T> storedProcedureResult = new StoredProcedureResult<T>();

    public StoredProcedureRunner(IMapper<T> mapper)
    {
        this.mapper = mapper;
    }

    public StoredProcedureResult<T> Run(
        string storedProcedureName,
        List<StoredProcedureParameterDefinition> parametersList = null)
    {
        try
        {
            using (var conn = new IfxConnection(ConnectionString))
            {
                conn.Open();

                using (var cmd = new IfxCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = storedProcedureName;
                    cmd.CommandType = CommandType.StoredProcedure;

                    if (parametersList != null)
                    {
                        foreach (var parameter in parametersList)
                        {
                            var parameterToAdd = new IfxParameter
                                                {
                                                    ParameterName = parameter.ParameterName,
                                                    IfxType = parameter.ParameterType,
                                                    Value = parameter.ParamerValue,
                                                    Direction = parameter.ParameterDirection
                                                };

                            cmd.Parameters.Add(parameterToAdd);
                        }
                    }

                    var ifxdr = cmd.ExecuteReader();

                    this.storedProcedureResult.Rows = this.mapper.Map(ifxdr).ToList();

                    if (parametersList != null)
                    {
                        foreach (var outputParameter in parametersList.Where(pl => pl.ParameterDirection == ParameterDirection.Output))
                        {
                            this.storedProcedureResult.OutputParameters.Add(
                                outputParameter.ParameterName, 
                                cmd.Parameters[outputParameter.ParameterName].Value);
                        }
                    }

                    return this.storedProcedureResult;
                }
            }
        }
        catch (Exception ex)
        {
            throw new ApplicationException(ex.Message);
        }
    }
}

This is the interface for it:

public interface IStoredProcedureRunner<T> where T : class 
{
    StoredProcedureResult<T> Run(
        string storedProcedureName, 
        List<StoredProcedureParameterDefinition> parametersList = null);
}

The problem is that after all of that It is throwing the exception:

ComponentActivator: could not instantiate StoreProcRunLib.DB.StoredProcedureRunner.FluentStoredProcedureRunner`1[[PromakGateway.WebApi.Models.InactiveAccountsTransferModel, PromakGateway.WebApi, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]

I've seen here Castle Windsor - IoC registration for open generic interfaces? that there is an adapter of some kind but I don't know how it looks like. Thanks in advance.

Community
  • 1
  • 1
Rafal_Koscinski
  • 317
  • 5
  • 22
  • 2
    Did you check inner exceptions? – Matías Fidemraizer Oct 13 '15 at 09:09
  • What does `StoredProcedureRunner` look like? – DavidG Oct 13 '15 at 09:18
  • On a side note: Why are you returning the class types inside your interface instead of the interface types itself? – Silvermind Oct 13 '15 at 09:22
  • hmm I found some tutorial about creating fluent api and I guess I went this track. Could that be a problem? – Rafal_Koscinski Oct 13 '15 at 09:26
  • What @Silvermind said may actually be your problem, the interface is tied directly to the implementation. – DavidG Oct 13 '15 at 09:27
  • @MatíasFidemraizer I just did, I did not think of that when focusing on this, I checked that threre are some errors regarding one dll from IBM i'm investigating on that, if this could be it. – Rafal_Koscinski Oct 13 '15 at 09:27
  • @DavidG I just changed that the way Silvermind suggested. I have the same exception – Rafal_Koscinski Oct 13 '15 at 09:41
  • in fact @MatíasFidemraizer helped. It was the Informix dll which was not loading. I'm having issues with it since I'm developing an app over it. But with the fighting and an exception suggesting that the fluentstoredproceduirerunner is raising an exception I did not think of looking in the inner exception where the real issue was. Thanks guys. – Rafal_Koscinski Oct 13 '15 at 10:53

0 Answers0