ASP.NET中的类库和静态变量

发布于 2024-09-11 20:29:37 字数 352 浏览 8 评论 0原文

我有应该封装 orm 逻辑的类库。为了避免一些数据库调用,它应该包含某种缓存或静态变量(我想避免它们)。它用于 asp.net 和 wcf 应用程序。由于它是类库,我不想访问缓存或其他与 ASP.NET 相关的东西。我还想避免静态变量,因为它们的应用程序范围性质。

我应该如何实施?为了实现这个目标你会做什么?

编辑:

为了简化:想象一下封装 DAL 的类库。它与数据库对话。里面有一些昂贵的查询。其中一些应该为每个用户获取一次并存储在某个地方,其中一些可以为每个应用程序使用(也存储以避免将来调用数据库)。问题是,通常我会使用 Cache,但由于它是 DAL 类库,我想在其中包含此功能(而不是在 asp.net 中)。希望现在更清楚了;)

I have class library which should encapsulates orm logic. To avoid some db calls, it should contain some kind of cache or static variables (I want to avoid them). It's used in asp.net and wcf applications. Since it's class library, I don't want to access Cache or other asp.net related stuff. I also want to avoid static vars because of the application scope nature of them.

How should I implement that? What do you do to achieve this?

EDIT:

To simplify: imagine class library encapsulating DAL. It talks to database. There are some costly queries inside. Some of them should be fetched once per user and stored somewhere and some of them could be used per Application (also stored to avoid future calls to DB). The thing is that normally I would use Cache, but since it's DAL class library, I want to include this functionality inside it (not in asp.net). Hope it's more clear now ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不在是我 2024-09-18 20:29:37

您应该使用缓存:

使用模式、接口等:

你的类库应该是松散耦合。 (易于交换且无交叉引用)

示例(我如何简化):

namespace MyDbContext
{
    var cache;
    var db;

    public MyDbContext()
    {
        // Cache init
        cache = ....;

        // DB init (can be factory or singleton)
        db = DataBase.Instance();
    }

    public class Car
    {
        // Db tuple id
        public CarId { get; set; }

        public Car(int id)
        {
             CarId = id;
        }

        public Car GetFromDb()
        {
            // your db code will be here
            Car myCar = ....;

            // cache your object
            cache.Put("Car" + CarId.ToString(), myCar);
            return myCar;
        }

        public Car Get()
        {
            // try to get it from cache or load from db
            Car mycar = cache.Get("Car" + CarId.ToString()) as Car ?? GetFromDb();
        }

        public ClearCache()
        {
            cache.Put("Car" + CarId.ToString(), null);
            // maybe cache.Remove("Car" + CarId.ToString())
        }
    }
}

You should use caching:

Use Patterns, Interfaces etc:

Your class library should be loosely coupled. (Easy to exchange and no cross references)

Example (how I'm doing it simplified):

namespace MyDbContext
{
    var cache;
    var db;

    public MyDbContext()
    {
        // Cache init
        cache = ....;

        // DB init (can be factory or singleton)
        db = DataBase.Instance();
    }

    public class Car
    {
        // Db tuple id
        public CarId { get; set; }

        public Car(int id)
        {
             CarId = id;
        }

        public Car GetFromDb()
        {
            // your db code will be here
            Car myCar = ....;

            // cache your object
            cache.Put("Car" + CarId.ToString(), myCar);
            return myCar;
        }

        public Car Get()
        {
            // try to get it from cache or load from db
            Car mycar = cache.Get("Car" + CarId.ToString()) as Car ?? GetFromDb();
        }

        public ClearCache()
        {
            cache.Put("Car" + CarId.ToString(), null);
            // maybe cache.Remove("Car" + CarId.ToString())
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文