Google API for .NET architecture (Part 2)

Part 1: Google API for .NET architecture (Part 1) 

Today is Harvey Nash - 10 years in Viet Nam celebration day. I very happy about that, and I also drunk some beer, so I have been eager to write this post. In last post, I had been analyze about Domain Model of Gapi4net, some of people maybe like it. That is a reason I write this post in next. And what will I write in this post? As you knew in last post, I will be analyze about some technical I used in Gapi4net. Three of techniques that I will present is download data, build the Url and make Fluent Interface for Gapi4net.

+ Download data: I use a technical that post from this link for implemented the asynchronous request to Google services. I want to execute it in asynchronous because it will save a little bit time when request to Google services, and I will continue to process something else until the request to Google services are completed. I thought I will not explain it clearly, because everyone also knew why we use the asynchronous request. Next step, I only need to code the Downloader, this code will be show as below:

    public interface IDownloader : IDisposable
    {
        void Add(string url);

        int Count { get; }

        IEnumerable<IAsync> DownloadAll(Action<Async<string>> action);
    }
    internal class Downloader : IDownloader
    {
        private readonly IList<string> _urls;

        public Downloader()
        {
            _urls = new List<string>();
        }

        public void Add(string url)
        {
            Contract.Assert(!string.IsNullOrEmpty(url), "Url is null or empty");

            _urls.Add(url);
        }

        public IEnumerable<IAsync> DownloadAll(Action<Async<string>> action)
        {
            return _urls.Select(url => Async.Parallel(
                ProcessResultFromDownloadContentFromUrl(url, action)
                                           ));
        }

        private IEnumerable<IAsync> ProcessResultFromDownloadContentFromUrl(string url, Action<Async<string>> action)
        {
            var req = WebRequest.Create(url);

            // asynchronously get the response from http server
            var response = req.GetResponseAsync();
            yield return response;

            var resp = response.Result.GetResponseStream();

            // download HTML using the asynchronous extension method
            // instead of using synchronous StreamReader
            var html = resp.ReadToEndAsync().ExecuteAsync<string>();

            yield return html;

            action(html);
        }

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }

        public int Count
        {
            get { return _urls.Count; }
        }
    }

 + Build the Url: Because my request is a Url, so I must to build the Url to request to Google services. I will convert from the input entities to Url, so I must to reflection the entities for get metadata and use it to build Url. To be clearly this, I will show one sample, assume that I have a input entity as:

    public abstract class EntityBase : IEntity
   {
        public string Query { getset; }

        public string Version { getset; }

        public string UserIp { getset; }

        public int ResultSize { getset; }

        public string HostLanguage { getset; }

        public string Key { getset; }


        public int Start { getset; }

        public abstract string SearchWebUrl();
    }
    public class Web : EntityBaseIWeb
    {
        public string UniqueId { getset; }

        public string Linked { getset; }

        // Safe type
        public Safe Safe { getset; }

        // ParticularLanguage type
        public ParticularLanguage ParticularLanguage { getset; }

        // Filter type
        public Filter Filter { getset; }

        // CountryCode type
        public CountryCode CountryCode { getset; }
     }

It is a web's entity, so we need knowing what are the fields that we need to build? To do that, we need creating a custom attribute as:

    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Property,
                    AllowMultiple = false,
                    Inherited = true)]
    public class UrlElementAttribute : Attribute
    {
        public string Name { getprivate set; }

        public bool Required { getprivate set; }

        public UrlElementAttribute(string name)
        {
            Name = name;
            Required = true;
        }
    }

After that I will decorate this custom attribute on my web's entity as:

    public abstract class EntityBase : IEntity
    {
        [UrlElement("q")]
        public string Query { getset; }

        [UrlElement("v")]
        public string Version { getset; }

        public string UserIp { getset; }

        public int ResultSize { getset; }

        public string HostLanguage { getset; }

        public string Key { getset; }

        [UrlElement("start")]
        public int Start { getset; }

        public abstract string SearchWebUrl();
    }

Finally, I will write code for exploring this entity as:

    public interface IUrlBuilder<T> where T : IEntity
    {
        string BuildUrl();

        T Entity { getset; }
    }
    public abstract class UrlBuilderBase<T> : IUrlBuilder<T> where T : IEntity
    {
        public T Entity { getset; }

        public virtual string BuildUrl()
        {
            var builder = new StringBuilder();

            var propertyInfos = Entity.GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

            foreach (var info in propertyInfos)
            {
                var attrs = info.GetCustomAttributes(typeof(UrlElementAttribute), true);

                if (attrs.Length == 0)
                {
                    continue;
                }

                var urlAttr = attrs[0] as UrlElementAttribute;

                builder.AppendFormat("{0}={1}&", urlAttr.Name, info.GetValue(Entity, null));
            }

            return builder.ToString().Substring(0, builder.ToString().Length - 1);
        }
    }
    public class WebUrlBuilder : UrlBuilderBase<Web>
    {
    } 

