locked
C# & unity DI : interface property not being able to access inside class function which extend interface RRS feed

  • Question

  • User264732274 posted

    my interface has one function and one property. the class which extend interface then we can access that property from a function declared with in same class. here i am using unity to dynamically call a function which return IDBConnection object.

    when i am compiling code then getting error like

    The name 'ConType' does not exist in the current context

    i am not being able to figure out where i made the mistake. so please some one see my full code and tell me where i am making the mistake and where and how to fix it.

    class code

    public interface IDBConnection
    {
        IDbConnection  CreateConnection();
        string ConType { get; set; }
    }
    
    public class BBAConnection : IDBConnection
    {
        string IDBConnection.ConType { get; set; }
    
        public  IDbConnection CreateConnection()
        {
            string _connectionString = "";
            IDbConnection connection=null;
    
            if (ConType == "local")
            {
                _connectionString = "put here local db connection";
                connection = new System.Data.SqlClient.SqlConnection(_connectionString);
            }
            else if (ConType == "remote")
            {
                _connectionString = "put here remote db connection";
                connection = new System.Data.SqlClient.SqlConnection(_connectionString);
            }
            else if (ConType == "OrcsWeb")
            {
                _connectionString = "put here website db connection";
                connection = new System.Data.SqlClient.SqlConnection(_connectionString);
            }
            else if (ConType == "Sage")
            {
                _connectionString = "put here sage connection";
                connection = new System.Data.SqlClient.SqlConnection(_connectionString);
            }
    
            return connection;
        }
    }
    
    public static class Factory
    {
        static IUnityContainer cont = null;
    
        public static IDBConnection initialize(string type)
        {
            IDBConnection oDbConnection = null;
    
            cont = new UnityContainer();
            cont.RegisterType<IDBConnection, BBAConnection>("local");
            cont.RegisterType<IDBConnection, BBAConnection>("remote");
            cont.RegisterType<IDBConnection, BBAConnection>("OrcsWeb");
            cont.RegisterType<IDBConnection, BBAConnection>("Sage");
    
            oDbConnection = cont.Resolve<IDBConnection>(type);
            oDbConnection.ConType = type;
    
            return oDbConnection;
        }
    }

    calling CreateConnection function like this way

    Factory.initialize("local").CreateConnection();

    Tuesday, June 28, 2016 12:48 PM

Answers

  • User264732274 posted

    Solved this issue

    using System.Data.SqlClient;
    using System.Data;
    using Microsoft.Practices.Unity;
    
    namespace ConnectionManager
    {
    
        public interface IBBAConnection
        {
            IDbConnection  GetConnection();
            string ConType { get; set; }
        }
    
        public class BBAConnection : IBBAConnection
        {
            public string ConType { get; set; }
    
            public IDbConnection GetConnection()
            {
                string _connectionString = "";
                IDbConnection connection = null;
    
                try
                {
                    // inside if else logic we fetch connection string from ini file or from any source and inistialize connection.
                    if (ConType == "local")
                    {
                        _connectionString = "put here local db connection";
                        connection = new System.Data.SqlClient.SqlConnection(_connectionString);
                    }
                    else if (ConType == "remote")
                    {
                        _connectionString = "put here remote db connection";
                        connection = new System.Data.SqlClient.SqlConnection(_connectionString);
                    }
                    else if (ConType == "OrcsWeb")
                    {
                        _connectionString = "put here website db connection";
                        connection = new System.Data.SqlClient.SqlConnection(_connectionString);
                    }
                    else if (ConType == "Sage")
                    {
                        _connectionString = "put here sage connection";
                        connection = new System.Data.SqlClient.SqlConnection(_connectionString);
                    }
    
                    connection.Open();
                }
                catch (Exception ex)
                {
                    string strErr = ex.Message;
                }
    
                return connection;
            }
        }
    
        public static class Factory
        {
            static IUnityContainer cont = null;
    
            public static IBBAConnection initialize(string type)
            {
                IBBAConnection oDbConnection = null;
    
                cont = new UnityContainer();
                cont.RegisterType<IBBAConnection, BBAConnection>("local");
                cont.RegisterType<IBBAConnection, BBAConnection>("remote");
                cont.RegisterType<IBBAConnection, BBAConnection>("OrcsWeb");
                cont.RegisterType<IBBAConnection, BBAConnection>("Sage");
    
                oDbConnection = cont.Resolve<IBBAConnection>(type);
                oDbConnection.ConType = type;
    
                return oDbConnection;
            }
        }
    
    }

    calling like this way

        private void button1_Click(object sender, EventArgs e)
            {
                if (Factory.initialize("OrcsWeb").GetConnection().State == System.Data.ConnectionState.Open)
                {
                    MessageBox.Show("Connection open");
                }
                else
                {
                    MessageBox.Show("Connection close");
                }
            }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 28, 2016 2:19 PM