Problem with factory.CreateDefault()

Topics: Building and extending application blocks, Enterprise Library Core
May 5, 2007 at 12:16 PM
Edited May 5, 2007 at 12:34 PM
Hello everyone,

After I created a very simple application block using the application block software factory I proceeded to create test for it. One of my tests was of the form given below.

public void CreateMCApplicationSettingsBaseTest(){
          IMCApplicationSettingsBase actual;
          actual = MC.Configuration.MCApplicationSettingsBaseFactory.CreateMCApplicationSettingsBase();
.
.
.
}
 

This is causing trouble because when MC.Configuration.MCApplicationSettingsBaseFactory.CreateMCApplicationSettingsBase() is called. Below is the definition of the CreateMCApplicationSettingsBase() method:

public static IMCApplicationSettingsBase CreateMCApplicationSettingsBase(){
          try {
                    MCApplicationSettingsBaseInstanceFactory factory = new MCApplicationSettingsBaseInstanceFactory(ConfigurationSourceFactory.Create());
                    return factory.CreateDefault();
          } catch (ConfigurationErrorsException configurationException) {
                    TryLogConfigurationError(configurationException, "default");
                    throw;
          }
}

factory.CreateDefault() calls the EnterpriseLibraryFactory.BuildUp(configurationSource) function where T is one of my base classes (MC.Configuration.IMCApplicationSettingsBase) The BuildUp function keeps on delegating to other overloads of the BuildUp function with more and more arguments. Then in one of the BuildUp methods in the EnterpriseLibraryFactory class the following code is envoked:

return GetObjectBuilder().BuildUp(locator, null, null, GetPolicies(configurationSource));

Debugging into the function takes me to the ConfigurationNameMappingStrategy class’s BuildUp method.

public override object BuildUp(IBuilderContext context, Type t, object existing, string id)

The last parameter id’s value in this method happens to be null. The method tries to find its values from ConfigurationNameMapper of the ReflectionCache of the passed in context but does not get anything and sets id to an Empty string. It then calls return base.BuildUp(context, t, existing, id); which calls the BuildUp method of the ConfiguredObjectStrategy class which makes the following calls

string newId = id;
.
.
.
ICustomFactory factory = GetCustomFactory(t, reflectionCache);
 
if (factory != null){
          existing = factory.CreateObject(context, newId, configurationSource, reflectionCache);
}

The CreateObject method in turn calls the Create method

public TObject Create(IBuilderContext context, string name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)

This method checks for the name to be Null or Empty and since an empty string has been passed to the method it throws an exception saying “The value can not be null or string or empty.”

Can anyone tell me what am I missing here in this chain of events ?

Thanks everyone,
And I would like to add that learning Ent. Lib. is one of the most enjoyable experience I have ever had because of the strong community sense behind it. =)
Cheers !!!