0

I have been programming in C# for a while but I was never good at design patterns or applying OOP, which I'm trying to change right now.

The problem I'm having is that I have all my SQL statements in a class called "SQLConn" but now I wan't to split it up into child classes instead.

In my application there is a database and two of these tables "Rules" and "Log".

My idea is to create a child class for each table (that I'm using in my application) and move all the methods to that class and open a new connection.

So, if I have a child class called SQLLog and SQLRules which are child classes of SQLConn is it a good idea that when the user inserts a new rule into the application it creates a new instance of SQLConn.SQLRules and runs "InsertRule()" and it also creates a new instance of SQLConn.SQLLog and runs "InsertLog("User inserted Rule X")?

According to this post Is it better to execute many sql commands with one connection, or reconnect everytime? connection pooling will be activated so there should be no big performance difference.

So, to summarize the question: Is it good to design the SQL classes like: * Parent class: SQLConn - Contains the conncetion information * Child class: SQLLog - Contains the "InsertLog()" method * Child class: SQLRules - Contains the "InsertRule()" method

And if I need both SQLRules and SQLLog in a part of my application should I create two instances, one of SQLLog and one of SQLRules, and call it's respectively methods? Is this a good design?

Best regards, Tomas

4

1 回答 1

1

如果您有两个单独的类具有自己的连接,并且您使用具有相同连接字符串的适当版本的 SQL Server,则连接应该池化。

就设计而言,将数据库和应用程序代码分开是一种很好的做法。也就是说,将数据库中的 SQL 代码作为 SQL 数据库中的存储过程(除非它是动态 SQL 或 LINQ-SQL 或类似的东西)。围绕使用存储过程存在一些争论,但我认为它适合您的情况。

然后,您可以拥有在 C# 代码中调用这些存储过程的方法,而实际上不必在代码中将 SQL 代码作为字符串。这通常更容易维护,因为您可以在 SSMS 中而不是 Visual Studio 中处理 SQL 代码。

如果做不到这一点,至少将您的 SQL 代码放在项目中自己的脚本文件中。

这听起来不是一个坏主意,根据得墨忒耳定律拆分功能是使项目更可测试和可维护的好方法。

于 2013-09-04T18:05:14.597 回答