Sunday, April 19, 2015

Structure and Operator overloading in C#

Recently I have been playing with structure and It’s fun. In this blog post we are going to learn how we can use operator overloading with structure in C#.  You can find my blog post that I have already written about structure  at following location.

Structure in C#
Structure with constructor in C#
Structure with Interface in C#

So as usual I've created a console application to demonstrate operator overloading in C#. Here I have created a structure with equals operator overloading and following is a code for that.
Share:

Structure with Interface in C#

Recently I have been playing with Structure and it is fun. I have written couple of blog post about structure. Following are links for that.

Structure in C#
Structure with constructor in C#

In this blog post we are going to learn how we can use structure with interface. So like earlier example I’ve created a console application. Now it’s time to write interface which we can implement with structure. Following is a code for that.
using System;

namespace StructureWithMethodAndInterface
{
    interface IPrintable
    {
        void Print();
    }
}
Here you can see that I have simple interface IPrintable which has simple print method. Now let’s create a structure which implement this interface. Following is a code for that.
Share:
Friday, April 17, 2015

Structure with constructor in C#

Before some time , I have written basic introduction post about Structure in C#. In this blog post we are going to see how we can have constructor with Structure. As we all know structure is a value type so it stored on stack. So let’s create a structure like follow with constructor.
public struct Customer
{
    #region Fields

    public int CustomerId;
    public string FirstName;
    public string LastName;

    #endregion 

    #region Constructor

    public Customer(int customerId, string firstName, string lastName)
    {
        CustomerId = customerId;
        FirstName = firstName;
        LastName = lastName;
    }

    #endregion
}
Here you can see I have created Stucture with parameter. As we all know structure is a value type so when initialize the it at that time we need to give values to its elements so that’s why default constructor without parameter is not supported in C#.

Share:
Wednesday, April 15, 2015

Structure in c#

In this blog post we are going to learn about Structure Data structure in C#. In C# Structure is value type which stores on Stack. It is a composite value type that contain other types. You can also create object of structure like class. In C# structure can  contains fields, properties, constants, constructors, properties, Indexers, operators and even other structure types.

So enough theory. Let’s create a sample console application to learn about structure.

structure-in-csharp

Share:
Tuesday, April 7, 2015

Entity Framework Code First and MySql on Azure

We have used Entity framework with SQL Servers mostly. In this blog post we are going to see how we can use Entity framework with MySql on Azure.

How to create MySql database on Azure:

On azure MySql is not maintained by Microsoft itself. It is provided their partner cleardb. So You need to search at Azure Market Place and then add it like below.

market-place-azure-my-sql-database

Share:
Sunday, April 5, 2015

Best coding standards checklist for C# and ASP.NET

Before some time I have written a blog post about coding standard Why coding standards are important?. It was appreciated by lots of people and few people asked me whether do you have coding standard checklist or not? So Today, In this blog post I’m going to shared few link for coding standard checklist which I follow.

Aviva C# coding guidelines:

An awesome guideline for the C# 3.0,4.0 and 5.0. It’s very good and open sources. You can find that following link on code plex.

https://csharpguidelines.codeplex.com/

Do Factory do and don’ts for C#:

A great list of do and don’ts of C# coding standards that all we should follow.

http://www.dofactory.com/reference/csharp-coding-standards

Encodo C# Handbook:

It’s having so many coding standard and most of relevant to naming conventions and formatting. You can download that via following link.

http://code.msdn.microsoft.com/encodocsharphandbook/Release/ProjectReleases.aspx?ReleaseId=3352

Microsoft All one code framework:

I have been using this for a while. It’s an awesome library which display best practices how we can write code.

http://1code.codeplex.com/

There are lots others are also available but I think it’s if you follow and practice above mentioned resources your code will be perfect.
Share:

Entity Framework Internals: Connection Resiliency

When you use cloud services to deploy your application and database it might be possible that transient connection will be a problem between database server and web servers. When you use on premise servers that uses the database server and web server on same data centre. When you use cloud there will be huge infrastructures and you never know where it is deployed even if it deployed on same data centre there will more connection like network load balancers etc. When you use cloud services that will be shared by lots of users which means its responsiveness can be affected by them. And your access to the database might be subject to throttling. Throttling means the database service throws exceptions when you try to access it more frequently than is allowed in your Service Level Agreement (SLA).

So in cloud service there will be transient problems which will be resolved in short period of time. So when you got such kind of errors then you can wait for some time and then you have retry. For that kind of operation Entity Framework provides connection resiliency feature.

The connection resiliency features must be configured for proper database services. It has to know which exceptions are likely to be transient problem and which exceptions are caused by our code. It has to wait for an appropriate amount of time between retries of failed operation. Also it has to try number of times before giving up.

Share:
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:

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