实体框架和ObjectContext n层架构 [英] Entity Framework and ObjectContext n-tier architecture

查看:140
本文介绍了实体框架和ObjectContext n层架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



数据库(Sql Server) )实际上是通过Entity Framework来确定的,问题基本上是每个调用从用户界面开始,并且遍历所有层,但是这样做需要每次对每个操作创建一个新的ObjectContext,这使得性能非常糟糕,因为每次我需要重新加载元数据并重新编译查询。



最推荐的模式是下面的模式,它正是我在做的:创建和传递新的上下文通过业务层方法每次服务接收一个调用

  public BusinessObject GetQuery(){
using(MyObjectContext context = new MyObjectContext()){
//..do something}}

查询我没有看到任何特定的dealy,它工作正常对于复杂和沉重的查询,它会进行2秒的查询,以保持每15秒的呼叫。



我可以设置ObjectContext静态,它将解决性能问题但似乎不是任何人建议的,也是因为我不能同时访问上下文,因为不同的线程和多个调用引发异常。我可以让它线程安全,但是长期以来,相同的ObjectContext使得它越来越大(慢),因为引用它会导入每个查询执行查询。



我认为这是最常见的架构,那么最好和已知的实现和使用ObjectContext的方法是什么?



谢谢你,
Marco

解决方案

在Web上下文中,最好使用无状态方法并创建一个 ObjectContext 对于每个请求。



ObjectContext 构造的成本是最小的。元数据从全局缓存中加载,因此只有第一个调用必须加载它。



静态绝对不是一个好主意。 ObjectContext 不是线程保存,这将导致在具有多个调用的WCF服务中使用它时出现问题。使其线程保存将导致较少的性能,并且可以在多个请求中重复使用时导致微妙的错误。



检查此信息:如何为您的ObjectContext决定一生一次


I have a n-tier application based on pretty classic different layers: User Interface, Services (WCF), Business Logic and Data Access.

Database (Sql Server) is obviously quered throught Entity Framework, the problem is basically that every call starts from user interface and go throught all the layers, but doing that I need to create a new ObjectContext each time for every operation and that makes performance very bad because every time I need to reload metadata and recompile the query.

The most suggested pattern it would be the one below and it is what I'm actually doing: creating and passing the new context throught business layer methods each time the service receives a call

 public BusinessObject GetQuery(){     
   using (MyObjectContext context = new MyObjectContext()){ 
      //..do something  }    }

For easy query I don't see any particular dealy and it works fine but for complex and heavy query it makes a 2 seconds query to keep going for like 15 seconds each call.

I could set the ObjectContext static and it would solve the performance issue but it appears to be not suggested by anyone, also because I won't be able to access the context at the same time from different thread and multiple calls raise an exception. I could make it thread-safe but mantain the same ObjectContext for long time makes it bigger and bigger (and slower) because the reference it imports each query it execute a query.

The architecture I have I think it is the most common so what is the best and known way to implement and use ObjectContext?

Thank you, Marco

解决方案

In a Web context, it's best to use a stateless approach and create an ObjectContext for each request.

The cost of ObjectContext construction are minimal. The metadata is loaded from a global cache so only the first call will have to load it.

Static is definitely not a good idea. The ObjectContext is not thread save and this will lead to problems when using it in a WCF service with multiple calls. Making it thread save will result in less performance and it can cause subtle errors when reusing it in multiple requests.

Check this info: How to decide on a lifetime for your ObjectContext

这篇关于实体框架和ObjectContext n层架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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