2
votes

I am currently setting the connection string for my linq to sql data context by using a wrapper class so that I can pass a connection string into the generated DataContext constructor:

    public class DB : GeneratedDataContext {
    public DB() : base(ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString) { }
}

My LinqToSql repository implementations then work with the DB class directly.

I am using StructureMap and wonder whether this is the best approach for this i.e. should I instead, take a connection string parameter as a constructor on my repository and set this argument within my StructureMap boot strapper?

Thanks, Ben

1

1 Answers

3
votes

Short answer to your question: yes, structuremap should handle the config of your db connection.

I use something like this to register my datacontext with structuremap

ForRequestedType<MyDataContext>()
    .CacheBy(StructureMap.Attributes.InstanceScope.Hybrid)
    .TheDefault.Is.ConstructedBy(
         () => 
         new MyDataContext(ConfigurationManager
                               .ConnectionStrings["MyConnectionString"]
                               .ConnectionString)
         );

That way, you won't need a wrapper for your datacontext either.