locked
Best Usage of Singleton and Factory pattern with a CRUD application RRS feed

  • Question

  • Hello experts

    I need to know where I shall Implement Singleton and Factory Method Pattern/Abstract Factory Pattern in a Crud application. Can anybody please suggest me?

    Can anybody please send me some sample code?

    Thanks in advance

    Md. Mojammel Haque

    mojamcpds@gmail.com

    Md. Mojammel Haque Senior Software Engineer Skype: mojamcpds1 Gmail:mojamcpds@gmail.com Cell: +8801817045882

    Monday, February 4, 2013 9:25 AM

Answers

  • This is a legacy approach a little, but still being used,

    You can get db connection object by Singleton Pattern or Factory Pattern, like

    Singleton:

    SQLConnectin.FetchConnectionInstance()

    or

    Factory:

    var con = new ConnectionFactory (ConnectionType.SQLServer)

    con.FectchConnection()

    Practically, Other crud options has to be done in wrapper or layer like repository (pattern).


    - Prashant http://prashantsblogs.wordpress.com http://prashantsblogs-algos.blogspot.in

    • Proposed as answer by Prashant.Soni Monday, February 4, 2013 12:14 PM
    • Marked as answer by Jason Dot Wang Tuesday, February 12, 2013 5:53 AM
    Monday, February 4, 2013 12:13 PM
  • Well, Okay

    Following are few snippets I have create on the fly, hope that can helps you,

    Case 1: Is to demonstrate Singleton pattern to create object

    Case 2: Is to demonstrate Factory to create object.

    Case 3: Is to demonstrate CRUD operation by Repository pattern.

    Side note: There are better ways to do this , But since you are particular about what you want to know, have posted this snippets.

    //CASE 1:

    public sealed class SingletonConnection { private static volatile SQLConnection instance; private static object syncObj = new Object(); private SingletonConnection() { } public static SQLConnection ConnectionInstance { get { if (instance == null) { lock (syncObj) { if (instance == null) instance = new SQLConnection(); } } return instance; } } } //Usage: (new SingletonConnection().ConnectionInstance) //http://msdn.microsoft.com/en-us/library/ff650316.aspx

    //CASE 2:

    public enum ConnectionProviderType { SQLConnection, OracleConnection, MySQLConnection } public interface IMyConnection { } public class CustomSQLConnection : SQLConenction, IMyConnection { } public class CustomMySQLConnection : MySQLConnection, IMyConnection { } public class CustomOracleConnection : OracleConnection, IMyConnection { } public class ConnectionFactory { public IMyConnection CreateConnection(ConnectionProviderType providerType) { switch (providerType) { case ConnectionProviderType.SQLConnection: return (new CustomSQLConnection()); case ConnectionProviderType.MySQLConnection: return (new CustomMySQLConnection()); case ConnectionProviderType.OracleConnection: return (new CustomOracleConnection()); default: return null; } } } //Usage: (new ConnectionFactory().CreateConnection(ConnectionProviderType.SQLConnection) //http://msdn.microsoft.com/en-us/library/ee817667.aspx public class Person {public int id;} public interface IRepository: IDisposable { IEnumerable<Person> GetAllPersons(); Person GetPersonById(int personId); void AddPerson(Person person); void RemovePerson(Person person); void UpdatePerson(Person person); void Save(); }

    //CASE 3:

    public class PersonRepository : IRepository { public IEnumerable<Person> GetAllPersons() { throw new NotImplementedException(); } public Person GetPersonById(int personId) { throw new NotImplementedException(); } public void AddPerson(Person person) { throw new NotImplementedException(); } public void RemovePerson(Person person) { throw new NotImplementedException(); } public void UpdatePerson(Person person) { throw new NotImplementedException(); } public void Save() { throw new NotImplementedException(); } public void Dispose() { throw new NotImplementedException(); } } //http://msdn.microsoft.com/en-us/library/ff649690.aspx




    - Prashant http://prashantsblogs.wordpress.com http://prashantsblogs-algos.blogspot.in


    • Edited by Prashant.Soni Tuesday, February 5, 2013 10:51 AM Added more explanations.
    • Proposed as answer by Prashant.Soni Tuesday, February 5, 2013 10:51 AM
    • Marked as answer by Jason Dot Wang Tuesday, February 12, 2013 5:53 AM
    Tuesday, February 5, 2013 10:48 AM

