Checkout Mark’s strategy after WhattsApp acquisition.
Checkout Mark’s strategy after WhattsApp acquisition.
Today, i came to know that nokia announces it’s 1st android phone. They are coming back in to dominate the mobile space again. it would be exciting the watch this space from now. Let’s see how samsung reacts on this.
In Today’s blog i just wanna to share one new technique which is been implemented by Microsoft team in their 2013 edition of Visual Studio, where in you can do Live Code Search while writing the code in the editor itself. let’s suppose you are writing some program, but you are missing some sort of syntactic sugar or How Do I Steps, then in that case after installing Add in, if you trigger Intellisense, then this will give you How Do I option as the 1st option, when you select the same, it will open a new editor with Bing embedded in the editor, so, here when you type your query, it will search the more relevant snippet based on your project context.
So, with this I would Like to wrap, Till then stay tuned and happy Coding.
Let’s continue the last session and let’s talk more about HTML Helpers. Basically,HTML is a property of the view page base class which is used to create
1) Create Inputs
2) Create Links
3) Create Forms
Many of these helpers are quite intelligent, like we have @Html.EditorFor(model=>model.FirstName) ,so what this editorFor will do here, it will walk through the property definition for that partciluar and then based on that, it will emit that particular control on the page, like if its a string property, then it will emit textbox here, if it would have been a boolean property, then it would have emitted that checkbox control on the page.
so, let me use couple of these property and explain you actual scenario. so, what am willing to do here, is to edit the review of each movie. so what this will do, it will take an action name say “Edit” with some anonymous value, here am passing id just to identify which review am planning to edit. i can pass as many as required anonymous parameters in there.
So, now what am saying to routing engine, that here are some essential values that needs to get passed on the url, these anonymous values are basically additional information which need to get passed in the url. Now, what i need to do here, is create one edit view as shown below in the sample
so, all the basics are fairly simple, this time i just selected from the scaffolding template Edit view.
so, it produced me a fairly simple Edit View. so, now when i build and refresh the page, it should produce me the below page.
now, look at the url, it took the value which i passed in from the Reviews Index page. also, accordingly it emitted the tag. Now, let’s look quite closely in here. for doing the same we need to look @ page source and see what actually happening over here.
so, now if any i change any value and hit save, then in that case it would be the [HttpPost] section of edit part which is going to be triggered. so, am going to change this code little bit which is given with scaffold-ed template.
so, what i have done here is grabbed the value which am going to edit, and to move the values in i have used TryUpdateModel, and what this will do, it will go through a process known as Model Binding. Basically, model binding happens anytime you even have a parameter in an action method. so, when i have id in edit action, so what model binder in MVC will find out that id for me and move it into that for me. so, here model binder is checking that rating property and checking is same exists and in the mean time if any thing fails or any validation error occurs, tryupdatemodel will return false and that instant i don’t want user to save review and if that happens, i’ll return the user on the review page to fix the issue and then save it again. And if every thing goes well, then we just save and return to Reviews page. For, now am just redirecting it Reviews page, since we are not working with database till now.
now, let’s save this rating to 10 and try.
now, let’s try to give some erroneous input and then save the same, like below:-
here, the error message is little bit misleading. obviously, we could easily fix the same. but we’ll see in the later section. Till then stay tuned and happy coding.
Today, in this discussion we’ll talk about more on razor views. But, today we’ll dig inside layout views. What happens, Layouts basically use inherited methods to display content areas like
so, layouts are basically similar to master pages what we have in web forms, it’s rather easy to understand. Methods like RenderBody or RenderSection will plugin the content views @ the specific page locations.
so, one obvious qn comes here, is how does asp.net mvc knows like how to use this layout view. and answer to this question which comes from _viewstart.cshtml which is there in the shared folder, here Layout property is getting set like below
so, whenever my view gets started, so prior to that this file runs and that is what the convention in asp.net razor view engine. so, basically this file resides in the root of the views folder, so before view runs basically this root runs, now let me go ahead and have multiple layouts for an application. is it possible. hmm yes.
so, what i need to do for this i just need to copy my viewstart and put inside one of the views sub folders and set the layout property with a different name.
Now, in order this to work we need to have that layout page defined in there. i can also have layout page set on the per view basis like
then it would look for that particular _Layout2 . I can also close the layout view just by saying
RenderSection is again a method which is going to inject content views @ one or more than one location, but this is optional.
it’s definition goes like
@RenderSection(“featured”,required:false) –> so, this means a content view can have section and cannot, but if i set to true the required field, then i must have section declared in the content page, otherwise it will result compiler error.
Now, let’s see how to decalare a section
We are showing Latest Movie Reviews for all the Movies @Model.Count()
now, we can decalre this section any where on the page. Also, since we are on the home page, we could also go ahead and make option for my Movie Reviews, so that i can reach there from the home page itself like shown below.
Now, when i refresh the page i would get the below link on every page as shown below:-
With this i would like to wrap this section. In the next section we’ll see more about HTML Helpers, till then stay tuned and Happy Coding.
In this section, we’ll discuss more about events, Till now we have seen that how delegates work. Now, events are basically wrappers around the delegates.So, the process of defining an event with the special C# keyword event and then we are going to come and use delegate like shown below:-
public event OrderPlacedHandler orderPlaced;
so, basically as discussed earlier events on it’s own doesn’t do anything, so we need to have a pipeline to route the data from point A to point B, so that event raiser can get the data over delegates. Although, we can use delegates pretty easy way on their own like we used in earlier sections, but with events it’s also very easy way to do that
so, now let’s get started and see in real world scenario, how it actually works so for that am going to add one class in there and give it a name let’s say Implementer and this Implementer class is going to do certain works assigned to it. so, now let’s assume whoever calls this method wants to know how the work is getting progressed. so, now i’ll define a event in there. so, basically what am going to do is to grab the delegate from the main file and define the same in there in Implementer class.
Now, assume that when the work is completely done, we would like to raise an event, so in that case what am going to do is to make another event and this time am going to use built in delegate EventHandler as shown below.
so, now this Eventhandler is built in .net framework like what we have done in button_click event or any index_change event, this is there by default. so, if i just see it’s definition by doing F12 on this, then will return below delegate signature.
so, basically in this case i don’t have any custom data to pass, i just have to notify that work is done. so, this was basically defining two custom events in a implementer class. now, let’s see that how to work with these events or actually raise these events.
so, when it comes to raising an event, we just need to invoke the event just like the method. so, if you look this closely and ask these questions like what is behind the scenes of event, that is delegate. so, even we basically invoke the delegates like a method, so before we actually call an event, it’s really important to check if there is anything in the invocation list, because if delegates behind the scenes null and nothing is there, then we get exception if we try to raise the same. it’s like calling a object on a null method. so, what we generally do here, is do a quick check if the event is not null and then invoke the event like a method.
// Checking events then invoking
another option could be casting event as like delegate and see if delegate is not null and then invoke the same like shown below
orderPlacedhandler del = orderPlaces as orderPlacedhandler // done casting as delegate.
and then check if delegate is not null.
so, the proper way of raising an event to attach separate methods for each event you want to raise. so, basically my intention is i just have to get the notification @ hourly basis say like the progress of work an then when it’s completed, i should also get notified.
so, now why am doing protected virtual here, so well anybody inherits from my Implementer class, they want to override the way i raised the event, so basically am providing kind of opportunity to them to implement their own way. this is obviously my personal choice writing this way, but if you don’t want this you can certainly make it private, depend on you.
now, inside the implementation i could do certainly either of the two case we discuss above for raising the events like as shown below.
so, either way you can do this implementation. Now, what i’ll do here is I’ll make one more method for event completed like, i need to rename the earlier method to OnOrderPerformed as this was the one which gets executed on regular interval now, another method which will be raised on completed will be actually inherited from EventHandler and Eventhandler doesn’t take any parameter, hence i have stripped this off as shown below.
now, as you see in the above section it takes two generic parameter and since i don’t have any eventArgs now, so i’ll pass the same as empty like shown below:
so, now i can go ahead and raise my events as shown below
so, now whatever no of listeners we have, they will all get notified with the above methods. so, if you see this method, this method is void , but am able to return two types of data out. so, this is quite interesting. now, this works fine, but in cases where in I need to pass on a bunch of data, then in that case i would be using EventArgs.
so, now let’s see how EventArgs work in normal practice. EventArgs is quite useful when i have a lot of data to pass on and then rather passing as is like shown above would be a mess, we could use EventArgs to pass the data. EventArgs will keep the data encapsulated inside.
so, let’s get started, so what we could do here, we can write a custom EventArgs class which basically gets inherited from System.EventArgs class. and my this custom class will have all the properties which i want to pass on like shown below
so, this is very familiar POCO (Plain Old CLR Object class) like all the classes we have. so, now i have to change my delegate definition like shown below:
so, now when i have to raise the event i have to play with these two parameters declared above and when i have to handle the event i have to implement likewise. so, now it’s become very easy and clean.
so, now the actual implementation goes like as shown below, we have one Custom EventArgs defined as shown below,
Then, we go ahead and implement the same in our main Implementer class as
Let’s suppose i would like to eliminate the delegate entirely, then in that case we can say like shown below
so, basically this will generate a kind of equivalent code which used to get generated from the delegate itself. so, now in oder to use the same i need to make one small change in my code as shown below.
and that’s it now, i have a cleaner and easier way of raising an event with EventArgs. so, basically it depends on you that how you want to use the same. Most of the times when you just have events, then you can use EventHandler<T> as this just saves couple of lines of code thats it.
although, we have just wired up, but we need to call the method to do some work like shown below
ok, we have create one instance of implementer, wired up the event and then in our callback will write the hours and orderType.
so, now i would like to handle the work Completed event, so in this case it would be like
so, with this i would like to wrap this session. It seems it became quite big but anyways stay tuned and happy coding.
Now, in this section we’ll see that how to work with delegates which are returning values. Till now, we have seen that delegates are returning nothing, they are just as is processing the data that is getting passed in. so, let’s try to work with one which is returning some data. so, i’ll change the code a bit and then it will look like
so, in the different methods, you could see that i have modified the return statement by just adding the nos 1,2 and 3 in three different methods, just to differentiate between which method is getting returned. Now, when i run the program, it will produce the below output as shown below:
and the reason for this is quite simple, that even though we have multiple delegates in the invocation list and each one is returning different values individually, but in case of multicast delegate it will produce that value which will be returned in the last. Hence, it produced the last one as a result. In case i want each individual results so in that case i would need to call the delegates explicitly. so, with this i would like to wrap, till then stay tuned and happy coding.
This is the continuation of 1st part of delegates where in i have explained the basics of delegates, and how to work with that. But, let’s suppose i have multiple delegates and i have to add the same in invocation list, then how would i do that. so, let’s get started. 1st of all i would add one more delegate and it’s corresponding method in the program, so that i can add multiple in the list.
then, inorder to add the multiple delegates in the invocation list i would need to modify the above code as in
so, this will produce the below result. so, basically, now del1 is now appended with del2 and del3 as well and hence producing the collective result. also, one more point to notice here, that this method call is synchronous means it gets invoked as it is declared.
so, this is basically an example that how multicast delegate works behind the scene and it makes it really easy to have one and point of reference to invoke multiple subscribers. so, with i would like to wrap. Till then stay tuned and happy coding.
Twitter’s CEO Dick Costolo and Microsoft’s new CEO Satya Nadella are basically the same person pic.twitter.com/67Icf2sA5e
In today’s discussion i would like to discuss more about delegates. This is one of the topics which i struggled most, hence thought to talk about. But, when i checked the topic back again, then understood it’s importance and came to know, it’s not that tough.
so, let’s get started.so, before jumping to the demos, let’s jump into the basic lessons of delegates and it’s role in real world.
Delegates:- Frankly speaking delegates are basically glue between event raiser and event handler. Delgates are the one which is making everything possible, when it comes to raising an event and handling an event. so, basically if i raise an event, then how does it will get passed on to the event handler, then in order to bring this guy event raiser to event handler, delegates play a key role here.
Events:- Events are basically important or key part as far as delegates and it’s role is concerned. it could be simple click or index change event in dropdown or we might even have custom object like an order object like when a customer places order and that notification goes out and notifies the other part of the system. so, basically an event is a notification which is getting passed on to the event handler. also, it provides a way to trigger notifications. so, basically this message goes out to one or more subscriber who has subscribed for the event.
Role of Delegates:- Delegates role are very important, they bring event data from one point to another point means it carries data from event raiser to event handler. so, delegates are often called as function pointer. now, actually when we create delegate and when we use events and when we are creating delegates, so basically we are doing to couple of things. there is a special class in .net called Multicast delegate. and this is the class which tracks everyone that’s listening. so, when the event notification goes off with some information like OrderId = 1, then this information needs to be sent to all the listeners. so, if there are 100 listeners, then we would have 100 objects inside of invocation list.
so, the reason why we call delegates a function pointer,it’s because of the reason of event handlers because we need to point the data through the pipeline over to eventhandler.
Role of Eventhandlers:- so, the final thing is event handler. so, when the event raiser raises the data and then delegates route it to event handler. then event handler process the data, may be it updates the UI or database. so, it’s basically responsible for receiving data from delegates and then processing the same. It normally receives two parameters
1) Sender and
2) EventArgs. Event Args are basically event data.
//Creation of delegates:-
so when it comes to creating delegates, it’s actually very simple process. so, actually we have to define basically blueprint for eventhandler. so,
a custom delegate is defined by using the word delegate. Now, this is kind of magic keyword, now behind the scenes when the compiler sees the delegate keyword,
it actually generates a class that inherits from another .net classes. so, basically a delegate is a blueprint for the method alongwith data which is going to be passed on to the handler.
Delegate has two really important property
1) method and
2) target property.
so, the method is that pipeline has to dump the data somewhere and you have to define the name of the method, where that data should go. Now, the target would be if you have to have an object instance where that method lives, then target would be actual object that has the method.
3) GetInvocationList():- Now, there is a class called multicast delegate that’s built into the framework as well. so, every delegate we create once compiled will inherit from multicast delegate. so, multicast delegate is really a way to hold multiple delegates. in other words, i may have one message to send out, but i have to send it across multiple pipelines. it’s not like that we are directly inheriting from delegate or multicast delegate rather the way we do is we use delegate keyword.
so, multicast delegate is just an array of multiple pipelines or multiple delegates. and it happens synchronously kind of top of the list.
//Creating Delegate –> Defn of delegate
public delegate void OrderPlacedHandler(Int16 Hours, OrderType orderType);
Now, when i run the same , it will work properly and give the output as shown below
so, in the above example basically i have hardcoded the call and then invoked, but let’s consider a scenario where in it is really very useful, it could be
dynamic call and from there we’ll be using the same like shown below
so, with this i would like to wrap this up. Till then stay tuned and happy coding.