I wan开发者_JAVA技巧t to inject a connection string into my repository but ideally, I want this connection string to be in my web.config connectionStrings section rather than in my windsor config. Is this possible?
I know I can use the fluent interface and achieve this easily but my bosses want an xml config file.
I also know that I can define a property and use that in my windsor config to pass the parameter in, but I have other code that reads the connectionstring from the web.config directly and do not really want two places with the same connectionString.
There are several ways to do this:
- This question has a solution that's very similar to what you want
You can tag the classes that need the connectionstring with an interface like this:
interface IRequiresConnectionString { string ConnectionString {get;set;} }then you subscribe to the event
container.Kernel.ComponentModelCreated(or write aIContributeComponentModelConstruction), inspect the component implementation type and if it implements IRequiresConnectionString you put the connectionString in the component's parameters.- You can also do the above without the interface if you follow the convention that the connectionstring parameter will always be called
connectionString(or whatever you like) but this is error-prone, if you accidentally name your parameterconectionStringit would not get injected.
加载中,请稍侯......
精彩评论