Search code examples
wcfasp.net-4.0strategy-pattern

How to properly implement the Strategy Pattern with two interfaces?


I have created a service data access layer where there are multiple databases where data needs to come from.

I was doing fine with one database where I defined the memberRepository that contained member details. However, now I have to get session-related details that are stored in another database.

OprationContracts:

  • IMemberServices contains GetLoggedInBuddies(int profileID);
  • ISessionServices contains GetProfileIDFromSessionID(string sessionID);

My service class:

public class MemberService : IMemberService, ISessionServices
{
    #region Strategy pattern configuration

    //
    // Member repo
    // 
    private MemberRepository memberRepository;
    public MemberService()
        : this(new MemberRepository())
    { }
    public MemberService(MemberRepository memberRepository)
    {
        this.memberRepository = memberRepository;
    }


    //
    // Session repo
    //
    private SessionRepository sessionRepository;
    public MemberService() : this(new SessionRepository()){}
    public MemberService(SessionRepository sessionRepository)
    {
        this.sessionRepository = sessionRepository;
    }

    #endregion



    /// <summary>
    /// Session-related details are maintained in the Secondary database
    /// </summary>
    /// <param name="sessionID"></param>
    /// <returns></returns>
    public int GetProfileIDFromSessionID(string sessionID)
    {
        int sessionProfileID = sessionRepository.GetProfileDetailsFromSessionID(sessionRepository);

        return sessionProfileID;
    }

    /// <summary>
    /// Try profileID = 1150526
    /// </summary>
    /// <param name="profileID"></param>
    public void  GetLoggedInBuddies(int profileID)
    {
        memberRepository.GetLoggedInBuddies(profileID);
        //return memberRepository.GetLoggedInBuddies(profileID);
    }

The issue is that in the // Session Repo section, as I already have a constructor defined. I get that.

So basically in each method I want to do something like

MemberService useSessionRepo = new MemberService(SessionRepository);
useSessionRepo.GetProfileDetailsFromSessionID(...);

MemberService useMemberRepo = new MemberService(MemberRepository);
useMemberRepo.GetLoggedInBuddies(...);

Just need a hand putting this together.

Thanks.


Solution

  • I created a central repository that accepts the name of the connection string of the database I want to connect to.

    public abstract class DatabaseRepository : BaseRepository
    {
        static IDbConnection connection;
    
        /// <summary>
        /// Handles db connectivity as Dapper assumes an existing connection for all functions
        /// Since this app uses three databases, pass in the connection string for the required db.
        /// </summary>
        /// <returns></returns>
        protected static IDbConnection OpenConnection(string connectionStringName)
        {
            try
            {
                connection = new SqlConnection(WebConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString);
                //connection = SqlMapperUtil.GetOpenConnection(connectionStringName);       // if we want to use the Dapper utility methods
                connection.Open();
                return connection;
            }
            catch (Exception ex)
            {
                ErrorLogging.Instance.Fatal(ex);        // uses singleton for logging
                return null;
            }
        }
    .
    .
    .
    

    Then in my service library, I make the connection to the appropriate db and perform whatever queries I need:

    using (IDbConnection connection = OpenConnection("FirstDBConnectionString")) { ...