Why have you removed parameter with PolicyList from EnterpriseLibraryFactory's method BuildUp<T>

Topics: Enterprise Library Core
May 19, 2008 at 1:03 PM
I have such class compiled with EntList3:
    public class MyFactory
    {
        private static readonly object s_lock = new object();
        private readonly IConfigurationSource configurationSource;
        private readonly PolicyList m_policies = new PolicyList();
        private volatile MyConfigManager m_configMan;

        public XConfigurationManagerFactory()
            : this(ConfigurationSourceFactory.Create())
        {}

        public MyFactory(IConfigurationSource configurationSource)
        {
            this.configurationSource = configurationSource;
        }

        public MyConfigManager Create()
        {
            if (m_configMan == null)
                lock (s_lock)
                    if (m_configMan == null)
                        m_configMan = EnterpriseLibraryFactory.BuildUp<MyConfigManager>(configurationSource, Policies);

            return m_configMan;
        }

        public PolicyList Policies
        {
            get { return m_policies; }
        }
    }

I could initializer MyFactory's instance with desirable OB policies and build up MyConfigManager's instance.
Now, in EntLib4 you have removed EnterpriseLibraryFactory.BuildUp<T>'s parameter with PolicyList. Why? How can I now specify additional policies for OB wich will be used for building my objects?
May 19, 2008 at 1:59 PM

Hi,

I'm confused: there was no such override in 3.1. Here's what I get from Reflector for the BuildUp method overloads in EnterpriseLibraryFactory:

    public static T public static T IConfigurationSource configurationSource);
    public static T IReadWriteLocator locator);
    public static T string id);
    public static T IReadWriteLocator locator, IConfigurationSource configurationSource);
    public static T IReadWriteLocator locator, string id);
    public static T string id, IConfigurationSource configurationSource);
    public static T IReadWriteLocator locator, string id, IConfigurationSource configurationSource);

Were you using a modified version of the sources? In any case, this static class is really a utility class. You can use any properly configured builder to create the objecs for you.

Hope this helps,
Fernando


EvilShrike wrote:
I have such class compiled with EntList3:
    public class MyFactory
    {
        private static readonly object s_lock = new object();
        private readonly IConfigurationSource configurationSource;
        private readonly PolicyList m_policies = new PolicyList();
        private volatile MyConfigManager m_configMan;

        public XConfigurationManagerFactory()
            : this(ConfigurationSourceFactory.Create())
        {}

        public MyFactory(IConfigurationSource configurationSource)
        {
            this.configurationSource = configurationSource;
        }

        public MyConfigManager Create()
        {
            if (m_configMan == null)
                lock (s_lock)
                    if (m_configMan == null)
                        m_configMan = EnterpriseLibraryFactory.BuildUp<MyConfigManager>(configurationSource, Policies);

            return m_configMan;
        }

        public PolicyList Policies
        {
            get { return m_policies; }
        }
    }

I could initializer MyFactory's instance with desirable OB policies and build up MyConfigManager's instance.
Now, in EntLib4 you have removed EnterpriseLibraryFactory.BuildUp<T>'s parameter with PolicyList. Why? How can I now specify additional policies for OB wich will be used for building my objects?


May 19, 2008 at 2:37 PM
Hi.
It's my fault. Yes, I probably modified EnterpriseLibraryFactory in my project and forget :( . Sorry for any inconvenience.

Then it's just a suggestion :)

        public static T BuildUp<T>(IReadWriteLocator locator,
                                   ILifetimeContainer lifetimeContainer,
                                   string id,
                                   IConfigurationSource configurationSource,
                                    PolicyList policyList)
        {
            if (configurationSource == null)
                throw new ArgumentNullException("configurationSource");

            PolicyList resultPolicyList = GetPolicies(configurationSource);
            if (
policyList != null)
                resultPolicyList.AddPolicies(policyList);

                return GetObjectBuilder()
                    .BuildUp<T>(locator,
                                lifetimeContainer,
                                resultPolicyList,
                                strategyChain,
                                NamedTypeBuildKey.Make<T>(id),
                                null);
        }

I've written the code and then found out that OB2's PolicyList havn't got AddPolicies method now (OB1 did have) :(.
damn
May 19, 2008 at 3:36 PM
I think the best approach to deal with this is to send your policyList as a parameter to the GetPolicies() method, and in that method create a new policy list using the external policy list as a .ctor parameter.

Fernando
May 20, 2008 at 8:24 AM
Thank, Fernando, it works.
But honestly I'm a bit confuse of the absence of adding methods in PolicyList (like AddRange). If I have two PolicyList's instances there seems no way to merge them.