如何在C#中将对象从一个类调用到另一个类

时间:2017-06-07 12:40:15

标签: c#

我有一个问题,我自己无法解决。我是编程新手,如果你能帮我解决这个问题,我将不胜感激: 我有一个我想继承的课程:

namespace rsDeployer.Common.SQLServerCommunication         
{
    public class RSDatabaseConnectionCreator: LoggerBase
    {
        public RSProfile profile;
        public RSDatabaseConnectionCreator(RSProfile profile)
        {
            this.profile = profile;
        }

        public SqlConnection CreateConnection(RSDatabaseNames DatabaseName, bool UseWindowsAuthentication, bool testConnection = false)
        {
            var connectionString = BuildRSDatabaseConnectionString(DatabaseName, UseWindowsAuthentication);            
            if (testConnection)
            {
                return IsConnectionAvailable(connectionString) ? new SqlConnection(connectionString) : null;
            }
            return new SqlConnection(connectionString);
        }
    }
}

我想在另一个类中调用CreateConnection()来注入允许我打开连接然后执行脚本的方法。
编辑1 - 我想把它注入。

        public void QueryExecution(string SQLQuery)
    {

        //here's where I would like to inject it
        SqlCommand command = new SqlCommand(SQLQuery, conn);
        var file = new StreamWriter(@"D:\Project\rsDeployer\ImportedQuery.txt");
        file.WriteLine(command);
        file.Close();
    }

如果这个问题是愚蠢的,值得回答,你只需指出我应该阅读的方向吗?

我希望这个问题得到充分肯定和明确。 提前谢谢。

4 个答案:

答案 0 :(得分:2)

像这样,

public void QueryExecution(string SQLQuery)
{ 
    RSProfile profile = new RSProfile();
    RSDatabaseConnectionCreator instance = new RSDatabaseConnectionCreator(profile);
    SqlConnection conn = instance.CreateConnection(...);
    SqlCommand command = new SqlCommand(SQLQuery, conn);
    var file = new StreamWriter(@"D:\Project\rsDeployer\ImportedQuery.txt");
    file.WriteLine(command);
    file.Close();
    conn.Close();
}

你还告诉你要继承这个类,这是另一种方法,

public class RSDatabaseConnectionCreator : LoggerBase
{
    public virtual object CreateConnection() // by virtual you can override it.
    {
      return new object();
    }
}

public class AnotherClass : RSDatabaseConnectionCreator {

    public AnotherClass() {
        CreateConnection(); // by inheriting RSDatabaseConnectionCreator , you can reach public functions.
    }

    public override object CreateConnection() // or you can override it 
    {
        // here might be some user Login check
        return base.CreateConnection(); // then you open connection
    }
}

希望有所帮助,

答案 1 :(得分:1)

这是你如何做到的。对于早期答案中的重大错误道歉。这个连接由using使用包围。

 namespace rsDeployer.Common.SQLServerCommunication         
    {
       public class ConsumerClass
       { 
           public void QueryExecution(string SQLQuery)
           {

           var profile = new RsProfile();
           var rsConnectionCreator = new RSDatabaseConnectionCreator(profile);
               using(var sqlConnection = rsConnectionCreator.CreateConnection(...Parameters here...)){
                      SqlCommand command = new SqlCommand(SQLQuery, sqlConnection );


                }
                var file = new StreamWriter(@"D:\Project\rsDeployer\ImportedQuery.txt");
                      file.WriteLine(command);
                      file.Close();
           }
       }
    }

答案 2 :(得分:1)

希望这是你要求的

public class ClassX
{
    private RSProfile _rsprofile;
    RSDatabaseConnectionCreator _dbConnectionCreator;
    private SqlConnection _sqlConnection;

        public ClassX()
        {
           _rsProfile = xxx; //  Get the RSProfile object
           _dbConnectionCreator = new RSDatabaseConnectionCreator (_rsProfile);
           RSDatabaseNames databaseName = yyy; //  get the RSDatabaseNames 
           var useWindowsAuthentication = true; 
           var testConnection = false;

          _sqlConnection = _dbConnectionCreator.CreateConnection(databaseName,useWindowsAuthentication ,testConnection );
        }

}

答案 3 :(得分:1)

您可以通过构造函数将连接创建者注入到使用者类中。

public class Consumer
{
    private RSDatabaseConnectionCreator _connectionCreator;

    // Constructor injection
    public Consumer (RSDatabaseConnectionCreator connectionCreator)
    {
        _connectionCreator = connectionCreator;
    }

    public void QueryExecution(string SQLQuery)
    {
        using (var conn = _connectionCreator.CreateConnection(dbName, true, true)) {
            if (conn != null) {
                ...
            }
        }
    }
}

注意:using语句会自动关闭连接。

用法

var connectionCreator = new RSDatabaseConnectionCreator(profile);
var consumer = new Consumer(connectionCreator);
consumer.QueryExecution(sqlQuery);

如果您想在每次调用QueryExecution时注入连接创建者,您可以将其作为附加参数直接注入方法中。

public void QueryExecution(string SQLQuery, RSDatabaseConnectionCreator connectionCreator)
{
    using (var conn = connectionCreator.CreateConnection(dbName, true, true)) {
        if (conn != null) {
            ...
        }
    }
}

用法

var connectionCreator = new RSDatabaseConnectionCreator(profile);
var consumer = new Consumer();
consumer.QueryExecution(sqlQuery, connectionCreator);