Thursday, February 8, 2018

Tip on MVC URL Routing

Recently, one of my colleague asked me a question, in which his routing was not working as expected. Let me mention the exact URL he was giving:

URL 1:

URL 2:

And his code snippet was as below:
    public class RouteConfig 
        public static void RegisterRoutes(RouteCollection routes) 
                name: "Default", 
                url: "{controller}/{action}/{id}", 
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 

Where he claimed that he didn’t change anything in his Route.Config file and his Controller looks something like this:
  public ActionResult StudentEnquiries(string StudentID) 
        return Content(StudentID); 

Now his question was, when he is giving URL 1, he is not getting the expected output, which is 44 whereas in URL 2 everything was working as expected.
Problem statement looks very straightforward. Any guesses, what went wrong with his code?
I’m sure most of you might have already figured out the solution. But for the sake of rest of the readers, I’ll elaborate more on it.
The route mentioned in Route.Config file outlines variables in the URL and the action parameter maps those URL variables to input parameters.

So, basically id has to be replaced with StudentId in routing parameter and we are done.
Hope you enjoyed this small tip. Happy troubleshooting.

Monday, January 1, 2018

Invoking web application from console application (.Net Core) via command prompt

In this article, I’ll be giving a walkthrough on how to create a console application and changing that into a web application. Or in other words, invoking a web application from a .Net core console application. And that too completely from command prompt. If you are a command prompt lover, you may love it. So, let’s gear up and proceed step-by-step.

Verify .Net Core
If you are creating a .Net Core application for the very first time, then it is good to verify whether it is installed on the system or not and this can be done by typing a simple command dotnet --version as shown below: 

Create Console application
Now we are sure that required setup is present on our machine, we can proceed and create our first Console application using command dotnet new console as shown below: 

On successful execution of above command, you will see that Program file and project file is created in your local directory and same can be verified by opening Program.cs in notepad using below command:

Building and Running Console application
Next step would be to see the output from the console application and that can be done by run command as shown below:

In above screen shot you can see that output “Hello World!” is shown on the screen which means both compilation and execution will be done using single command.
Console app into a Web app
For any web application, first we have to add dependency packages. So, let’s go ahead and add reference of AspNetCore library from Nuget and that too via command line as shown below:

In above screenshot, you can see that dependent package is added to .csproj file. Run your application and you would still be able to see the console application output as we didn’t change our app. If you are facing any reference related errors then run the restore command as dotnet restore, and things will be alright.
Add Startup file for Web application
As a practice, usually any web application starts with a Startup.cs file, we will also go ahead and add a Startup.cs file in our project with basic skeleton and namespaces added as shown below:

Next is to fill in the missing parts of Startup class file to make our code functional. Let’s quickly add the code to the Startup class:
  1. using System;  
  2. using Microsoft.AspNetCore.Builder;  
  3. using Microsoft.AspNetCore.Hosting;  
  4. using Microsoft.AspNetCore.Http;  
  6. namespace SampleCore  
  7. {  
  8.    public class Startup  
  9.    {  
  10.     public void Configure(IApplicationBuilder builder)  
  11.         {  
  12.         builder.Run(appContext =>   
  13.          {  
  14.         return appContext.Response.WriteAsync("Hey, I'm from Web!");  
  15.          });  
  16.         }  
  17.    }  
  18. }  

Hooking up the web application in console
At this point, if you will run the application, you will still get the output which is mentioned in Program.cs because we have not told the Main() about our Startup class.

So, let’s quickly go ahead and plug our web application into console application. Below is the code to do so:
  1. static void Main(string[] args)  
  2.         {  
  3.             var hostBuilder = new WebHostBuilder()  
  4.             .UseKestrel() //tiny web server. It can be replaced with any web server  
  5.             .UseStartup<Startup>()  
  6.             .Build();  
  8.         hostBuilder.Run();  
  9.         }  
Now we are all set to run our application.
Launching an application 
Go back to command prompt and fire dotnet run and you will see that your application is now an web application with web server up and running.

Hope you like this. Enjoy reading.

Friday, December 29, 2017

Verify database prior to data insertion via EF

Recently, one of my colleague got a requirement on inserting data into database using EF. His issue was, how to verify if the database schema is proper or say all the columns in the tables matches with his POCO entities. Hope few of you must have come across similar scenarios.

Here is the quick solution for this.

In such scenarios, developers can do the schema compatibility check prior to inserting any data into the columns to ensure that model class still holds good with database tables.

bool isModelValid = yourContext.Database.CompatibleWithModel(true);

