One based. NET micro-Letter SDK

Source: Internet
Author: User
Tags bool count json mail

First, the preface

A person who especially dislikes trouble, recently came across a micro-credit development. Downloaded some other people wrote the micro-letter development "framework", but was disgusting to realize that too bloated.

The last thing I like is to assemble the XML messages that are returned by the micro-letters into the entity class, so it will be bloated, and now all advocate lightweight, so there is a way to avoid the existence of a large number of entity classes.

Of course, there are more complex packaging, after reading the official API, and then look at the "framework", people feel confused, not clear enough to understand.

Second, I realize the idea

My micro-mail SDK (dare not claim to be a framework), the most important to achieve 2 goals:

1. Lightweight, is to abandon the entity class, as little as possible to declare entity, reduce the volume of the SDK;

2. Simple and straightforward, that is, the SDK class division and the official API is consistent, let a person read your intentions.

User Send please first post to the micro-trust server, then the micro-mail server was posted to my server, and this accepted message was XML, and I guess why XML, not more lightweight JSON, was for better compatibility, after all, XML is more generic (say it wrong, please point out). And when we volunteered to call some of the micro-mail APIs, it returned the JSON format, I'm going to die, tall. Did your vice President Zhangxiaolong know about this? Well, that's OK.

In fact, the operation of the micro-letter is very simple, there is no need to come up on the framework of anything, I believe that a qualified programmer can do it.

Our server only needs a GET, and a post can communicate with the micro-letter, from this point of view, the design is still more humane, praise one. The get is used to authenticate the micro-service, verify that the post is used to receive messages from the micro-server, and then assemble the response to return.

Third, on the code

Well, there's no more nonsense to say.

Because the micro-mail server post to us is the XML message, returns the JSON, therefore needs to turn each other. There are 3 types of format in this way, which is why a large number of framework definition entity classes cause the framework to be less lightweight.

To achieve the first goal, I mainly used the dynamic features of the. NET Framework4.0, and a DynamicXml.cs class that automatically converts XML strings to dynamic objects, and a JSON string that is automatically converted to dynamic The DynamicJson.cs class of object.

searching, finally let me find what I want.

1. The following is the DynamicXml.cs class, with the original author's copyright information in the file header.

* *--------------------------------------------------------------------------* https://www.captechconsulting.com
 /blog/kevin-hazzard/fluent-xml-parsing-using-cs-dynamic-type-part-1 * Blog Park netizens night Demon Friendship borrow this code for micro-letter development.
* http://www.cnblogs.com/deepleo/*--------------------------------------------------------------------------* *
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.dynamic;
Using System.Xml.Linq;
    
Using System.Collections;
    
    public class Dynamicxml:dynamicobject, IEnumerable {private readonly list<xelement> _elements;
        Public Dynamicxml (String text) {var doc = xdocument.parse (text); _elements = new List<xelement> {doc.
    Root};
    } protected Dynamicxml (XElement element) {_elements = new list<xelement> {element}; } protected Dynamicxml (ienumerable<xelement> elements) {_elements = new list<xelement> (E
    lements); } Public override bool Trygetmember (GetMemberBinder binder, out object result) {result = null; if (binder. Name = = "Value") result = _elements[0].
        Value; else if (binder. Name = = "Count" result = _elements.
        Count; else {var attr = _elements[0]. Attribute (Xname.get (binder).
            Name));
            if (attr!= null) result = attr; else {var items = _elements. Descendants (Xname.get) (binder.
                Name)); if (items = null | | items.
                Count () = 0) return false;
            result = new Dynamicxml (items);
    } return true; override bool Trygetindex (Getindexbinder binder, object[] indexes, out object result) {int
        NDX = (int) indexes[0];
        result = new Dynamicxml (_elements[ndx]);
    return true;
    Public IEnumerator GetEnumerator () {foreach (var element in _elements)        Yield return new dynamicxml (element); }
}

This code I did not look carefully, anyway, can be used, there has been no mistake.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.