开发者

Static DataService class vs. IRepository<T>?

I am just studying the code of Sacha Barbers MVVM framework Chinch and I saw this in the xxxViewModel.cs file:

DataService.FetchAllOrders(CurrentCustomer.CustomerId.DataValue);

DataService is a Static class. Being a junior dev I am only used to Interfaces with Data services. Why is that class static?

Or do you think he made it just for the example?

So is that a good a开发者_开发知识库pproach?


In fairness, I don't know what's going on in FetchAllOrders - it might be programmed to behave well.

In practical experience, I've seen static classes used poorly to maintain the infrastructure needed to do data access. I say "poorly" because these implementations (that I've seen) were not made thread-safe. When the code was deployed to a multi-user environment (such as a web application), it exploded.

  • Use static classes for classes that contain no state (and are threadsafe as a result). Classes with just methods, for example.
  • Use static classes for classes where it is intended to make access serial with locks (threadsafe).
  • Use static classes in throw-away code to avoid the design overhead of constructing/maintaining/passing instances.

Look into the .net framework and see which classes Microsoft made static and meditate on why.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