Thursday, April 2, 2015

Quick Tip–How to get time difference in C#

Recently, One of the reader of this asked me about, how we can get time difference between two dates or two times? So I thought it will be a good idea to write a blog post about that. So that other use can also get benefit from that.

For that I have created sample console application like below.
using System;

namespace TimeDifferenceCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime firstDate = DateTime.Parse("12:50");
            DateTime secondDate = DateTime.Parse("10:40");

            TimeSpan difference = TimeSpan.Parse(firstDate.Subtract(secondDate).ToString());

            Console.WriteLine(difference.Hours);
            Console.WriteLine(difference.Minutes);
        }
    }
}
In above example, If you see it carefully I have taken two dates with different time. Then I have used subtract method DateTime class to find different of both and then I parse it to TimeSpan class. After that I have printed hours and minutes for  via Console.Writeline method.

Now let’s run this application. You will see output as following.

time-difference-in-csharp

That’s it.Hope you like it.

You can find complete source code of this blog post at github on- https://github.com/dotnetjalps/TimeDifferenceInCSharp
Share:

Quick tip- How to change menu bar letter style in visual studio 2013 and 2015

When Microsoft has introduced, Visual Studio 2012 with Upper case menu, There were so many reactions from the developers. So with Visual Studio 2013 Microsoft has provided that settings to Turn of capital letters for menu. I have just came to know that accidentally. You can find that settings in Tools->Options-General settings.

menu-capital-settings-visual-studio-2013-15

Once you check it. Upper case will turn off like below.

upper-case-turn-off-visual-studio

That’s it. Hope you like it. Stay tuned for more!.
Share:
Wednesday, April 1, 2015

Entity Framework Internals: Private setters and private constructors

I have been heavily learning Domain Driven Design recently and In that there is a concept called “Anemic Domain Model” which we used to use in our project. In simple terms Anemic Domain Models are those classes that do not have any behaviour with them and most of created with properties. You can use them as container for your data from database. While Domain driven design is all about behaviours. So we need to make our models incorporate behaviours also which is called  “Rich Domain models”.

One of step to convert your Anemic Domain Models to Rich Domain Models is to create parameterised constructors. For example an Employee must have FirstName and LastName. So instead of doing validation at the insertion time on UI we should not allow Employee to be created without FirstName and Lastname. The only way to do this to make this properties setters private and assign value of those properties via parameters constructors like below.
public class Employee
{
        public Employee(string firstName, string lastName)
        {
            FirstName = firstName;
            LastName = lastName;
        }
            
        public int EmployeeId { get; set; }
        public string FirstName { get; private set; }
        public string LastName { get; private set; }
        public string Designation { get; set; }
}
Now, If you want to do DDD with any Object Relational Mappers there will be a problem as most of Object Relational Mappers create properties with public setters. Entity Framework is such a Object Relational mapper and I love to work with Entity Framework.

Share:
Thursday, March 26, 2015

Fluent Nhibernate : Create Table from Code(Class- CodeFirst)

Now days,  Convention over configured is more preferred and that’s why Fluent Nhibernate got more popularity Because with Fluent Nhibernate we don’t have to write and maintain mapping in xml. You can write classes to map your domain objects to your database tables. If you want to learn how we can do this with existing database and Fluent Nhibernate, I have also written a blog post about CRUD operation with Fluent Nhibernate and ASP.NET MVC . After writing this blog post I was getting a lots of emails about how we can create database from the Fluent Nhibernate as we can do same with Entity Framework Code First. So I thought it’s a good idea to write a blog post about it instead of writing individual emails.

How to create tables from class via Fluent Nhibernate:

To Demonstrate how we can create tables based mapping classes create we’re going to create a console application via adding new project like below.

Share:
Sunday, March 22, 2015

Entity Framework Internals: Enum Support

In .NET world, We all are using Enums in our code. It’s makes our code more readable then using hardcoded numbers of strings. Entity Framework 5.0 or higher version has support for Enums. In this blog post we are going to see how we can use Enums with entity framework.

We are going to create a console application to see how it’s works. I’m going to create a console application for the same.

entity-framework-enum-support-console-application

After creating a application I’ve added Entity framework via Nuget package.

Share:
Saturday, March 21, 2015

Entity Framework internals :IEnumerable and IQueryable

