문제

I have difficulty registering following class in StructureMap container. Only time I see this class getting registered if I change the parameter type from String to an object type. What am I doing wrong?

public class SomeCommand : ICommand
{

    public SomeCommand(String path)
    {
        this.Path = path;
    }
    public string Path { get; private set; }


    public Guid CommandId
    {
        get { return null; }
    }
}

public class ObjectsRegistry : StructureMap.Configuration.DSL.Registry
{
    public ObjectsRegistry()
    {
        Scan
        (
            (scanner) =>
            {
                scanner.TheCallingAssembly();
                scanner.Assembly(Assembly.GetExecutingAssembly());
                scanner.WithDefaultConventions();
                scanner.RegisterConcreteTypesAgainstTheFirstInterface();
                scanner.AddAllTypesOf(typeof(ICommand));
            }
        );
    }     
}
도움이 되었습니까?

해결책

StructureMap cannot automatically resolve primitive data types.

If you know the value at registration time you can use this syntax

ObjectFactory.Initialize(x =>
{
    x.For<ICommand>()
        .Use<SomeCommand>()
        .Ctor<string>("path")
        .Is("");
});

If you're using an appSetting

ObjectFactory.Initialize(x =>
{
    x.For<ICommand>()
        .Use<SomeCommand>()
        .Ctor<string>("path")
        .EqualToAppSetting("key");
});

However, if you want a different path value for each instance of ICommand then StructureMap cannot create these instances for you. You could, for example, define a ICommandBuilder abstraction for creating instances of ICommand or make Path a settable property.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top