All thing is fine, and now we can convert from the entities to Url with a little bit code. Are you like that too?

+ Make Fluent Interface: As you knew, Gapi4net is written for easy used, so I implemented the Fluent Interface inside it and applied the Lambda Expression to association with Fluent Interface. It make the cleanly structure for invoked the APIs as:

    Gapi4NetFactory<T>
                .Init()
                .With(x => x.Version, "1.0")
                .With(x => x.Query, searchText)
                .Create();

I copied some of code from this link, and modified something to fixed with my solution. It is so cool! Below is full of this code:

    public interface IGenericFactory<T>
    {
        IGenericFactory<T> With(Expression<Func<T, object>> property, object value);

        T Create();
    }

    public class GenericFactory<T> : IGenericFactory<T>
    {
        private T _entity;

        public GenericFactory(T entity)
        {
            _entity = entity;
        }

        public IGenericFactory<T> With(Expression<Func<T, object>> property, object value)
        {
            PropertyInfo propertyInfo = null;

            if (property.Body is MemberExpression)
            {
                propertyInfo = ((MemberExpression)property.Body).Member as PropertyInfo;
            }
            else
            {
                propertyInfo = ((MemberExpression)((UnaryExpression)property.Body).Operand).Member as PropertyInfo;
            }

            propertyInfo.SetValue(_entity, value, null);

            return this;
        }

        public T Create()
        {
            return _entity;
        }
    }
 
    public static class Gapi4NetFactory<T> where T : IEntitynew()
    {
        public static IGenericFactory<T> Init()
        {
            return new GenericFactory<T>(new T());
        }
    }

That is the main techniques I used in Gapi4net. For more information please download my library and look it clearly. In next post, I will show you some of technical in ASP.NET MVC 3 Preview 1. Hope you will welcome it. Thanks for you read this post and see you next time.

Published Thursday, September 16, 2010 12:43 AM by thangchung

Comments

# Google API for .NET architecture (Part 2) - Context is King

Wednesday, September 15, 2010 1:06 PM by DotNetShoutout

Thank you for submitting this cool story - Trackback from DotNetShoutout

# Google API for .NET architecture (Part 2)

Wednesday, September 15, 2010 1:07 PM by DotNetKicks.com

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# Google API for .NET architecture (Part 1) - Context is King

Wednesday, September 15, 2010 1:11 PM by Google API for .NET architecture (Part 1) - Context is King

Pingback from  Google API for .NET architecture (Part 1) - Context is King

# Twitter Trackbacks for Google API for .NET architecture (Part 2) - Context is King [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 Google API for .NET architecture (Part 2) - Context is King         [asp.net]        on Topsy.com

# re: Google API for .NET architecture (Part 2)

Wednesday, August 22, 2012 7:00 AM by Morris

I do not even know the way I ended up right here,

however I assumed this put up was good. I do not realize who you're but certainly you are going to a famous blogger when you are not already. Cheers!

# re: Google API for .NET architecture (Part 2)

Monday, September 03, 2012 8:25 AM by Hutchings

Do you mind if I quote a couple of your posts as long as I provide credit and sources back to

your webpage? My blog is in the very same niche as yours and my visitors

would really benefit from a lot of the information you present here.

Please let me know if this alright with you. Thanks!

# re: Google API for .NET architecture (Part 2)

Monday, September 24, 2012 5:28 AM by Carroll

The other day, while I was at work, my cousin stole my iphone and tested to see if

it can survive a forty foot drop, just so she can be a youtube sensation.

My apple ipad is now destroyed and she has 83 views. I know this is totally off topic but I had to share

it with someone!

# re: Google API for .NET architecture (Part 2)

Tuesday, September 25, 2012 11:20 AM by Carson

Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point.

You clearly know what youre talking about, why

waste your intelligence on just posting videos to your weblog when you could be giving us something enlightening

to read?

# re: Google API for .NET architecture (Part 2)

Saturday, September 29, 2012 1:22 AM by Schmitz

Great post. I used to be checking constantly this weblog and I am impressed!

Very helpful info particularly the remaining section :

) I maintain such information a lot. I was seeking this particular information for a very long time.

Thanks and best of luck.

# re: Google API for .NET architecture (Part 2)

Sunday, September 30, 2012 6:53 PM by Mckenna

พระล้านนา พระล้านนา

พระล้านนา พระล้านนา พระล้านนา พระล้านนา

พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา

พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา

พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา

พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา

พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา

พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา

พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา

พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา

พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา

พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา

พระล้านนา พระล้านนา

พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา

พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา

พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา

พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา

พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา

พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา

พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา

พระล้านนา พระล้านนา พระล้านนา

พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา

พระล้านนา พระล้านนา

พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา

พระล้านนา พระล้านนา

พระล้านนา พระล้านนา

พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา

พระล้านนา พระล้านนา

พระล้านนา พระล้านนา พระล้านนา

พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา

พระล้านนา พระล้านนา พระล้านนา

พระล้านนา พระล้านนา พระล้านนา พระล้านนา พระล้านนา

# re: Google API for .NET architecture (Part 2)

Tuesday, October 02, 2012 2:46 AM by Coffman

I was wondering if you ever considered changing the structure of your blog?

Its very well written; I love what youve got to say. But maybe you could a

little more in the way of content so people could connect with it better.

Youve got an awful lot of text for only having 1 or 2 images.

Maybe you could space it out better?

# re: Google API for .NET architecture (Part 2)

Thursday, October 25, 2012 11:44 PM by nike tn

Assume‘capital t rubbish your time and efforts on just the gentleman/young lady,people who isn‘capital t prepared to rubbish their particular energy on you.

nike tn adidaschile62.blogspot.com

# re: Google API for .NET architecture (Part 2)

Sunday, November 04, 2012 12:16 PM by Hershberger

I was able to find good advice from your articles.

# re: Google API for .NET architecture (Part 2)

Wednesday, December 12, 2012 8:54 AM by Cornish

This page was really helpful. I actually really enjoyed

reading it. It makes me want to be gone play a

few more games than I normally would which isn't most likely the most productive thing I have ever done but regardless thanks!

# re: Google API for .NET architecture (Part 2)

Friday, December 28, 2012 3:28 AM by Sawyer

When someone writes an paragraph he/she retains the thought of a user in his/her mind that how a user can understand it.

Therefore that's why this article is perfect. Thanks!

# re: Google API for .NET architecture (Part 2)

Saturday, December 29, 2012 2:21 AM by Stone

Its like you read my mind! You seem to know a lot about this, like you

wrote the book in it or something. I think that you can do with some pics to drive the message home a

little bit, but instead of that, this is excellent blog.

An excellent read. I'll definitely be back.

# re: Google API for .NET architecture (Part 2)

Saturday, December 29, 2012 11:07 AM by Headley

When I initially commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is added I get four e-mails with the same comment.

Is there any way you can remove people from that service?

Appreciate it!

# re: Google API for .NET architecture (Part 2)

Monday, January 14, 2013 2:09 PM by Manns

I've been browsing online more than 4 hours today, yet I never found any interesting article like yours. It's pretty worth enough for me.

Personally, if all site owners and bloggers made good content

as you did, the internet will be a lot more useful than ever before.

# re: Google API for .NET architecture (Part 2)

Monday, January 28, 2013 6:17 AM by Schultz

I'm really enjoying the design and layout of your website. It's a very

easy on the eyes which makes it much more pleasant for me to come here

and visit more often. Did you hire out a designer to create your theme?

Fantastic work!

# re: Google API for .NET architecture (Part 2)

Sunday, February 03, 2013 4:40 AM by Pettway

Good day I am so happy I found your weblog, I really

found you by error, while I was browsing on Aol for something else,

Nonetheless I am here now and would just like to say cheers

for a marvelous post and a all round entertaining blog (I also love the theme/design),

I don’t have time to go through it all at the

minute but I have saved it and also included your RSS feeds, so when I have time I will be back to read much more, Please do keep up

the awesome b.

# re: Google API for .NET architecture (Part 2)

Sunday, February 10, 2013 12:25 AM by Delgado

Remarkable issues here. I am very satisfied to see your article.

Thanks so much and I'm having a look forward to touch you. Will you kindly drop me a e-mail?

# re: Google API for .NET architecture (Part 2)

Sunday, February 10, 2013 7:37 AM by Sharp

It's awesome to pay a quick visit this website and reading the views of all friends concerning this piece of writing, while I am also zealous of getting experience.

# re: Google API for .NET architecture (Part 2)

Wednesday, February 13, 2013 4:30 PM by Winn

I don't even understand how I stopped up here, however I believed this submit used to be good. I don't recognise who you're however definitely you are going to a famous blogger if you are not already. Cheers!

# re: Google API for .NET architecture (Part 2)

Thursday, February 14, 2013 5:54 AM by Stapleton

Wonderful beat ! I wish to apprentice while you amend your web site, how could i subscribe for a