In the above method, passing the correct boolean value will do the trick for us. If the parameter value passed is true, then the framework will throw an exception if the schema doesn’t matched with your model class.

Isn’t it a useful tip?
Happy troubleshooting!!!

Sunday, December 24, 2017

Ways to add dependency packages in .NET core

This would be a very short article on how to add dependencies in .Net Core.

Well, there are many ways to achieve this. One is via Visual Studio and another way is through command prompt. Let’s quickly have a look. 

1) From Visual Studio: 
This is the straight forward way for the ones who want to use user interface to add dependencies. Right click on your project/library and get it from Nuget gallery. 

2) From Command Prompt: 
If you are a command prompt fan, then there itself, you have 2 choices.
A) Open command prompt. Navigate to your project directory and simply fire:

C:\<Your project directory> dotnet add package Microsoft.AspNetCore 

B) Alternative you can go and add the reference directly in .csproj file as shown below:

and restore it from command prompt using very simple command:

C:\<Your project directory> dotnet restore

Happy learning!!!

Sunday, December 17, 2017

What and How of Requirement Gathering - Part 3

In continuation to my previous post, I’ll add some more techniques which can be used for information gathering. 

Prototyping allows to gather information by faking the production environment. In this technique, quite a lot of tools can be used to collect information, such as software programs to monitor/record mouse clicks or keystrokes, camera to monitor visual activity, etc. 

Basically, what type of tool should be used is solely depend upon what type of information you want to collect. The cost of prototyping might be high because information gathered from this approach can be easily validated with a reason that prototyping is experimental rather than the responses received from users. 

Note: Prototyping is preferred in the scenarios where it is impossible to shadow a person.

Prototype can help you to get below sort of information: 
  • Use of technologies, tools and applications. 
  • Verification of workflow. 
  • Customer specific quality requirements and goals. 
  • User interface related issues.

User Instruction: 
Another approach which can be used to gather information can be User Instruction. In this technique, users actually train you on the tasks that they perform.  This allows you to participate in each and every activity and view each and every step from the user’s perspective. In this technique, user can gain more knowledge as compared to reading documents. 

Note: In this method, it is mandatory for the user to be habituated to teaching others else it would be very frustrating for both the parties. 

It is always advisable to get the instructions for the same task from multiple people as same task can be performed differently by different people. In this way, you might get to know the shortcuts to perform the task.

Focus Groups: 
In this technique, a presentation is given on a particular topic to a group of people and post session they provide feedback to facilitator. This approach is based on group interviewing techniques in which group of people consists of all the stakeholders which are part of that process. 

Note: One should have properly defined topic and must have ability to keep the group focused. 

This technique might not be successful, if participants are located in different-different geographic locations. This technique may also fail, if the group of people participating in defined session doesn’t belong to that activity or doesn’t have much knowledge about the activity is performed.

That's all I have to share. Hope you enjoyed reading.

Monday, December 11, 2017

What and How of Requirement Gathering - Part 2

In continuation to my previous post, I’ll add some more techniques which are popularly used for information gathering.

Interviewing: While on one hand shadowing provides an effective means to discover what is currently being done in the business, but on the other hand it does not provide all the necessary information. Another flaw of shadowing would be, it is not suitable for getting information about long-term activities that extent weeks or months along with the processes that requires very less or no human intervention. Hence, we can look for other techniques like interviewing.

Interviewing someone is the one-on-one meeting between project team and the user.
Here quality of information totally depends upon interviewee and the interviewer. The interviewer can ask a wide range of questions as compared to shadowing mechanism. These questions can be from basic information to difficulties to limitations of the system. During the interview process, the interviewee can give some ideas to improvise the solution, but one should avoid assuming that those ideas are the correct solution.

The success of this technique totally depends on how the questions are structured or framed? The interviewer should be in a position to get more and more questions out of the answers given by the interviewee. One should always avoid asking misleading questions.
Few of the standard questions that can be asked during this process can be:
  • Is there any documentation available to help you in performing your job?
  • Is there any third party, that affect your work – i.e. support specialists, external suppliers/system, etc.?
  • Does any business policy hinder you in performing your job?
  • Do you need any assistance or help when you work remotely? If yes, then what?

