Search code examples
c#design-patternssingletonfactory-pattern

how to use singleton and/or factory patterns in business logic layer


I understand the purpose of singleton and factory patterns, however I have doubts implementing it. My problem is that whenever I need to call a method from BLL, then I have create an instance of BLL class (please see the end of the code below). So, I am not sure if this is a proper singleton pattern implementation. I will try to briefly explain the layers that I am using for years in my application:

DataAccessLayer

public sealed class DataAccess
{
    private static readonly string path = "SqlServerDAL";
    public static IDAL.IPost CreatePostInstance()
    {
        string className = "SqlServerDal.Post"
        return(IDAL.IPost)Assembly.Load(path).CreateInstance(className))
    }
}

InterfaceDataAccessLayer

public interface IPost
{
    IList<PostInfo> GetPostsOfUser(Guid userId);
}

SQLServerDataAccessLayer

public class Post: IPost
{
    public virtual IList<PostInfo> GetPostsOfUser(Guid userId)
    {
        //Sql code to retrieve the data and create the IList<PostInfo>
    }
}

Model Class

public class PostInfo
{  }

Business Logic Layer

public class PostBLL
{
     private static readonly IPost dal = DALFactory.DataAcess.CreatePostInstance();
     public IList<PostInfo> GetPostsOfUser(Guid userId)
     { 
         return dal.GetPostsOfUser(userId);
     }
}

And, I call the BLL class like this from the aspx pages:

PostBLL managePost = new PostBLL();
managePost.GetPostsOfUser(userId);

Is this structure and pattern use appropriate? I have been using this for years but not sure if it is effective and correct.


Solution

  • Referring to link below:
    http://www.dofactory.com/net/factory-method-design-pattern

    I think you are correct.
    Difference is you did not apply abstract class, and you are using interface (WCF-like) pattern into the mixture.

    Thank you,