asp.net MVC-如何通过不同的存储库类共享SqlConnection的相同实例 [英] asp.net MVC - How can I share the same instance of a SqlConnection through different repository classes

查看:70
本文介绍了asp.net MVC-如何通过不同的存储库类共享SqlConnection的相同实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVC5和普通的ADO.NET创建一个新项目(仅作为学习练习),我需要创建一个存储库,该存储库注册了具有多个相关对象的模型,这些对象也需要同时创建,这些对象可能需要插入其他对象.

I'm creating a new project using MVC5 and plain ADO.NET (just as a learning exercise) and I need to create a repository that registers a model with several related objects that also need to be created at the same time and those objects in turn may need to insert other objects.

我能想到的最简单的解决方案是拥有一个大型方法(在存储库中),该方法可以接收父对象的实例(该实例包含所有需要插入的相关对象),并且还具有一个存储过程,可以接收所有相关数据作为表值参数,然后使用单个事务插入所有内容.

The easiest solution I can think of is to have a massive method(in the repository) that receives an instance of the parent object(which contains all the related objects that needs to insert) and also have a single stored procedure that receives all the related data as table-valued parameters and then insert everything using a single transaction.

尽管这似乎是最直接的方法,但我不是它的忠实拥护者,所以我想知道的是,是否有任何方法/通用实践可以用来共享为该对象创建的SqlConnection的相同实例.父对象和其他相关对象?

Though that might seem the most straightforward approach, I'm not a big fan of it, so what I want to know is if there's any way/common practice that I can use to share the same instance of a SqlConnection created for the parent object with the other related objects?

我当时想也许在相关对象的构造函数中传递SqlConnection对象,这样每个存储库只需要处理插入单个对象的逻辑,但是我不确定.

I was thinking that perhaps passing the SqlConnection object in the constructor of the related objects, that way each repository would only have to deal with the logic to insert a single object, but I'm not sure.

这是父对象(模型)的存储库,我认为应该是实例化SqlConnection并启动事务

This is the repository for the parent object (Model), the one that I think, should instantiate the SqlConnection and start the transaction

 public class ModelRepository : IModelRepository
    {
        public int Add(Model entity)
        {
            using (var conn = new SqlConnection(ConnectionString))
            {   
                conn.Open();
                using (var command = conn.CreateCommand())
                {                    
                    command.Transaction = conn.BeginTransaction(IsolationLevel.ReadCommitted);
                    command.CommandText = "up_Model_Insert";
                    command.CommandType = CommandType.StoredProcedure;                 
                    

                    command.Parameters.Add(command.CreateParameter("@pName ", entity.Name));
                    command.Parameters.Add(command.CreateParameter("@pDescription", entity.Description));
                    //Other parameters...

                    //Call the repositories of the other objects

                    //....

                    //how can I make the other repositories use the same connection and 
                    //transaction as the Model entity?


                    

                    
                    return Convert.ToInt32(command.ExecuteScalar());
                }
            }
        }
    }

问题是我不知道如何使其他存储库使用与模型"相同的连接和事务.实体.

The problem is that I don't know how to make the others repositories use the same connection and transaction as the "Model" entity.

我已经阅读了Ninject的概念,但我不确定哪种情况合适.

I've read the Ninject has this "object scope" concept , but I'm not sure what would be the right one in my case.

推荐答案

我不确定为什么要共享"连接.回到基础,您应该始终将连接封装在using块中(假设您使用EF).

I'm not sure why you want to 'share' a connection. Back to the basics, you should always encapsulate a connection in a using block (assume you use EF).

您追求的是交易,对吗?我现在在这里只是想着我的头: 您可以做的是将插入逻辑包装在TransactionInfo类中.这些TransactionInfo类可以将Action和存储库作为参数.该操作最终执行通过给定存储库插入单个对象的逻辑(该存储库应遵循为您提供CRUD逻辑的某个接口).

What you are after are transactions, right? What I'm just thinking of my head here right now: What you could do is wrap your insert logic in TransactionInfo classes. These TransactionInfo classes could take an Action and a repository as parameters. The action eventually does the logic of inserting a single object via the given repository (the repository should follow some interface that provides you with CRUD logic).

当您有多个TransactionInfo时,可以将它们一起发送到可以开始

When you have multiple TransactionInfo's, you could send them together somewhere that would do the logic of beginning an database transaction, execute all given Actions from every TransactionInfo object and eventually execute the transaction.

TransactionInfo可以具有类似以下的构造函数:

TransactionInfo could have a constructor like:

public TransactionInfo(Action action, ICrudRepository repository)

我希望我对你的理解很好.共享SqlConnection似乎对我来说是错误的.

I hope I've understood you well. Sharing an SqlConnection seems just wrong to me.

这篇关于asp.net MVC-如何通过不同的存储库类共享SqlConnection的相同实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