Surveys: Surveys are the other means of gathering information. It consists of a collection of questions. A very good example of surveys is the customer feedback form.
One of the best parts of surveys is that, they keep the identity of the user hidden as they can be given anonymously, which in turn promoted confidentiality.  Such kind of information gathering technique is useful when one is more concerned about the responses rather than which individual has responded.
The most difficult part of surveys is its preparation. Everyone cannot come up with surveys as is requires a trained professional for choosing questions and then analyzing the responses.
Note: Surveys can be affected by user attitude or mood :(
Few are the common examples of information that can be collected via surveys:
  • Organizational structure
  • Policies
  • Training material and instructor feedback
  • Customer satisfaction surveys, etc.
Hope you got an idea about interviewing and surveying technique and when to use what. In my next blog post, I’ll continue to write about few more techniques.
Till then keep reading.

ReferencesMSDNCDEBH Project Management

Saturday, December 9, 2017

What and How of Requirement Gathering - Part 1

This time rather than writing on some technology or solution, I thought to write on one of the important phases of SDLC which is nothing but requirement gathering. To be more specific, this article will be more about collecting information about business requirement. What are the various sources to get and understand most of the portions of any business requirement.

It’ very important to understand that, information can be gathered from many standpoints. It can be from Business front, Application front, Operations front, Technology front, and may be many more. So, while gathering information, one should have a clear vision on what kind of information they are looking for. Let’s have a look at few of the well-known categories:

  • Business – Goal of business, Service offerings, Products, Financial Structure, etc.
  • Application – Productivity tools, interaction with business application system, Code Modules, etc.
  • Operations – Identify the information’s origin, information’s consumption, information’s ownership, Data warehousing, Data models, Data Management policies, etc.
  • Technology-Technical Services, i.e. Development Environment, Topologies, Network Services, Security, DBMS, Technical Specifications, Hardware, Software etc.

The next biggest question would be how to gather all this information?
If you will search on the internet about how to gather information, you will get lot many ways. But most of these tactics, more or less fall into 1 of the below categories:

Shadowing: In this technique one will observe a user performing the tasks in an actual world environment and ask the user any questions related to the task. It is basically like following the user as he or she performs daily tasks. In other words, more the questions, more the information.
Note: This technique is effective only for frequently performed activities or tasks because if the task is performed occasionally, then it would be difficult to shadow someone.
Some common questions which can be asked during shadowing can be:-
  • What decisions do users make when starting or completing a task?
  • What modifications must be made over time to make it easier to complete the task?
  • Which related tasks may affect the design of the solution?
  • Are there any performance criteria?
  • How many people does a user interact during a given task?
  • Are there any variations in the steps to complete the task?
  • How often does system or management interfere with their job?
  • What do users like/dislike about the system?
  • Characteristics and preferences of user.
  • Concepts and terminology used by users?
  • What trainings do users need?
  • How can training and support costs can be reduced?
  • Have users been through the training or they are self-taught?
  • What information about the user is not documented?
In essence, one has to observe and question both the management and the users. If there are any external stakeholders involved with the task then they should also be part of this observation.
Hope you got an overview of shadowing. There are a few more techniques, which can be considered in order to get the required information for starting any project. But being a completely theoretical write-up, I would like to end this blog here itself and will write about more techniques in my next blog.
Till then keep reading.

ReferencesMSDNCDEBH Project Management

Friday, December 1, 2017

Generating XML Root Node having colon - via Serialization

Recently I got a requirement to generate an XML on the fly with some defined schema. I know this requirement looks very simple in first sight, but actually it was not.

The XML which was to be generated was having a very specific format as it was supposed to be the input for some 3rd party tool. So, it means, it should be fully compliant with the prescribed XML node structure. Just have a look at the below schema:

<sl:RandomWindow xmlns:xsi="" xmlns:sl="" xsi:schemaLocation=" wwRandomWindow.xsd">
  <Title>First Window</Title>

Below are the classes I created for serialization purpose:

By using the XmlElement and XmlAttribute classes I was able to generate most of the required parts of the XML as shown below:

But the only thing which didn’t come up correctly was the root node which is expected to be in the form <sl:RandomWindow>.

So, in order to achieve root node with prefix and colon, I updated the code to:

    public class RandomWindow {…}

But alas! It gave me some strange results. It converted the prefix to hexadecimal as shown below:

Then I thought, instead of providing concrete prefix, let’s add namespace itself as shown below:
[XmlRoot("RandomWindow", Namespace = "")]
     public class RandomWindow { ... }

And my result was:

Which was bit closer to what I need, but not the exact one. Now the issue remaining was the extra prefix in front of each element :(.

In order to resolve this issue, I tried various options provided over various blogs, but no luck.

But after spending hours, I was able to crack it by hit and try. To hide namespaces at element node level, I provided the namespace value as empty as shown below:

And that did the trick for me. Finally, I was able to achieve the required format.

Although this issue was pretending to be very small, it ate up my so much time. So, thought to share it here. Hope it would be helpful for you too.

Happy troubleshooting !!!