Now days, Entity framework is one the most used ORM in .NET world. Still I can see lots of people confused with IQueryable and IEnumerable. If they dont' know what is difference between them then they are going to make mistakes .When you first look at it both looks same. But there are quite differences and If you don’t understands internals of it then it will impact your query performance also.

IEnumerable vs IQueryable:

Here is the basic difference between IEnumerable and IQueryable. When you write queries with IEnumerable it executes queries on Server and load data in memory and then filter data on client side. This will be a problem when you are fetching large amount of data. While IQueryable executes queries on server side with all filters and then load all data into memory.

Example:

Sound complex!. Let’s write a simple example to understand it better. I am going to create a simple table in database called student table.

Share:
Friday, March 13, 2015

NSubstitute Introduction and Why I like it

Recently I am heavily learning Test Driven Development and Domain Driven design. As you know With TDD you need to have a mocking libraries. So after doing on research on internet they are three libraries available in Microsoft.NET development world which are quite mature.

  1. Rhino Mocks
  2. NSubstitute
  3. Moq
I have gone through all of above three and then After doing research and I choose NSubstitute.

Why I Choose NSubstitute:

Following are the reasons why I have chosen NSubstitute.

  1. It is super easy to use. I don’t have to write very complex lamda expressions to mock objects.
  2. It works really fine with all framework of Test Driven Development
  3. As it is super easy to use you need to write very less code to mock objects.
  4. With other Visual Studio plugins like Resharper, Telerik Justcode its works perfectly and they are able to generate code for the same.
  5. It’s got fluent syntax and very easy to understand.
  6. It’s perfect for some one who is not mature enough for Test Driven Development.
Share:
Thursday, February 5, 2015

Shared project in Visual Studio 2015

Shared project is new feature that was added in Visual Studio 2013 update 2 but in visual studio 2015 it is coming by default. In this post we are going to learn about Shared Project and how it is different from class library.

Shared project in Visual Studio 2015:

Shared project is a new feature that is introduced with Visual Studio 2013 update 2 and it’s comes by default with Visual Studio 2015. So with shared project you can share your common code with any number of applications. You can use that in another project via adding project reference. So let’s see how we can use Shared project in multiple application.

To understand how Shared project works, I have create a blank solution like following.

blank-solution-for-shared-project-visual-studio-2015

Share:
Wednesday, February 4, 2015

Why coding standards are important?

Yesterday, I had a discussion with friend about coding standard are important and why you need it?  So I thought it will be a good idea to write a blog post about it. Followings are some reasons why coding standard are so important.

  • Readability:  coding standard will increase overall code readability means if you follow you coding standard defined other people also know that what you have written. They don’t need to debug the code to know what function this code will perform.
  • Code Style: Every programmer have different style of writing code. So if we don’t have coding standard then Programmer ‘A’ can write variable in camel casing while other  will prefer to use Pascal case. So different modules have different coding styles.Which makes code hard to understand.
  • Code Reviews: Code review is an important part of any software development. If you don’t have a coding standard defined then it will take more time to do code review.
  • Error handling/Exception handling : The another benefits of coding standard is standardized way of handling errors. If you don’t have coding standard defined then each programmer will handle errors in unique way that could create a problem of identification of a bug  in software development .
  • Maintenance: Large enterprise software's will have  long life span. So if there is any enhancement or maintenance work is required with the help of coding standard any people can understand code easily and easily do the some changes.
That’s it. Hope you like it. Stay tune for more!
Share:

What is MongoDB and Why MongoDB?

Recently I have playing a lot with MongoDB and it’s  one of the favourite document database. In this blog post we are going to learn about MongoDB in details.

What is MongoDB?

Those who don’t know MongoDB here is what documentation on MongoDB says about it.

MongoDB is a document database that provides high performance, high availability, and easy scalability. Document Database. Documents (objects) map nicely to programming language data types. Embedded documents and arrays reduce need for joins.
A MongoDB is one of popular database. A standard deployment can hold many databases. A database holds a set of collections. It's like similar to tables in relational databases. A collection holds a set of documents just like rows in relational database tables. A document is a set of Key value pair. It’s stores data in such a way that there is no relation is required. It was created by company called 10gen.

MongoDB supports almost all the languages and there are lits of drivers are available for MongoDB and it open sourced at Github at following location.

Share:

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews