Search code examples
.netormpetapoco

Circular base class


I am getting the following error, I have paste the code and connection string. I am using PetaPoco for the first time.

Circular base class dependency involving 'PetaPoco.DatabaseConfiguration' and 'PetaPoco.DatabaseConfiguration.IBuildConfigurationSettings'  

Following is the connection string:

 <add name="PriceCheckString" connectionString="Data Source=.;Initial     Catalog=PriceCheck;Integrated Security=SSPI;"
     providerName="System.Data.SqlClient"/>

Following is the line of code

      Database priceCheck = new Database("PriceCheckString");
      var ItemPrice = priceCheck.Query<ItemPrice>("SELECT * FROM itemPrice");

Solution

  • I had the same issue and was able to resolve. You have to move the interface declaration outside of the DatabaseConfiguration class.

    In PetaPoco.cs, replace your DatabaseConfiguration class declaration with the following code:

    public interface IBuildConfigurationSettings
    {
        /// <summary>
        ///     Sets the setting against the specified key.
        /// </summary>
        /// <param name="key">The setting's key.</param>
        /// <param name="value">The setting's value.</param>
        void SetSetting(string key, object value);
    
        /// <summary>
        ///     Tries to get the setting and calls the <paramref name="setSetting" /> to set the value if found.
        /// </summary>
        /// <typeparam name="T">The setting type.</typeparam>
        /// <param name="key">The setting's key.</param>
        /// <param name="setSetting">The set setting callback.</param>
        /// <param name="onFail">The on fail callback, called when no setting can be set.</param>
        void TryGetSetting<T>(string key, Action<T> setSetting, Action onFail = null);
    }
    
    /// <summary>
    ///     A helper class which enables fluent configuration.
    /// </summary>
    public class DatabaseConfiguration : IDatabaseBuildConfiguration, IBuildConfigurationSettings, IHideObjectMethods
    {
        private readonly IDictionary<string, object> _settings = new Dictionary<string, object>();
    
        /// <summary>
        ///     Private constructor to force usage of static build method.
        /// </summary>
        private DatabaseConfiguration()
        {
        }
    
        /// <summary>
        ///     Starts a new PetaPoco build configuration.
        /// </summary>
        /// <returns>An instance of <see cref="IDatabaseBuildConfiguration" /> to form a fluent interface.</returns>
        public static IDatabaseBuildConfiguration Build()
        {
            return new DatabaseConfiguration();
        }
    
        public void SetSetting(string key, object value)
        {
            // Note: no argument checking because, pref, enduser unlikely and handled by RT/FW
            if (value != null)
                _settings[key] = value;
            else
                _settings.Remove(key);
        }
    
        public void TryGetSetting<T>(string key, Action<T> setSetting, Action onFail = null)
        {
            // Note: no argument checking because, pref, enduser unlikely and handled by RT/FW
            object setting;
            if (_settings.TryGetValue(key, out setting))
                setSetting((T) setting);
            else if (onFail != null)
                onFail();
        }
    }
    

    You will also need to a find/replace on the following (in the same PetaPoco.cs file):

    find: ((DatabaseConfiguration.IBuildConfigurationSettings) source).SetSetting(key, value);

    replace with: ((IBuildConfigurationSettings) source).SetSetting(key, value);