如何用两个接口正确实现策略模式?

时间:2012-04-09 16:23:47

标签: wcf asp.net-4.0 strategy-pattern

我创建了一个服务数据访问层,其中有多个数据库需要数据来源。

我在一个数据库中做得很好,我在其中定义了包含成员详细信息的memberRepository。但是,现在我必须获取存储在另一个数据库中的与会话相关的详细信息。

OprationContracts:

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

我的服务类:

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);
    }

问题在于// Session Repo部分,因为我已经定义了构造函数。我知道了。

所以基本上在每种方法中我都想做一些像

这样的事情
MemberService useSessionRepo = new MemberService(SessionRepository);
useSessionRepo.GetProfileDetailsFromSessionID(...);

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

只需要把它放在一起。

感谢。

2 个答案:

答案 0 :(得分:0)

我不确定你的问题,但是你可以使用没有param的ctor,以及每个repo的param。

public MemberService()
{
   this.memberRepository = new MemberRepository();
   this.sessionRepository = new SessionRepository();
}

答案 1 :(得分:0)

我创建了一个中央存储库,它接受我要连接的数据库的连接字符串的名称。

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;
        }
    }
.
.
.

然后在我的服务库中,我建立了与相应数据库的连接并执行我需要的任何查询:

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