我应该在 ColdFusion Model Glue 控制器中的哪里实例化我的模型?
假设我有一个客户,他有一个订单列表和一个愿望清单。在我的模型中,我有一个 ClientRepo
、OrderRepo
和 WishListRepo
。在控制器中,我应该在哪里实例化这些存储库?让它们成为类级实例是个好主意吗?
component ClientController
{
ClientRepo = new ClientRepo();
OrderRepo = new OrderRepo();
WishListRepo = new WishListRepo();
public void function HomePage(any event)
{
var clientId = event.getValue("id");
var client = ClientRepo.getClientById(clientId);
var orders = OrderRepo.getOrdersForClientId(clientId);
// put the variables into the event object for the view to access
}
}
或者更好的设计是在函数中实例化它们?
public void function HomePage(any event)
{
var ClientRepo = new ClientRepo();
var orderRepo = new OrderRepo();
var wishListRepo = new WishListRepo();
// rest of the code is the same
}
这里的假设是 ClientController 中的其他函数需要访问相同的存储库。
另外,控制器的使用寿命是多少?是每个请求一次、每个会话一次还是每个应用程序一次?
Let's say I have a client who has a list of orders and a wishlist. In my model, I have a ClientRepo
, OrderRepo
, and WishListRepo
. In the controller, where should I be instantiating these repositories? Is it a good idea to make them class-level instances?
component ClientController
{
ClientRepo = new ClientRepo();
OrderRepo = new OrderRepo();
WishListRepo = new WishListRepo();
public void function HomePage(any event)
{
var clientId = event.getValue("id");
var client = ClientRepo.getClientById(clientId);
var orders = OrderRepo.getOrdersForClientId(clientId);
// put the variables into the event object for the view to access
}
}
Or would a better design be to instantiate them within the function?
public void function HomePage(any event)
{
var ClientRepo = new ClientRepo();
var orderRepo = new OrderRepo();
var wishListRepo = new WishListRepo();
// rest of the code is the same
}
The assumption here is that other functions in ClientController
need access to the same repositories.
Also, what is the lifetime of the controller? Is it once-per-request, once-per-session, or once-per-application?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
披露:我是 Model-Glue 项目的贡献者,所以我可能知道我在说什么:-)
CFML 没有像 Java 那样的真正的类级实例。如果您想要单例(在应用程序的许多部分共享的单个实例),您需要将其放入共享范围(yuk!)或使用 bean 容器。幸运的是,Model-Glue 与 ColdSpring(CFML 的流行 bean 容器)紧密集成,并且 Model-Glue 3 使您可以比以往更轻松地在控制器中使用 ColdSpring bean。
首先,编辑 ColdSpring.xml 以包含单例的定义:
当然,这只是一个简单的示例。 ColdSpring是一个基于Java的Spring的强大的依赖注入框架。查看ColdSpring 快速入门,了解您可以用它做的其他一些事情。
一旦在 ColdSpring 中定义了 beans,您就可以让控制器通过 getModelGlue().getBean() 调用显式请求它们。然而,更好的方法是声明控制器对这些 bean 的依赖关系,并让 Model-Glue 将它们注入到您的控制器中。可以在 ModelGlue.xml 或控制器 CFC 中声明依赖关系。以下是如何在控制器中声明 bean 依赖关系:
任何声明的 bean 都会由框架注入到控制器的“beans”范围中,以便任何侦听器函数可以使用它们。但请注意,bean 注入发生在初始化之后,因此您不能将它们用作 init() 函数。
默认情况下,ColdSpring bean 是单例,因此如果将同一个 ColdSpring bean 注入到多个控制器中,它们最终都会得到相同的 bean 实例。如果将 singleton="false" 添加到 bean 定义中,那么每个控制器最终都会有一个不同的实例;我想不出你为什么要这么做。
有关 Model-Glue 中 Bean 注入的更多信息,请查看 Bean 注入 HOWTO在 Model-Glue Wiki 上。
Model-Glue 3.1 在框架初始化时将其所有控制器实例化为单例,因此每个控制器为每个应用程序创建一次。未来的版本可能会延迟控制器的实例化,直到第一次需要它们为止,因此最好不要对它们的初始化时间做出假设。如果您确实需要在应用程序初始化时执行控制器中的某些代码,我建议您向其添加一个 onApplicationStart 侦听器。
Disclosure: I'm a contributor to the Model-Glue project so I probably know what I'm talking about :-)
CFML doesn't have true class-level instances like Java. If you want singletons (a single instance shared across many parts of an application) you need to either put it in a shared scope (yuk!) or use a bean container. Fortunately Model-Glue has tight integration with ColdSpring (a popular bean container for CFML), and Model-Glue 3 makes it easier than ever to use ColdSpring beans in your controllers.
First, edit your ColdSpring.xml to include definitions for your singletons:
This is only a simple example of course. ColdSpring is a powerful dependency injection framework based on Java's Spring. Check out the ColdSpring Quickstart for some of the other things you can do with it.
Once the beans are defined in ColdSpring, you could have your controller explicitly ask for them via getModelGlue().getBean() calls. However a better way is to declare your controller's dependency on those beans and let Model-Glue inject them into your controller for you. The dependency can be declared either in ModelGlue.xml or in your controller CFC. Here's how to declare bean dependencies in your controller:
Any declared beans are injected into the "beans" scope of your controller by the framework so they're ready to use by any listener functions. Note however that bean injection occurs after initialization, so you cannot use them an init() function.
ColdSpring beans are singletons by default, so if the same ColdSpring bean is injected into multiple controllers they will all end up with the same instance of the bean. If you add singleton="false" to the bean definition then each controller will each end up with a different instance; I can't think of why you would want to do that though.
For more information on bean injection in Model-Glue, check out the Bean Injection HOWTO on the Model-Glue Wiki.
Model-Glue 3.1 instantiates all its controllers at framework initialization time as singletons, so each controller is created once-per-application. Future versions may delay the instantiating of controllers until they are first needed, so it's best to not make assumptions on when they are initialized. If you really need to have some code in a controller executed on application initialization time, I suggest you add an onApplicationStart listener to it.