类库和asp.net中的静态变量

时间:2021-04-25 03:37:58

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.

我有类库,它应该封装orm逻辑。为了避免某些db调用,它应该包含某种缓存或静态变量(我希望避免它们)。它在asp.net和wcf应用程序中使用。因为它是类库,所以我不想访问缓存或其他asp.net相关的东西。我还想避免静态vars,因为它们的应用范围性质。

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 ;)

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

1 个解决方案

#1


2  

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())
        }
    }
}

#1


2  

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())
        }
    }
}