在业务服务中打开sql连接

时间:2022-06-03 11:58:44

Do you see it as tight coupling that my business service class opens a SqlConnection ?

你认为它是我的业务服务类打开SqlConnection的紧密耦合吗?

Actually a business service should not be aware of the concrete dataprovider?!

实际上商业服务不应该知道具体的数据提供者?!

public class UnitService:

公共类UnitService:

 public void DeleteUnit(Unit unit)
            {
                using (SqlConnection con = new SqlConnection());
                using (TransactionScope trans = new TransactionScope())
                {
                    con.Open();

                    _unitDataProvider.Delete(unit,con);
                    _employeeDataProvider.UpdateEmployees(con);

                    trans.Complete();
                }             
            }

2 个解决方案

#1


3  

Your qustion is very subject to opinion...

你的qustion很容易受到意见......

I love to abstract code and uncouple everywhere possible. The question, as always, is that of time and requirements.

我喜欢抽象代码并在任何地方解开。一如既往的问题是时间和要求。

For a small simple project that does not require extensive Unit Testing within the Business Tier, your coupling although does not necessarily follow best practice may be exactly what the customer/end-user requires, and may allow you to provide the software in a more timely fashion.

对于不需要在业务层中进行大量单元测试的小型简单项目,您的耦合虽然不一定遵循最佳实践,但可能正是客户/最终用户所需要的,并且可能允许您更及时地提供软件时尚。

For a larger/more complex/etc project it may be best to abstract the persistance layer.

对于更大/更复杂的/ etc项目,最好抽象持久层。

It is simply not feasible to follow best practices, best design patterns, and best coding principles for every line of code you write. I've found the authors of such books quite often mention the patterns as potentially surplus to requirements and should simply be used as a tool, when needed.

对于您编写的每行代码,遵循最佳实践,最佳设计模式和最佳编码原则是不可行的。我发现这些书籍的作者经常提到这些模式可能会超出要求,并且只需在需要时用作工具。

Hope that helps?

希望有帮助吗?

#2


0  

Do you see it as tight coupling that my business service class opens a SqlConnection ?

你认为它是我的业务服务类打开SqlConnection的紧密耦合吗?

Yes. If you have some calculation to do that job you can do in Business layer before reaching to presentation layer.

是。如果您有一些计算来完成这项工作,您可以在到达表示层之前在业务层中执行此操作。

One more thing I would like to suggest is to use "Using" statements for the IDisposable Objects in case of SQLConnection class

我想建议的另一件事是在SQLConnection类的情况下对IDisposable对象使用“Using”语句

I meant It should be like below.

我的意思应该是如下。

using (System.Data.SqlClient.SqlConnection con = new SqlConnection("YourConnection string")) {
    con.Open();
    SqlCommand cmd = new SqlCommand();
    string expression = "Parameter value";
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "Your Stored Procedure";
    cmd.Parameters.Add("Your Parameter Name", SqlDbType.VarChar).Value = expression;
    cmd.Connection = con;
    using (IDataReader dr = cmd.ExecuteReader()) {
        if (dr.Read()) {
        }
    }
}

#1


3  

Your qustion is very subject to opinion...

你的qustion很容易受到意见......

I love to abstract code and uncouple everywhere possible. The question, as always, is that of time and requirements.

我喜欢抽象代码并在任何地方解开。一如既往的问题是时间和要求。

For a small simple project that does not require extensive Unit Testing within the Business Tier, your coupling although does not necessarily follow best practice may be exactly what the customer/end-user requires, and may allow you to provide the software in a more timely fashion.

对于不需要在业务层中进行大量单元测试的小型简单项目,您的耦合虽然不一定遵循最佳实践,但可能正是客户/最终用户所需要的,并且可能允许您更及时地提供软件时尚。

For a larger/more complex/etc project it may be best to abstract the persistance layer.

对于更大/更复杂的/ etc项目,最好抽象持久层。

It is simply not feasible to follow best practices, best design patterns, and best coding principles for every line of code you write. I've found the authors of such books quite often mention the patterns as potentially surplus to requirements and should simply be used as a tool, when needed.

对于您编写的每行代码,遵循最佳实践,最佳设计模式和最佳编码原则是不可行的。我发现这些书籍的作者经常提到这些模式可能会超出要求,并且只需在需要时用作工具。

Hope that helps?

希望有帮助吗?

#2


0  

Do you see it as tight coupling that my business service class opens a SqlConnection ?

你认为它是我的业务服务类打开SqlConnection的紧密耦合吗?

Yes. If you have some calculation to do that job you can do in Business layer before reaching to presentation layer.

是。如果您有一些计算来完成这项工作,您可以在到达表示层之前在业务层中执行此操作。

One more thing I would like to suggest is to use "Using" statements for the IDisposable Objects in case of SQLConnection class

我想建议的另一件事是在SQLConnection类的情况下对IDisposable对象使用“Using”语句

I meant It should be like below.

我的意思应该是如下。

using (System.Data.SqlClient.SqlConnection con = new SqlConnection("YourConnection string")) {
    con.Open();
    SqlCommand cmd = new SqlCommand();
    string expression = "Parameter value";
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "Your Stored Procedure";
    cmd.Parameters.Add("Your Parameter Name", SqlDbType.VarChar).Value = expression;
    cmd.Connection = con;
    using (IDataReader dr = cmd.ExecuteReader()) {
        if (dr.Read()) {
        }
    }
}