2017-06-07 17 views
4

Objekt von einer Klasse in eine andere Ich habe ein Problem, das ich allein nicht lösen kann. Ich bin neu in der Programmierung und ich würde schätzen, wenn Sie mir mit diesem Problem helfen könnten:
ich eine Klasse, die ich von erben möchte:So rufen Sie in 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); 
     } 
    } 
} 

und ich möchte Create() in einer anderen Klasse nennen Methoden zu injizieren, damit ich die Verbindung öffnen und dann Skripte ausführen kann.
Edit 1 - Klasse Ich möchte es injiziert haben.

 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(); 
    } 

Wenn diese Frage zu dumm ist, um Antwort zu verdienen, würden Sie nur in die Richtung zeigen, wo ich darüber lesen sollte?

Ich hoffe, dass diese Frage gut gestellt und klar ist. Vielen Dank im Voraus.

+1

Sie über das Erstellen Instanz eines Objekts suchen soll. – Berkay

+0

Kannst du zeigen, wie du es erwartest? Wie die Verbraucherklasse aussehen würde. – Juan

Antwort

2

Wie diese

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(); 
} 

Sie sagten auch, dass Sie von dieser Klasse erben wollen, hier ein anderer Ansatz ist,

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 
    } 
} 

Hoffnung hilft,

+0

Vielleicht verwenden 'conn = instance.CreateConnection();', so wird die Verbindung automatisch entsorgt werden ... –

+0

Sie haben Recht, aber das ist nur ein Beispiel zum Erstellen von Instanz eines Objekts :) @MartinVerjans – Berkay

1

Dies ist, wie Sie tun, es. Entschuldigung für einen großen Fehler in der früheren Antwort. Dieser hat die Verbindung umgeben von einer Verwendung.

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(); 
      } 
     } 
    } 
+0

Vielleicht verwenden Sie ' mit conn = instance.CreateConnection(); ', so wird die Verbindung automatisch entsorgt ... –

1

Hoffnung das ist, was Sie für

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); 
     } 

} 
+0

Vielen Dank, ich schätze Ihre Bemühungen sehr. Ich habe das gelöst! Und ich wurde nicht für die Syntax meiner Frage gestampft (ich bin so fabelhaft: P);) –

1

anfordern Sie können die Verbindung Schöpfer in den Consumer-Klasse durch den Konstruktor injizieren.

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) { 
       ... 
      } 
     } 
    } 
} 

Hinweis: Die using-Anweisung schließt die Verbindung automatisch.

Nutzungs

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

Wenn Sie die Verbindung Schöpfer bei jedem Aufruf von QueryExecution injizieren wollen, können Sie es direkt in das Verfahren als zusätzlicher Parameter injizieren, statt.

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

Nutzungs

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