ASP.net MVC控制器 - 构造使用 [英] ASP.net MVC Controller - Constructor usage

查看:145
本文介绍了ASP.net MVC控制器 - 构造使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个ASP.net MVC应用程序,我有一个关于使用构造我的控制器的问题。

I'm working on an ASP.net MVC application and I have a question about using constructors for my controllers.

我使用实体框架和LINQ到实体所有我的数据交易。我需要访问我的实体模型,几乎我所有的控制器动作。当我第一次开始写程序,我在创建每个Action方法的开头实体对象,进行力所能及的工作,我需要再回到我的结果。

I'm using Entity Framework and linq to Entities for all of my data transactions. I need to access my Entity model for nearly all of my controller actions. When I first started writing the app I was creating an entity object at the beginning of each Action method, performing whatever work I needed to and then returning my result.

我意识到,我一遍又一遍地创建相同的对象,每个动作的方法,所以我创建了实体对象的私有成员变量,并开始在构造函数中每个控制器实例化。现在,每个方法只引用私有成员变量做的工作。

I realized that I was creating the same object over and over for each action method so I created a private member variable for the Entity object and started instantiating it in the constructor for each controller. Now each method only references that private member variable to do it's work.

我还在质问自己上哪条路是正确的。我不知道A.)哪种方法是最合适的? B.)在类的构造方法,多久是那些居住对象? C.)与构造方法有性能/完整性问题?

I'm still questioning myself on which way is right. I'm wondering A.) which method is most appropriate? B.) in the constructor method, how long are those objects living? C.) are there performance/integrity issues with the constructor method?

感谢您

推荐答案

您问正确的问题。

一个。这绝对是不恰当的创建这个依赖每个操作方法中。一个MVC的主要特点是分离关注的能力。通过加载了这些依赖你的控制器,你正在为厚厚的控制器。这些应注入控制器。有依赖注入(DI)的各种选项。通常这些类型的对象既可以注入构造或成一个属性。我的preference是构造器注入。

A. It is definitely not appropriate to create this dependencies inside each action method. One of the main features of MVC is the ability to separate concerns. By loading up your controller with these dependencies, you are making the controller for thick. These should be injected into the controller. There are various options for dependency injection (DI). Generally these types of objects can be either injected into the constructor or into a property. My preference is constructor injection.

乙。这些对象的生命周期将被垃圾收集器来确定。 GC不确定性。所以,如果你有一个有连接资源受限服务(数据库连接)的对象,那么你可能需要确保您关闭那些连接你的自我(而不是依赖于处置)。很多时候,一生的担忧被分离出来进入控制(IOC)容器的反转。有许多在那里。我的preference是Ninject。

B. The lifetime of these objects will be determined by the garbage collector. GC is not deterministic. So if you have objects that have connections to resource constrained services (database connections) then you may need to be sure you close those connections your self (instead of relying on dispose). Many times the 'lifetime' concerns are separated out into an inversion of control (IOC) container. There are many out there. My preference is Ninject.

℃。实例化的成本可能是最小的。该数据库事务的成本在哪里,你可能想要把你的注意力。有一个叫做工作单元的概念你可能想看看。从本质上讲,数据库可以处理的事务大于只是一个保存/更新操作。增加交易规模能带来更好的性能分贝

C. The instantiation costs are probably minimal. The database transactions cost are where you probably want to focus your attention. There is a concept called 'unit of work' you may want to look into. Essentially, a database can handle transactions larger than just one save/update operation. Increasing the transaction size can lead to better db performance.

希望得到您开始。

鲍勃

这篇关于ASP.net MVC控制器 - 构造使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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