简体   繁体   English

为Webservice客户端实现ActiveRecord

[英]Implement ActiveRecord for Webservice client

I'm writing a webservice client with C#/MVC 4 that communicates with a REST-Webservice, using JSON.net on the client side. 我正在使用C#/ MVC 4编写一个Web服务客户端,该客户端使用JSON.net与REST-Webservice通信。 Everything is working fine so far, but I want to improve the architecture to make the handling more fluid. 到目前为止,一切工作正常,但我想改进体系结构以使处理更加流畅。

I wrote a connector class and initialize it like this: 我写了一个连接器类,并像这样初始化它:

var conn = new MyConnector("admin", "admin", "http://localhost:9000");

Then I have a POCO class like this: 然后我有一个像这样的POCO课:

public class MyRecord
{
    [JsonProperty("record_id")]
    public string RecordId;
    ...

I'm saving changes by performing a PUT request to the Webservice. 我通过对Web服务执行PUT请求来保存更改。 It looks like: 看起来像:

var updated = conn.UpdateRecord("MyRecordId", new  NameValueCollection{{"title","new_title"}});

What I want to do is to implement it more like ActiveRecord: 我想要做的是像ActiveRecord一样实现它:

var myRecord = conn.GetRecord("myRecordId);
myRecord.title = "Foo";
myRecord.save();

That means that the MyRecord-class must be aware of the connection handler. 这意味着MyRecord-class必须知道连接处理程序。 That seems bad design to me, because MyRecord is basically a plain object. 这对我来说似乎是不好的设计,因为MyRecord本质上是一个普通对象。 Another choice would be to pass the connection handler to the save-method, but that smells, too. 另一个选择是将连接处理程序传递给保存方法,但这也很奇怪。

Any suggestions for improving it? 有任何改进建议吗?

If you want a "plain record" you should not implement the activerecord pattern. 如果要“普通记录”,则不应实现activerecord模式。 That is a contradiction. 那是一个矛盾。 It sounds to me like you are more interested in the repository pattern. 在我看来,您对存储库模式更感兴趣。

As references have a look at: Is Repository pattern as same as Active Record pattern? 作为参考,请看: 存储库模式与Active Record模式相同吗?

or for a broader perspective of your options: 或者更广泛地了解您的选择:

http://msdn.microsoft.com/en-us/magazine/dd569757.aspx http://msdn.microsoft.com/zh-CN/magazine/dd569757.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM