Thursday, December 25, 2014

Do‘t listen to them, those who never did it in their life.



There was an old link in one kingdom.  He was very brave in his early age and ran his kingdom well. Now got old and not getting charm in material life.

He announces in his kingdom “I wish to become monk now. People who will able to bring spirituality in my mind by reading Bhagabat (A holy book in INDIA), I will give him my whole kingdom to him and will live in forest to spent spiritual life”

So good, but the king attached one condition with announcement “If someone tries but could not! I will send him to jail”

Many priests, Brahmin tried but could not and sends to jail by king. One-day one old priest told to his wife, Hey! Shall I try once to bring spirituality in king’s mind?

 Wife laughed at him and replied “Many great readers fail, and you are saying you can do it?” Priest replied,”Let me try once”.

In one beautiful morning, he went to king’s palace and expressed his wish. Kind allowed him to read Bhagabat, after reading of some chapter, King said “Hey man! Stop your reading, It’s not able to stimulate my desire”, as condition the old Brahmin was send to jail.

There was a very young monk in kingdom who left his house in his childhood to practice spiritual activity and to realize god.

 After 12 years he came once to visit his home in disguise. He is seeing his mother at home but not seeing his father. He asked, woman! Where is your husband?

His mother told the who story of the king and said , the king has send his husband to jail while he could not able to bring spirituality in king’s mind.

The young monk meets to king and wish his desire of reading Bhagabat. King allowed. At first day of reading, the king felt good. In second day, he relaxed. In third day, his heart became full with enjoyment. In 4th day, his eyes flooded with tears.

King said. Hey young monk, Please stop your reading, it is enough for me. Now I am no longer interested in this material world. However, let me ask you one question. “How did you do the task? where many wise person and great reader in my kingdom could not? “

The young monk replied , you have send my father in jail for same reason ,let take him with us and go to nearby garden.

They have reached nearest mango garden. The young monk binds the king in one mango tree tightly and to his father in another one.

Now, he said. Let’s open each other’s tie and get freedom. King replied, how can we do it ? We are tightly bind with tree.

Monk replied, that is the reason, all reader those who read Bhagabat for you are tightly bind with material world, how they will make you free from it?

In my childhood, I left my home and still now chasing spirituality, I am free from relations and boundaries.
 In our life, we will meet with many readers but not listen to them; they cannot help you to reach your goal. Listen to only a monk, who has done it, realize it in his life. Follow his instruction.





Wednesday, December 24, 2014

Inject service with Annotation to Controller of Angular

Inject service to controller in Angular is very common operation. Here is sample code to do it.

        var app = angular.module('myApp', []);

        app.service('myService', function () {
        });

        app.controller('myController', function (myService) {
        })


In this example we have injected custom service to controller function by the service name. In case of Angular’s own service, the implementation is like this.

app.controller('myController', function ($http) {
        })

Here we are injecting $http service to controller. This type of implementation is fine if we do not want to minify our .js file, but if we minify then program may not run correctly. The reason is, Once we minify code, the minification program will minify the function argument as well and the dependency injector would not be able to identify service correctly.

There are two ways to solve this problem.
1)      Inject dependency explicitly using $.injector    

At first we have created simple JavaScript function and then injected the dependencies into it.
var app = angular.module('myApp', []);

        //Define function
        function myController() {
        }

        //Inject dependency
        myController.$inject = ['$scope'];

        //Attach function to Angular
        app.controller('myController', myController);

 At last we are attaching the function to module to treat the function as controller of Angular. Now, even we minify the file , the argument name will not minify hence application will run properly.

    2)      Inline annotation

This is simple and compact solution of this problem. Here is single line of code to do same.

       var app = angular.module('myApp', []);

       app.controller('myController',['$scope', function ($scope) {
       }]);

Custom filter in AngularJS

Custom filter is useful when we want to implement our own logic in time of iteration. Filter is angular component, which used to sort data based on certain criteria.

In this example, we will implement our own filter and we will use it to manipulate data.  Here we have implemented simple filter, which will make uppercase of input data.

var app = angular.module('myApp', []);

app.filter('makeUpper', function () {

            return function (item) {
                return item.toUpperCase();
            }

        })

The implementation is very simple , we have created myApp module and attached “makeUpper” filter with it. The function will take input string and return upper case of the string.
Now, we have to use the filter in view. Here is sample code to use.

        <input type="text" ng-model="name" />
        <div>
            {{name | makeUpper}}
        </div>


Angular allows filter associated with “|” over model data, Just we have to specify filter name. 


We are seeing that the output value is coming in uppercase.  So, this is one of the simplest example of filter in AngularJS. Let us assume another use-case.

We want to show only those strings (for example name) which are starts with particular character (say “S”).
Have a look on sample implementation.

    <script>

        var app = angular.module('myApp', []);
        app.controller('myController', function ($scope) {
           
            var persons = ["Ram", "Sourav", "Sudip"];
            $scope.names = persons;
        });
     
        app.filter('startsWithS', function () {
            return function (name) {
                if (name.substring(0, 1) == "S") {
                    alert(name);
                    return name;
                }
            }
        })
      
    </script>

We have implemented our custom logic to check whether the name starts with “S” or not.  If starts we are returning the name otherwise not.
Here is view to execute the filter.

<body ng-app="myApp">
  
    <div ng-controller="myController">

        <div ng-repeat="n in names">
            {{ n | startsWithS}}
        </div>

    </div>

</body>

Now, we are seeing that only 2 names which started with “S” are showing.



Inject one service into another service in AngularJS

The philosophy behind implementation of service is to use same functionality in more than one place. In angular service are singleton component across application.

As it is singleton, we need not to create particular object to call/consume service. In Angular, we can inject any service to any other component (read controller) and consume the service within this component.

There might be some scenario, where we might create one base service and want to consume the base service from any other services.

In this example, we will learn to inject one service to another service using DI pattern of Angular and we will consume the base service within it.


  
var app = angular.module('myApp', []);

        app.service('messageService', function () {
            var success = function () {
                return "success";
            };

            return {
                success: success
            };
        });

The ‘messageService’ is the base service, which is, attach with app module.  We have implemented success function which will return “success” string as ,message once it get call.

Fine, now we will implement another service and will inject messageService into it. Here is code implementation.

        app.service('messageConsumer', function (messageService) {

            var successProxy = function () {
                var data = messageService.success();
                if (data == 'success') {
                    return "executed successfully"
                }
            }

            return {
                success: successProxy
            }
        });


The implementation is pretty simple, just we are injecting service name into it. Now , successProxy function will internally call message to get data from base service.

Here, is one sample controller which again consuming “messageConsumer” service into it.

        app.controller('myController', function (messageConsumer) {

            alert(messageConsumer.success());

        });

And we will get below output once we call “myController” from application.


Return promise from service of AngularJS

Hope we all have basic concept of promise in Asynchronous operation. The implementation of AJAX with asynchronous operation heavily depends on promise.

The semantics of Angular dictate that you use promise as a sort of ‘Callback handle’ – do something asynchronous in a service, return a promise and when the asynchronous operation done, then function of promise will trigger.

By keeping this theory in mind, let’s implement same in AngularJS.

Here is sample code for Service, we have used factory to implement service. We have used $q service of Angular to implement promise and defer. 

var app = angular.module('myApp', []);

        app.factory('myDataFactory', function ($http,$q) {

            return {
                getdata: function () {
                    var deferred = $q.defer();

                    $http.get('../DummyData/getData').success(function (value) {
                            deferred.resolve(value);
                    }).error(function (msg, code) {
                            deferred.reject(msg);
                    });
                    return deferred.promise;
                }
            };

        });


Here is code in service, Which is returning a simple object.

public class person
    {
        public string name { get; set; }
        public string surname { get; set; }
    }
    public class DummyDataController : Controller
    {
      
        public ActionResult Index()
        {
            return View("Index");
        }

        public string getData()
        {
            return JsonConvert.SerializeObject(
             new person { name = "Sourav", surname = "Kayal" });
        }

    }

Now, we will consume the service from controller. Here is sample code to consume the service.

app.controller('myController', function ($scope,  myDataFactory) {

            var returnValue = myDataFactory.getdata();
            returnValue.then(function (retData) {
                alert(JSON.stringify(retData));
            }, function (error) {
                alert(JSON.stringify(error));
            });

        });

then() function will get execute if promise is success type and if it failure then error function will get execute.


Friday, December 12, 2014

What do you like? To give or take?. Here is a beautiful study on it.

Moreover, the give and take process helps us to fulfil our need in human life. In ancient age people used to use exchange policy, exchanging something with some other things. Scenario changed when people invent currency. Which is common for all (At least within one country) and they can get anything (almost, let us not fall into debate what we cannot now) by it.




We get things, be happy (How happy we feel ? when delivery boy ships our online order in our hand . J )  use it, one day it became old or damage , we throw it or simply forget about it , just no longer use it.

Let me ask you one question! “Which gives you more happiness? By taking something or giving something?

 Many will say by “taking things” (haha.. Oh ! Not in mouth but in heart) and obviously people are there who feel happy by giving others, by helping others.

Here is story, which is all about human nature in time giving and taking . This is not a story but a true study.

There was a professor in a college .One day he announced in his class, “Guys! In coming Sunday, we will go for movie in nearby theater, please raise your hand those who are interested to go.“

Almost whole class!. Why not? Teenage student likes movie and nothing bad into it. They went for a very nice and popular heart touching romantic movie.  Comes back and everyone enjoy on that day.

After few weeks, the teacher cane in classroom and announced! “I am planning to go our nearby slum in coming Sunday and we will make bit charity over there, so interested people can raise their hand” Again almost whole class! Why not ?Young blood always happy to help.  Therefore, they went and whole day they spent with children over there, gave books, toys and medicine to suffering people.

So far so good. After one year suddenly the teacher, announce in class “Guys! Did you remember that one year ago we went for movie and after few weeks we went to our nearby slum to helped people “?

Students replied, “Yes sir! We remember those events “

Good! Now get one paper and write your feelings of two days in back to back of the sheet.  Student wrote and given to professor.

What a pleasant survey result! One side of each Sheet is almost empty and opposite side is not enough to express their heart.

Which side is empty? Obviously,  the event where they went for movie. Even somebody could not able to remember the movie name and the story.

Moral of the story:
We remember more when we give; we remember more when we share. It might be by helping others with belongings or by sharing your thought.
So, give always, do not say no.  Help needy.




Wednesday, December 10, 2014

How we treat to our parents ?


Found this post in Internet , not written by me. Just wanted to share.

A long time ago, there was a huge apple tree. A little boy loved to come and play around it every day. He climbed to the treetop,
ate the apples, and took a nap under the shadow. He loved the tree and the tree loved to play with him.
Time went by, the little boy had grown up and he no longer played around the tree every day.
One day, the boy came back to the tree and he looked sad.
“Come and play with me”, the tree asked the boy.
Boy: “I am no longer a kid, I do not play around trees anymore” the boy replied. “I want toys. I need money to buy them.”
Tree: “Sorry, but I do not have money, but you can pick all my apples and sell them. So, you will have money.”
The boy was so excited. He grabbed all the apples on the tree and left happily. The boy never came back after he picked the apples. The tree was sad.
One day, the boy who now turned into a man returned and the tree was excited.
“Come and play with me” the tree said.
“I do not have time to play. I have to work for my family. We need a house for shelter. Can you help me?”
“Sorry, but I do not have any house. But you can chop off my branches to build your house.” So the man cut all the branches of the tree and left happily.
The tree was glad to see him happy but the man never came back since then. The tree was again lonely and sad.
One hot summer day, the man returned and the tree was delighted.
“Come and play with me!” the tree said.
“I am getting old. I want to go sailing to relax myself. Can you give me a boat?” said the man.
“Use my trunk to build your boat. You can sail far away and be happy.”
So the man cut the tree trunk to make a boat. He went sailing and never showed up for a long time.
Finally, the man returned after many years.
“Sorry, my boy. But I do not have anything for you anymore. No more apples for you”, the tree said.
“No problem, I do not have any teeth to bite” the man replied.
Tree : “No more trunk for you to climb on.”
“I am too old for that now” the man said.
“I really cannot give you anything, the only thing left is my dying roots,” the tree said with tears.
“I do not need much now, just a place to rest. I am tired after all these years,” the man replied.
“Good! Old tree roots are the best place to lean on and rest, come sit down with me and rest.” The man sat down and the tree was glad and smiled with tears.
This is a story of everyone. The tree is like our parents. When we were young, we loved to play with our Mum and Dad.
When we grow up, we leave them; only come to them when we need something or when we are in trouble.
No matter what, parents will always be there and give everything they could just to make you happy.
You may think the boy is cruel to the tree, but that is how all of us treat our parents.
We take them for granted; we don’t appreciate all they do for us, until it’s too late.
~ Moral ~ 
Treat your parents with loving care…. For you will know their value, when you see their empty chair…

We never know the love of our parents for us; till we have become parents.

Sunday, December 7, 2014

Cut down your relationship if you want to run fast.





We all know the well-known proverb,

                If you want to run fast, run alone.
                If you want to run long, run together.

Definitely, choice is yours. From my personal experience, I feel that most of the person chooses the second line. Why not ? We are human being and belong to certain community as well as responsible family member.  

From our childhood, we have grown by our parents (at least in Indian scenario), they spend money like water (Again in Indian scenario, where parent’s dream to educate their children) until we get settle and earn our bread. So in return we too have responsibility to feed them (forgive me for my selfish word “feed”, It is not only feed but to be with them) in their old ages.

Sometimes the scenario is not like, what I explained .Anyway I will not go deep into it. Let us come our point of interest.

Being a family member, we might have fulfilled lot of responsibility and commitment, when relationship is more there is more responsibility, more commitment and more chance to live your life on because of others.

Even we forget that; how we have passed many years of our life to keep people happy to make them smile. So many times, we compromised with our decision and our choices.
And suddenly we realize I am not in my position what I supposed to be now.  Believe me, I talk to many people they mourn and said, “I supposed to be something else. Wanted to start my something own.. Still working for some other and cannot do it now because now it’s risky for me to start my own company!”

 Why risky ? The reason is. They are running together. If somewhere you fail, someone will hurt.  Probably they have chosen the second line of the proverb.

Once I talked with one CEO of some funding company and there company name is attach with certain age(19) . He explained why they chose the name! Yes, 19 years old age is the perfect to start something of someone’s own. When I understood this, I was 24, already running behind schedule.  However, I will not say its dry run. Because at that age I have founded the blog , already written 200+ articles in this and another 400+ in various .NET community including c-sharpcorner.com , dotnetfunda.com and codeproject.com.  1500000+ readers have read my articles and share their thought with me.

Anyway, let us come again in discussion. Instead of being a family member and social person, I will not fully agree with the second line at least in my whole life.

Yes, second line might give you more value in community but first line will give you more mental satisfaction. Your life could be more meaningful.

At least live half of a day for you, for your dream run fast, as much as you can, live alone!