Monojit Sarkar
Monojit Sarkar

Reputation: 2451

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

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 BBAConnections
{
    IDbConnection  CreateConnection();
    string ConType { get; set; }
}

public class BBAConnection : BBAConnections
{
    string 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 BBAConnections initialize(string type)
    {
        BBAConnections oDbConnection = null;

        cont = new UnityContainer();
        cont.RegisterType<BBAConnections, BBAConnection>("local");
        cont.RegisterType<BBAConnections, BBAConnection>("remote");
        cont.RegisterType<BBAConnections, BBAConnection>("OrcsWeb");
        cont.RegisterType<BBAConnections, BBAConnection>("Sage");

        oDbConnection = cont.Resolve<BBAConnections>(type);
        oDbConnection.ConType = type;

        return oDbConnection;
    }
}

calling CreateConnection function like this way

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

Upvotes: 0

Views: 203

Answers (2)

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56697

Change the declaration within your implementing class to

public string ConType { get; set; } 

Upvotes: 1

Snak
Snak

Reputation: 693

First of all, C# is case sensitive, so you should change all IDbConnection to IDBConnection.

Now, your interface says that anything inheriting from it must have a CreateConnection method and a ConType property.

In your BBAConnection class you should make a public string property named ConType.

That should fix your code, it should look like this :

public interface IDBConnection
{
    IDBConnection CreateConnection();
    string ConType { get; set; }
}

public class BBAConnection : IDBConnection
{
    public string 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;
    }
}

Upvotes: 2

Related Questions