views:

208

answers:

2

I have just updated to the latest structuremap dll and now my site no longer works at runtime due to deprecated methods in structuremap so I am trying to update my bootstrapper code.

In my bootstrapper class I have rewritten it to:

public class Bootstrapper
{
    public static void ConfigureStructureMap()
    {
        ObjectFactory.Initialize(InitializeStructureMap);
    }

    private static void InitializeStructureMap(IInitializationExpression x)
    {
        x.AddRegistry(new DatabaseServiceRegistry());                        
        x.For<IArticleService>().Use<ArticleService>();
        x.For<IArticleRepository>().Use<SqlArticleRepository>();
    } 
}

I have added the registry line as I am using Linq to SQL which is in a seperate project.

This is the code that worked in the older version of StructureMap:

public class DatabaseServiceRegistry : Registry
{
    public override void ConfigureStructureMap()
    {
        ForRequestedType<Database>()
        .TheDefaultIs(() => new Database(Settings.Default.server))
        .CacheBy(InstanceScope.Hybrid);                
    }

I think I need to rewrite it to something like this?

public void ConfigureStructureMap()
{
    ObjectFactory.Configure(x =>
    {                
        For<Database>().Use(new Database(Settings.Default.server));                
    });
}

This compiles but then I get a runtime error of:

StructureMap Exception Code: 202 No Default Instance defined for PluginFamily MyProject.Data.SqlRepository.Database

What am I doing wrong? I am finding it hard to find documentation that relates to the latest syntax and not referencing deprecated methods :(

+1  A: 

Use ObjectFactory.Initialize instead if configure :

ObjectFactory.Initialize(x =>
        {


            x
                .For<IProductsRepository>()
                .LifecycleIs(Lifecycles.GetLifecycle(InstanceScope.PerRequest))
                .Use<SqlProductsRepository>()
                .Ctor<string>("connectionString")
                .Is(@"Data Source=.\sqlexpress2008;Initial Catalog=SportsStore;Persist Security Info=True;User ID=xxxxx;Password=xxxxxxx");


        });

Hope this helps.

ali62b
+2  A: 

You are almost right on with your new 2.5.4 syntax. You are just missing the Hybrid configuration. I tried to reproduce your problem and could not (using trunk or the 2.5.4 release). Here is my successful test.

    public class DatabaseServiceRegistry : Registry
{
    public const string DefaultSettings = "my settings";

    public DatabaseServiceRegistry()
    {
        For<Database>().HybridHttpOrThreadLocalScoped().Use(new Database(DefaultSettings));
    }
}

public class Database
{
    public string Settings { get; set; }

    public Database(string settings)
    {
        Settings = settings;
    }
}

[TestFixture]
public class Structure_map_configuration
{
    [Test]
    public void TEST()
    {
        ObjectFactory.Initialize(cfg =>
        {
            cfg.AddRegistry<DatabaseServiceRegistry>();
        });

        ObjectFactory.GetInstance<Database>().Settings.ShouldEqual(DatabaseServiceRegistry.DefaultSettings);
    }
}

Is it possible that you have two classes named 'Database' and are configuring the one of them in your registry and a different one in your service location?

KevM
Thanks that fixed it, it could really do with more documentation! :D
KevinUK
I agree that 2.5.4 features are not well documented yet. Jeremy knows this and has mentioned on Twitter that it is his next focus.
KevM
KevM