4

I have a problem that I just can't solve on my own. I am new to programming and I would appreciate if you could help me with this issue:
I have a class I would like to inherit from:

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

and I would like to call CreateConnection() in another class to inject to methods to allow me to open connection and then execute scripts.
Edit 1 - class I would like to have it injected to.

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

If this question is way to silly to deserve answer would you just point in the direction where I should read about it?

I hope this question is well asked and clear. Thanks in advance.

2
  • 1
    You should search about creating instance of an object. Commented Jun 7, 2017 at 12:41
  • can you show how you are expecting to use it? What the consumer class would looks like. Commented Jun 7, 2017 at 12:42

4 Answers 4

2

Like this,

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

You also told that you want to inherit from this class, here is another approach,

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

Hope helps,

Sign up to request clarification or add additional context in comments.

2 Comments

Maybe use using conn = instance.CreateConnection();, so the Connection will be disposed automatically...
You are right but, this is just a sample of creating instance of an object :) @MartinVerjans
1

Hope this is what you are requesting for

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

1 Comment

Thank you guys so much, I really appreciate your effort. I got this solved! And I wasn't stomped for my question's syntax (I am so fab:P) ;)
1

This is how you do it. Apologies for a major blunder in the earlier answer. This one has the connection surrounded by a 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(); } } } 

1 Comment

Maybe use using conn = instance.CreateConnection();, so the Connection will be disposed automatically...
1

You can inject the connection creator into the consumer class through the constructor.

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

Note: The using statement automatically closes the connection.

Usage

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

If you want to inject the connection creator at each call of QueryExecution, you can inject it directly into the method as an additional parameter, instead.

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

Usage

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.