弱引用与 Autofac 解析?
我有 POCO 对象,它们通过在存储库中查找 ID 来相互引用。我不希望对象之间具有强引用,因为存储库具有可能逐出对象的缓存策略。引用它们的其他对象应该通过存储库重新加载它们。我使用 AutoFac 作为我的 IoC 容器。
非常简单的例子 - 区域对象引用货币对象:
class Region
{
...
public Currency GetCurrency()
{
... Get the right Currency object
}
...
}
我一直在尝试两种方法来做到这一点。第一个是要求 AutoFac 每次解析货币存储库,以便我可以调用 Find(id)。
第二个是 WeakReference,我在其中检查 .IsAlive 以查看是否可以返回 .Target,或者是否需要解析存储库并调用 Find,并对我得到的内容进行 WeakReference。
我一直在研究 WeakReference 带来的小开销,但我不确定这与每次都要求 AutoFac 解决相比如何。
想法?
编辑: 花费存储库需要的任何时间/精力来回答 .find - 我更感兴趣的是哪个更昂贵,WeakReference 及其开销或 AutoFac 分辨率。
I have POCO objects that reference each other by finding ID's in repositories. I don't want the objects to have strong references between each other because the repositories have cache policy that might evict objects. Other objects that reference them should just reload them via the repository. I am using AutoFac as my IoC container.
Very simple example - a Region object references to a Currency object:
class Region
{
...
public Currency GetCurrency()
{
... Get the right Currency object
}
...
}
I have been experimenting with two ways to do this. The first is asking AutoFac to resolve the Currency repository every time so I can call Find(id).
The 2nd is WeakReference, where I check .IsAlive to see if I can just return .Target or if I need to resolve the repository and call Find, and make a WeakReference to what I got.
I have been studying up on the small overhead that WeakReference imposes but i am ont sure how that compares to asking AutoFac to resolve every time.
Thoughts?
Edit: Taking the whatever time/effort the repository needs to answer the .find - I'm more interested in which is more expensive, WeakReference with its overhead or AutoFac resolution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这很难回答,因为两者做的事情完全不同。开销也很大程度上取决于您如何使用 autofac 注册/解析组件。
例如:您是否计划注册为 InstancePerLifetimeScope(),并获取对 Lazy 的引用?
使用弱引用,您持有对对象的引用,但告诉运行时可以收集它 - 您将处理它。
使用 autofac,您要求容器构建和提供实例、注入依赖项以及(可能)管理生命周期范围和处置。
因此,就开销而言 - 这两个问题相交的小区域(获取某个实例)完全取决于您如何注册和解析组件,但我没有看到任何弱引用不会有任何代码路径(非常轻微)更少的开销。不过,WR 的运行时开销要高一些,因此很难直接进行比较。
This is hard to answer, because the two do entirely different things. The overhead is also quite dependent on how you will be registering / resolving the component with autofac.
For example: Are you planning on registering as InstancePerLifetimeScope(), and getting a referency to a Lazy?
With weakreference, you are holding a reference to an object, but telling the runtime that it's okay to collect it - you'll deal with that.
With autofac, you are asking a container to construct and provide instances, inject dependencies, as well as (possibly) managing lifetime scope and disposal.
So as far as overhead - the small area where these two concerns intersect ( get an instance of something) is entirely dependent on how you register and resolve the component, but I don't see any codepath where the WeakReference wouldn't have slightly (very slightly) less overhead. The WR has a bit more runtime overhead, though - so it's hard to compare directly.
访问数据库总是比处理弱引用的开销要大得多。对于这个特定的货币示例,只有少数对象,因此最好将它们全部保留在内存中,但对于较大的数据集,2 层方法(弱引用+存储库)似乎是合理的。
Going to the database is always going to be far more overhead than dealing with a weakreference. For this particular example of currencies, there are only a handful of objects so you'd be best served by keeping them all in memory but for larger datasets, the 2-tiered approach (weakreference+repository) seems reasonable.