views:

42

answers:

1

I have a class:

 public class SystemQuery<T> : ISystemQuery<T> where T : class, IUIView {

    protected ISession session;
    protected ICriteria baseCriteria;

    public SystemQuery(SessionContext sessionContext) {
        this.session = sessionContext.Session;
        this.baseCriteria = session.CreateCriteria<T>();
    }

    public SystemQuery(SessionContext sessionContext, string newConnectionString)
    {
        var connection = new SqlConnection(newConnectionString);
        connection.Open();
        this.session = sessionContext.Session.SessionFactory.OpenSession(connection);
        this.baseCriteria = session.CreateCriteria<T>();
    }

StructureMap knows how to build SessionContext, ISession and ICriteria.

In another class I have (I am trying to set up an initial state in this one case):

    public T BuildQuery<T>() where T: ISystemQuery {
        return container.GetInstance<T>();
    }

    public T BuildQuery<T>(string newConnectionString) where T: ISystemQuery
    {
        var dict = new Dictionary<string, object>();
        dict.Add("newConnectionString",newConnectionString);
        return container.GetInstance<T>(new ExplicitArguments(dict));
    }

The problem is it is not overloading the constructor when it creates the instance, when it calls

container.GetInstance<T>();

by itself it does not call the single parameter constructor...instead get an error:

StructureMap Exception Code: 205 Missing requested Instance property "newConnectionString" for InstanceKey "f4fea539-2b04-4067-9c1a-990516268cea"

A: 

You can overload the constructor for a specific concrete (this is Structuremap 2.6.2; not sure about earlier or later):

ObjectFactory.Initialize(
    x => x.For<ISystemQuery>.Add<BuildQuery<T>>.Ctor<string>().Is(connectionString)
);

I will qualify this with the statement that I haven't used StructureMap to do generic resolution, and I don't know that the code above will compile as-is. But presumably you've already got your mappings compiling.

arootbeer