blog site? The account aided me a acceptable deal.

I had been tiny bit acquainted of this your broadcast offered bright clear

concept

# re: Google API for .NET architecture (Part 2)

Friday, February 15, 2013 4:43 PM by Haro

you are in point of fact a excellent webmaster. The web site loading speed is

amazing. It sort of feels that you are doing any distinctive

trick. Furthermore, The contents are masterwork.

you've performed a excellent task on this matter!

# re: Google API for .NET architecture (Part 2)

Monday, February 18, 2013 9:39 PM by Lombardo

We are a group of volunteers and opening a new

scheme in our community. Your website provided us with valuable information to work on.

You've done an impressive job and our whole community will be grateful to you.

# re: Google API for .NET architecture (Part 2)

Wednesday, February 27, 2013 10:44 AM by Thibodeau

I don't know whether it's just me or if perhaps everybody else experiencing issues with your blog.

It appears as if some of the text in your posts are running off the screen.

Can somebody else please comment and let me know

if this is happening to them too? This might be a problem with my

internet browser because I've had this happen before. Thanks

# re: Google API for .NET architecture (Part 2)

Tuesday, March 05, 2013 4:50 AM by LuRaley

Heya i'm for the first time here. I found this board and I find It really useful & it helped me out much. I hope to provide one thing again and help others like you aided me.

# re: Google API for .NET architecture (Part 2)

Thursday, March 07, 2013 6:51 PM by Schulz

I am extremely impressed along with your writing talents and also with the format

in your blog. Is this a paid subject or did you customize it your self?

Anyway keep up the nice quality writing, it's rare to peer a nice weblog like this one today..

# re: Google API for .NET architecture (Part 2)

Thursday, March 21, 2013 7:11 PM by Majors

My partner and I absolutely love your blog and find nearly all of your post's to be exactly I'm looking for.

Would you offer guest writers to write content to suit your needs?

I wouldn't mind composing a post or elaborating on many of the subjects you write regarding here. Again, awesome blog!

# re: Google API for .NET architecture (Part 2)

Tuesday, April 02, 2013 8:51 PM by Arellano

I got this web page from my friend who shared with me regarding this website and at the moment this

time I am browsing this web site and reading very informative content at this time.

# re: Google API for .NET architecture (Part 2)

Tuesday, April 09, 2013 11:26 PM by Yancey

I'm extremely impressed with your writing skills and also with the layout on your blog. Is this a paid theme or did you customize it yourself? Anyway keep up the nice quality writing, it is rare to see a great blog like this one nowadays.

# re: Google API for .NET architecture (Part 2)

Wednesday, April 10, 2013 12:04 AM by Saucedo

If some one needs to be updated with newest technologies after that he must be visit this site and be up

to date daily.

# re: Google API for .NET architecture (Part 2)

Thursday, April 11, 2013 9:55 PM by Cronin

Wonderful beat ! I wish to apprentice while you amend

your website, how can i subscribe for a blog

web site? The account helped me a acceptable deal.

I had been a little bit familiar of this your

broadcast provided vibrant transparent concept

# re: Google API for .NET architecture (Part 2)

Friday, April 12, 2013 11:52 AM by Beaty

Hi there! I could have sworn I've been to this site before but after looking at many of the posts I realized it's new to me.

Regardless, I'm certainly pleased I came across it and I'll be bookmarking it and checking

back regularly!

# re: Google API for .NET architecture (Part 2)

Friday, April 12, 2013 9:40 PM by Collazo

I was curious if you ever considered changing the structure of your blog?

Its very well written; I love what youve got to say. But maybe

you could a little more in the way of content

so people could connect with it better. Youve got an awful

lot of text for only having 1 or two pictures.

Maybe you could space it out better?

# re: Google API for .NET architecture (Part 2)

Sunday, April 14, 2013 1:29 AM by Post

Great goods from you, man. I have have in mind your stuff prior to

and you're simply too excellent. I actually like what you have got right here, certainly like what you are saying and the best way during which you say it. You are making it entertaining and you still care for to stay it wise. I can not wait to read far more from you. This is actually a tremendous site.

# re: Google API for .NET architecture (Part 2)

Sunday, April 14, 2013 3:11 AM by Sommer

Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.

I've been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

# re: Google API for .NET architecture (Part 2)

Tuesday, May 14, 2013 6:49 PM by Loper

This design is spectacular! You obviously know how to keep a reader entertained.

Between your wit and your videos, I was almost

moved to start my own blog (well, almost...HaHa!) Excellent job.

I really loved what you had to say, and more than that, how you presented it.

Too cool!

Leave a Comment

(required) 
(required) 
(optional)
(required)