Is it good to use a static EF object context in an MVC application for better perf?

后端 未结 3 1028
刺人心
刺人心 2021-01-22 10:48

Let\'s start with this basic scenario:

I have a bunch of Tables that are essentially rarely changed Enums (e.g. GeoLocations, Category, etc.) I want to load these into

3条回答
  •  遥遥无期
    2021-01-22 11:10

    Personally, I never have any static Context stuff, etc. For me, when i call the database (CRUD) I use that context for that single transaction/unit of work.

    So in this case, what you're suggesting is that you wish to retrieve some data from the databse .. and this data is .. more or less .. read only and doesn't change / static.

    Lookup data is a great example of this.

    So your Categories never change. Your GeoLocations never change, also.

    I would not worry about this concept on the database/persistence level, but on the application level. So, just forget that this data is static/readonly etc.. and just get it. Then, when you're in your application (ie. ASP.NET web MVC controller method or in the global.asax code) THEN you should cache this ... on the UI layer.

    If you're doing a nice n-tiered MVC app, which contains

    • UI layer
    • Services / Business Logic Layer
    • Persistence / Database data layer

    Then I would cache this in the Middle Tier .. which is called by the UI Layer (ie. the MVC Controller Action .. eg. public void Index())

    I think it's important to know how to seperate your concerns .. and the database stuff is should just be that -> CRUD'ish stuff and some unique stored procs when required. Don't worry about caching data, etc. Keep this layer as light as possible and as simple as possible.

    Then, your middle Tier (if it exists) or your top tier should worry about what to do with this data -> in this case, cache it because it's very static.

提交回复
热议问题