Would nice to have a "default" named instance

Apr 5, 2008 at 9:33 AM
Hello,

Recently I've run into a situation when I need to have a kind of DEFAULT (or maybe CURRENT) named instance of the registered type.
Simply like this: we have two named registered instances of Database type - SqlDatabase (named "SQL") and OracleDatabase (named "Oracle"). To inject any of them I should specify a name I want to get - say Resolve<Database>( "SQL" ) but I need to have a default instance which is SqlDatabase, for instance. So if I ask a container like Resolve<Database>() it gets me SqlDatabase. And it would nice to have an ability to switch the default instance between "SQL" and "Oracle".

I've got an idea that it can be handled using nested containers chain or just manually re-assign instances, but I suppose it would be really nice to build in such an ability into container because it's a kind of the real world situation.

What do you think?

-
Thanks, Leonid
Apr 5, 2008 at 3:51 PM
Edited Apr 5, 2008 at 3:51 PM
You could use configured unity instances to manage the registered instance names.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<configSections>
		<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
	</configSections>
	<unity>
		<containers>
			<container>
				<types>
					<type name="SqlDatabase" type="CodePlex.Database" mapTo="CodePlex.SqlDatabase" />
					<type name="OracleDatabase" type="CodePlex.Database" mapTo="CodePlex.OracleDatabase" />
				</types>
				<instances>
					<add name="DefaultDatabase" type="System.String" value="SqlDatabase" />
					<add name="SqlDatabaseName" type="System.String" value="SqlDatabase" />
					<add name="OracleDatabaseName" type="System.String" value="OracleDatabase" />
				</instances>
				<extensions>
				</extensions>
				<extensionConfig>
				</extensionConfig>
			</container>
		</containers>
	</unity>
</configuration>
            string databaseName = container.Resolve<string>("DefaultDatabase");
            CodePlex.Database db = container.Resolve<CodePlex.Database>(databaseName);


Recently I've run into a situation when I need to have a kind of DEFAULT (or maybe CURRENT) named instance of the registered type.
Simply like this: we have two named registered instances of Database type - SqlDatabase (named "SQL") and OracleDatabase (named "Oracle"). To inject any of them I should specify a name I want to get - say Resolve<Database>( "SQL" ) but I need to have a default instance which is SqlDatabase, for instance. So if I ask a container like Resolve<Database>() it gets me SqlDatabase. And it would nice to have an ability to switch the default instance between "SQL" and "Oracle".

I've got an idea that it can be handled using nested containers chain or just manually re-assign instances, but I suppose it would be really nice to build in such an ability into container because it's a kind of the real world situation.
Leonid

Apr 5, 2008 at 7:41 PM
I've got an idea, thanks! I'm going to try this way.