All replies

  • This is a legacy approach a little, but still being used,

    You can get db connection object by Singleton Pattern or Factory Pattern, like

    Singleton:

    SQLConnectin.FetchConnectionInstance()

    or

    Factory:

    var con = new ConnectionFactory (ConnectionType.SQLServer)

    con.FectchConnection()

    Practically, Other crud options has to be done in wrapper or layer like repository (pattern).


    - Prashant http://prashantsblogs.wordpress.com http://prashantsblogs-algos.blogspot.in

    • Proposed as answer by Prashant.Soni Monday, February 4, 2013 12:14 PM
    • Marked as answer by Jason Dot Wang Tuesday, February 12, 2013 5:53 AM
    Monday, February 4, 2013 12:13 PM
  • @Prashant.Soni : Can you please send me some sample code? It will be more helpful for me.

    Thanks in Advance

    Md. Mojammel Haque

    mojamcpds007@hotmail.com

    mojamcpds@gmail.com


    Md. Mojammel Haque Senior Software Engineer Skype: mojamcpds1 Gmail:mojamcpds@gmail.com Cell: +8801817045882

    Tuesday, February 5, 2013 9:35 AM
  • Well, Okay

    Following are few snippets I have create on the fly, hope that can helps you,

    Case 1: Is to demonstrate Singleton pattern to create object

    Case 2: Is to demonstrate Factory to create object.

    Case 3: Is to demonstrate CRUD operation by Repository pattern.

    Side note: There are better ways to do this , But since you are particular about what you want to know, have posted this snippets.

    //CASE 1:

    public sealed class SingletonConnection { private static volatile SQLConnection instance; private static object syncObj = new Object(); private SingletonConnection() { } public static SQLConnection ConnectionInstance { get { if (instance == null) { lock (syncObj) { if (instance == null) instance = new SQLConnection(); } } return instance; } } } //Usage: (new SingletonConnection().ConnectionInstance) //http://msdn.microsoft.com/en-us/library/ff650316.aspx

    //CASE 2:

    public enum ConnectionProviderType { SQLConnection, OracleConnection, MySQLConnection } public interface IMyConnection { } public class CustomSQLConnection : SQLConenction, IMyConnection { } public class CustomMySQLConnection : MySQLConnection, IMyConnection { } public class CustomOracleConnection : OracleConnection, IMyConnection { } public class ConnectionFactory { public IMyConnection CreateConnection(ConnectionProviderType providerType) { switch (providerType) { case ConnectionProviderType.SQLConnection: return (new CustomSQLConnection()); case ConnectionProviderType.MySQLConnection: return (new CustomMySQLConnection()); case ConnectionProviderType.OracleConnection: return (new CustomOracleConnection()); default: return null; } } } //Usage: (new ConnectionFactory().CreateConnection(ConnectionProviderType.SQLConnection) //http://msdn.microsoft.com/en-us/library/ee817667.aspx public class Person {public int id;} public interface IRepository: IDisposable { IEnumerable<Person> GetAllPersons(); Person GetPersonById(int personId); void AddPerson(Person person); void RemovePerson(Person person); void UpdatePerson(Person person); void Save(); }

    //CASE 3:

    public class PersonRepository : IRepository { public IEnumerable<Person> GetAllPersons() { throw new NotImplementedException(); } public Person GetPersonById(int personId) { throw new NotImplementedException(); } public void AddPerson(Person person) { throw new NotImplementedException(); } public void RemovePerson(Person person) { throw new NotImplementedException(); } public void UpdatePerson(Person person) { throw new NotImplementedException(); } public void Save() { throw new NotImplementedException(); } public void Dispose() { throw new NotImplementedException(); } } //http://msdn.microsoft.com/en-us/library/ff649690.aspx




    - Prashant http://prashantsblogs.wordpress.com http://prashantsblogs-algos.blogspot.in


    • Edited by Prashant.Soni Tuesday, February 5, 2013 10:51 AM Added more explanations.
    • Proposed as answer by Prashant.Soni Tuesday, February 5, 2013 10:51 AM
    • Marked as answer by Jason Dot Wang Tuesday, February 12, 2013 5:53 AM
    Tuesday, February 5, 2013 10:48 AM
  • nice post
    Friday, April 4, 2014 2:50 PM