MVC4 C#项目:在整个应用程序中提供一个对象的单个实例

MVC4 C# Project: Provide a single instance of an object across the application
2020-11-21
  •  译文(汉语)
  •  原文(英语)

背景 在提出我的实际问题之前,我将在此处提供背景:

我正在使用C#开发ASP.NET MVC 4项目.我当前正在处理的开发任务是在此Web应用程序中实现RazorMachine(https://github.com/jlamfers/RazorMachine),以便在应用程序层中利用Razor引擎的使用(相对于纯粹在MVC网络层).我已经在一系列测试治具中成功实现了RazorMachine,并且一切正常.现在,我需要在应用程序层的应用程序体系结构中实现它.

Jaap Lamfers在RazorMachine上的CodeProject文章(http://www.codeproject.com/Articles/423141/Razor-2-0-template-engine-support-layouts)中指出:"请注意,通常出于任何应用程序的性能原因您将创建RazorMachine的单例实例(由于内部实例绑定的JIT创建了类型缓存)".

运行中的代码的一个非常基本的示例如下:

RazorMachine rm = new RazorMachine();
ITemplate template = rm.ExecuteContent("Razor says: Hello @Model.FirstName @Model.LastName", 
  new {FirstName="John", LastName="Smith"});
Console.WriteLine(template.Result);

您可以在CodeProject网站上查看更多代码示例.

以此为背景,我的问题如下:

向我的MVC应用程序提供RazorMachine对象的单个实例的最佳方法是什么?

让我指出,我知道Singleton和Factory模式及其可能的实现.但是,使用Singleton似乎并不正确,因为我不是从头开始编写该类,该类已经存在.Factory似乎也不完全适合,但我想听听别人怎么说.

所有输入将不胜感激.

解决过程1

获取RazorMachine的单例实例的最快,最简单的方法是使用您选择的DI容器,知名的DI容器的示例包括Autofac,Ninject,Castle Windsor,Unity,StructureMap(有关主要.NET的性能比较,请参见此链接) Ioc / DI容器:http : //www.palmmedia.de/blog/2011/8/30/ioc-container-benchmark-performance-comparison)

这些容器使您摆脱了开发人员正确实施Singleton模式的责任.

使用Autofac,注册类的单例实例,您可以执行以下操作:

var builder = new ContainerBuilder();
builder.RegisterType<YourClassGoesHere>().SingleInstance();

参考:http : //autofac.readthedocs.org/en/latest/lifetime/instance-scope.html#single-instance

速聊1:
谢谢@halcharger.这是非常容易和快速实现的.我想时间会证明它是否运行良好.

Background I am going to provide the background here before posing my actual question:

I am working on an ASP.NET MVC 4 project in C#. The current development task I am busy with is to implement RazorMachine (https://github.com/jlamfers/RazorMachine) in this web application in order to leverage the use of the Razor engine in my application layer (as opposed to purely in the MVC web layer). I have successfully implemented RazorMachine in a series of Test Fixtures and all is working great. Now, I need to implement it into my application architecture inside my application layer.

In his CodeProject article on RazorMachine (http://www.codeproject.com/Articles/423141/Razor-2-0-template-engine-supporting-layouts), Jaap Lamfers states "Please note that normally for performance reasons at any application you would create a singleton instance of RazorMachine (because of the inner instance bound JIT created type caching)".

A very basic example of the code in action is as follows:

RazorMachine rm = new RazorMachine();
ITemplate template = rm.ExecuteContent("Razor says: Hello @Model.FirstName @Model.LastName", 
  new {FirstName="John", LastName="Smith"});
Console.WriteLine(template.Result);

You can view more code samples on the CodeProject site.

With this as background, my question is as follows:

What is the best way to provide a single instance of the RazorMachine object to my MVC application?

Let me state that I am aware of the Singleton and Factory patterns and their possible implementations. However, using Singleton doesn't seem to sit right as I am not writing the class from scratch, the class already exists. Factory also seems to not be wholly appropriate, but I would like to hear what others say.

All input will be greatly appreciated.

Solutions1

THe quickest, easiest way to get a singleton instance of RazorMachine is to use your DI container of choice, examples of well known DI containers are Autofac, Ninject, Castle Windsor, Unity, StructureMap (see this link for a performance comparision of major .NET Ioc/DI containers: http://www.palmmedia.de/blog/2011/8/30/ioc-container-benchmark-performance-comparison)

These containers abstract away from you the developer the responsibility to correctly implementing the Singleton pattern.

Using Autofac, to register a singleton instance of a class you would do the following:

var builder = new ContainerBuilder();
builder.RegisterType<YourClassGoesHere>().SingleInstance();

Ref:http://autofac.readthedocs.org/en/latest/lifetime/instance-scope.html#single-instance

Talk1:
Thanks @halcharger. This was very easy and quick to implement. I guess time will tell if it performs well.
转载于:https://stackoverflow.com/questions/28361556/mvc4-c-sharp-project-provide-a-single-instance-of-an-object-across-the-applicat

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入