Git Ignore

My project contains C# files, but it also contains Meta files (e.g.: DBHelper.cs.meta). How can Git ignore the files with .meta extension?

Git can ignore files by adding ‘*.meta’ in the end of exclude file. Location: $GIT_DIR/info/exclude. You should use Notepad or similar to edit.

Source: http://git-scm.com/docs/gitignore

Singleton Pattern

Singleton Pattern is a design pattern that prevents to create various objects of the same class when it is only necessary one.

For example, imagine a scenario where you have to insert your credentials (username and password). Before you go to a window after you have logged in successfully, behind the login form, the application creates a database connection and validate your credentials.

The database connection has been created and you want insert a new record to table called ‘Orders’. Will the application create another database connection or use the previous one?

Without a singleton pattern, the application will create another database connection (so the application will have two connections). With singleton pattern, the application will use the database connection created before.

The singleton pattern requires:

  • a static variable (e.g.: private static DBHelper uniqueInstance)
  • a static method (e.g.: public static DBHelper GetInstance(){…})
  • a private constructor (e.g.: private DBHelper(){…})

Remember, it uses a keyword ‘static’ because you needn’t instantiate the class. Below there are two classes (written in C#) to help you understand better.

public class DBHelper
{

	private static string connectionString = 
		"Server=PC\\SQLEXPRESS;" +
		"Database=myDatabase;" +
		"Integrated Security=SSPI;" +
		"User Id=John;" +
		"Password=JohnDoe2015;";
	private static SqlConnection sqlConnection;
	private string query;
	private SqlDataAdapter sqlDataAdapter;
	private DataTable dataTable;
	private int rows;

	private static DBHelper uniqueInstance;

	private DBHelper ()
	{
		sqlConnection = new SqlConnection (connectionString);
	}

	public static DBHelper GetInstance ()
	{
		if (uniqueInstance == null)
			uniqueInstance = new DBHelper ();
		return uniqueInstance;
	}

	public void OpenConnection ()
	{
		if (!sqlConnection.State.Equals (ConnectionState.Open))
			sqlConnection.Open ();
	}

	public void CloseConnection ()
	{
		if (!sqlConnection.State.Equals (ConnectionState.Closed))
			sqlConnection.Close ();
	}

	public int ValidateUser (string username, string password)
	{
		query = "select * from utilizadores where username = '" + username + "' and password = '" + password + "' ";
		sqlDataAdapter = new SqlDataAdapter (query, sqlConnection);
		dataTable = new DataTable ();
		rows = sqlDataAdapter.Fill (dataTable);
		return rows;
	}
}
public class Login : MonoBehaviour
{
	public GameObject username;
	public GameObject password;

	private MD5 md5;
	private DBHelper mDBHelper;

	// Use this for initialization
	void Start ()
	{
		username = GameObject.Find ("userInputField");
		password = GameObject.Find ("passwordInputField");
		mDBHelper = DBHelper.GetInstance ();
		mDBHelper.OpenConnection();
		md5 = new MD5CryptoServiceProvider();
	}
	
	// Update is called once per frame
	void Update ()
	{
	}

	//void OnMouseDown()
	public void OnLogin ()
	{
		UnityEngine.UI.InputField user = username.GetComponent<InputField> ();
		UnityEngine.UI.InputField pass = password.GetComponent<InputField> ();
		string passwordEncrypted = md5Hash(pass.text);

		if (user.text.Length != 0 && pass.text.Length != 0) {
			if (mDBHelper.ValidateUser(user.text, passwordEncrypted) == 1) {
				Application.LoadLevel ("SceneIntro");
			}else{
 				Debug.Log("Invalid Login!");
			}
		} else {
			Debug.Log("Invalid Login!");
		}
	}

	private string md5Hash(string password){
		md5.ComputeHash (ASCIIEncoding.ASCII.GetBytes (password));

		byte[] result = md5.Hash;

		StringBuilder stringBuilder = new StringBuilder ();
		for (int i=0; i < result.Length; i++) {
			//change it into 2 hexadecimal digits for each byte
			stringBuilder.Append(result[i].ToString ("x2"));
		}
		return stringBuilder.ToString ();
	}
}

Note: this design pattern was the first I have ever implemented at my career. 😀