Enforcing date pattern with DateTime.TryParseExact method

Hello friends,

Today we will go though a useful method of datetime class called TryParseExact.  You can use this method to specify the pattern in which you want user to enter the dates. There are two overloads as following.

TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTime)
TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTime)

Example: var isValid = DateTime.TryParseExact(Convert.ToString(value), “d MMM yyyy”, CultureInfo.CurrentCulture,
DateTimeStyles.None, out dateTime);

Here if user enter date like 1-Jan-2018, if will be true else false. Please note that 
MMM is case sensitive and mmm will not work.

Hope you found this post useful. Please share your comments.

Efficiently handling DBNull in C#

Hello friends,

Let’s go through about DBNull today.

DBNull represents a nonexistent value returned from the database. In a database, for example, a column in a row of a table might not contain any data whatsoever. That is, the column is considered to not exist at all instead of merely not having a value. A DBNull object represents the nonexistent column.

Ex

if (! DBNull.Value.Equals(row[fieldName]))
return (string) row[fieldName] + ” “;
else
return String.Empty;

Please do not confuse the notion of null in C# language with a DBNull object. In an object-oriented programming language, null means the absence of a reference to an object, whereas DBNull represents an uninitialized field or nonexistent database column.

Below is one way to abstract the DBNull implementation and return null to middle/front end of the Data layer.

SqlDataReader r = …;

String firstName = getString(r[COL_Firstname]);

private static string getString(object o)

{

if (o == DBNull.Value) return null;

return (string) o;

}

Hope you found this post useful. Please share your comments below.

Working with Factory Method Pattern

Hello friends,

Today we will go through another creational design pattern called Factory Method.

Before talking about its implementation let’s begin with defining it.

Factory method pattern is similar to simple Factory pattern with a few difference. Unlike Simple factory, here client doesn’t call the factory class instead it calls the factory interface. Factory interface further have concrete implementations where object creation is performed.

Now let’s talk about implementation.

How to use Factory Method Pattern

Let’s try to understand using simple example.

Assume the client wants to know the on-road price of various brands of cars and have an interface as in the following,

  • interface ICar
  • {
  •    string GetOnRoadPrice(string model);
  • }

And here goes the concrete implementation of the ICar.

  • class Maruti : ICar
  • {
  •     public string GetOnRoadPrice(string model)
  •     {
  •         //Logic to get OnRoadPrice based on model
  •         if (model == “Alto 800 VXI”)
  •         {
  •             return “3.4 Lakhs INR”;
  •         }
  •         else
  •         {
  •             return “Information not available!”;
  •         }
  •     }
  • }
  • class Hyundai : ICar
  • {
  •     public string GetOnRoadPrice(string model)
  •     {
  •         //Logic to get OnRoadPrice based on model
  •         if (model == “Grand i10 Magna 1.2 BSV”)
  •         {
  •             return “5.4 Lakhs INR”;
  •         }
  •         else
  •         {
  •             return “Information not available!”;
  •         }
  •     }
  • }

We need to create a Factory Interface now which will sit between client and business layers and it will provide the required factory object to the client.

  • interface ICarFactory
  • {
  •    ICar GetCar();
  • }

And here goes the concrete implementation of the ICarFactory.

  • class MarutiFactory : ICarFactory
  • {
  •     public ICar GetCar()
  •     {
  •         return new Maruti();
  •     }
  • }
  • class HyundaiFactory : ICarFactory
  • {
  •     public ICar GetCar()
  •     {
  •         return new Hyundai();
  •     }
  • }

Now let’s see, how client can use the setup we have created so far.

  • //Client side
  • Console.Title = “Factory method pattern demo”;
  • ICarFactory carFactory = null;
  • ICar car = null;
  • string model = null;
  • //Maruti
  • carFactory = new MarutiFactory();
  • car = carFactory.GetCar();
  • model = “Alto 800 VXI”;
  • Console.WriteLine(“On-road price for {0} car is: {1} “, model, car.GetOnRoadPrice(model));

Output

run

  • //Hyundai
  • carFactory = new HyundaiFactory();
  • car = carFactory.GetCar();
  • model = “Grand i10 Magna 1.2 BSV”;
  • Console.WriteLine(“On-road price for {0} car is: {1} “, model, car.GetOnRoadPrice(model));

Output

Output

And here goes the class diagram.

class diagram

As you can see in the above code that,

  • Client is getting the required factory object by just creating instance of respective car factory brand.
  • And then it gets the required car object by calling GetCar method of retrieved factory object.
  • Client gets the On-road price of the car by calling GetOnRoadPrice method and passing model name.

So just to summarize, using Factory Method pattern, we achieved the following,

  • Loose coupling between client and business layers.
  • Client is only aware about Factory Interface (ICarFactory) and not their concrete implementations.
  • Abstracted concreate classes (Maruti, Hyundai) from client.

Hope you have liked the article. Looking forward for your comments/suggestions.