用于存储引用类型的C#引用集合 [英] C# reference collection for storing reference types

查看:136
本文介绍了用于存储引用类型的C#引用集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢实现一个集合(例如 List< T> ),它可以保存我在整个过程中创建的所有对象我的应用程序的生命周期,就像它的指针数组在C ++。想法是,当我的进程开始,我可以使用一个中央工厂创建所有对象,然后定期验证/无效他们的状态。基本上我想确保我的进程只处理有效的实例,我不重新获取我已经从数据库获取的信息。所以我的对象基本上都在一个地方 - 我的收藏。一个很酷的事情,我可以做到这一点是避免数据库调用从数据库获取数据,如果我已经得到它(即使我更新它后检索其仍然最新,如果当然一些其他进程没有更新它,但是不同的关注)。我不想再打电话新客户(詹姆斯·托马斯); 再次如果我开始詹姆斯·托马斯已经有一段时间过去。目前,我将最终得到相同的对象跨越appdomain的多个副本 - 一些不同步其他同步,即使我处理这使用MSSQL服务器上的 timestamp 字段我想在我的应用程序域只保留每个客户一个副本(如果可能的话会更好)。

I like to implement a collection (something like List<T>) which would hold all my objects that I have created in the entire life span of my application as if its an array of pointers in C++. The idea is that when my process starts I can use a central factory to create all objects and then periodically validate/invalidate their state. Basically I want to make sure that my process only deals with valid instances and I don't re-fetch information I already fetched from the database. So all my objects will basically be in one place - my collection. A cool thing I can do with this is avoid database calls to get data from the database if I already got it (even if I updated it after retrieval its still up-to-date if of course some other process didn't update it but that a different concern). I don't want to be calling new Customer("James Thomas"); again if I initted James Thomas already sometime in the past. Currently I will end up with multiple copies of the same object across the appdomain - some out of sync other in sync and even though I deal with this using timestamp field on the MSSQL server I'd like to keep only one copy per customer in my appdomain (if possible process would be better).

我不能使用List或ArrayList示例,因为我不能通过他们的真正的本地引用传递参数到他们现有的Add()方法,我使用 ref 创建它们,所以这不是好的我认为。那么如何实现/可以实现呢?所有方法使用 ref & out params是我现在想的,但它可能会很快丑陋。是否有其他方法来实现 RefList< T> .Add(ref T obj)

I can't use regular collections like List or ArrayList for example because I cannot pass parameters by their real local reference to the their existing Add() methods where I'm creating them using ref so that's not to good I think. So how can this be implemented/can it be implemented at all ? A 'linked list' type of class with all methods working with ref & out params is what I'm thinking now but it may get ugly pretty quickly. Is there another way to implement such collection like RefList<T>.Add(ref T obj)?

底线是:我不想重新创建一个对象,如果我已经在整个应用程序生命期间创建它,除非我决定重新创建它明确(也许它的过时或某事,所以我必须从db重新获取它)。有没有替代品?

So bottom line is: I don't want re-create an object if I've already created it before during the entire application life unless I decide to re-create it explicitly (maybe its out-of-date or something so I have to fetch it again from the db). Is there alternatives maybe ?

推荐答案

做最简单的方法是创建一个包装器到列表。这个包装器将有一个add方法,它接受一个ref。在添加它查找列表中的值,并创建它,当它找不到值。或者缓存

The easiest way to do what you're trying to accomplish is to create a wrapper that holds on to the list. This wrapper will have an add method which takes in a ref. In the add it looks up the value in the list and creates it when it can't find the value. Or a Cache

但是...这个语句会让我很担心。

But... this statement would make me worry.


我不想重新创建一个对象如果
我已经创建它之前
整个应用程序生命

I don't want re-create an object if I've already created it before during the entire application life

但正如Raymond Chen指出,具有错误策略的缓存是内存泄漏的另一个名称。您描述的是没有策略的缓存

But as Raymond Chen points out that A cache with a bad policy is another name for a memory leak. What you've described is a cache with no policy

要解决此问题,您应该考虑使用 4.0的System.Runtime.Caching 或对于3.5及更早版本的企业库缓存块< a>。如果这是一个Web应用程序,那么您可以使用 System.Web。缓存。或者如果你必须滚动自己至少得到一个合理的政策到位。

To fix this you should consider using for a non-web app either System.Runtime.Caching for 4.0 or for 3.5 and earlier the Enterprise Library Caching Block. If this is a Web App then you can use the System.Web.Caching. Or if you must roll your own at least get a sensible policy in place.

当然,这些都假设您的数据库缓存不足。

All of this of course assumes that your database's caching is insufficient.

这篇关于用于存储引用类型的C#引用集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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