CodeReview에서 내 코드를 검토 한 후 코드를 더 추상적으로 만드는 방법에 대한 질문에 갇혀 있습니다.
여기 에서 볼 수 있듯이 많은 사람들 이 리팩토링하기 쉽도록 ICommand
및 IConnection
인터페이스를 만들어야 한다고 제안합니다 .
내 예제에서는를 사용 IConnection
하지만 전체도 유효해야합니다 ICommand
.
나는 이와 같은 인터페이스를 만들어야한다고 생각했다.
public interface IConnection
{
//etc...
}
그런 다음에서 MySqlConnection
상속 할 수 있도록하려면 다음 과 같이 상속 할 IConnection
고유 MySqlConnection
한 항목 을 만들어야 합니다.
public class MySqlConnection : MySql.Data.MySqlClient.MySqlConnection, IConnection
{
//etc...
}
Which would mean the new MySqlConnection
would still have its methods and fields, and would inherit from IConnection
. I should then be able to write a Database
class like this;
public abstract class Database
{
protected IConnection con;
}
And extend it like this;
public class MySqlDatabase : Database
{
private override IConnection con = new MySqlConnection();
}
Now, my problem is, MySqlConnection
is sealed
; I can't extend it and therefore I do not know any option to make this database class abstract.
The question is; is there a proper way to implement the abstraction of Database
and if yes, how would I do it?
Please note that this question has nothing to do with the class being a singleton (as shown in my CodeReview post). That problem has been issued and is not relevant to the question.
해당 코드 리뷰에 대한 주석을 읽으면 실제로 사용 IDbConnection
하고 IDbCommand
자신을 롤링하는 대신 사용을 의미한다고 생각 합니다. 모든 ADO.NET 공급자는 이미이를 구현하고 있습니다.
당신이 원하는 경우 당신은 상속 할 수 DbConnection
및 IConnection
및 포장 을 MySqlConnection
. 모든 DbConnection
메서드 를 구현 하고 래핑 된 연결로 릴레이해야합니다.
public sealed class MyMySqlConnection : DbConnection, IConnection
{
public MyMySqlConnection(MySqlConnection underlyingConnection)
{
UnderlyingConnection = underlyingConnection;
}
public MySqlConnection UnderlyingConnection
{
get;
private set;
}
public override void Open()
{
UnderlyingConnection.Open();
}
// ...
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다