Wcf 如何正确实施具有两个接口的策略模式?
我创建了一个服务数据访问层,其中有多个数据库,数据需要来自这些数据库 我在一个数据库中做得很好,我在其中定义了包含成员详细信息的Wcf 如何正确实施具有两个接口的策略模式?,wcf,asp.net-4.0,strategy-pattern,Wcf,Asp.net 4.0,Strategy Pattern,我创建了一个服务数据访问层,其中有多个数据库,数据需要来自这些数据库 我在一个数据库中做得很好,我在其中定义了包含成员详细信息的memberRepository。但是,现在我必须获取存储在另一个数据库中的与会话相关的详细信息 运营合同: IMemberServices包含GetLoggedInBuddies(int profileID) ISessionServices包含GetProfileIDFromSessionID(字符串sessionID) 我的服务级别: public class
memberRepository
。但是,现在我必须获取存储在另一个数据库中的与会话相关的详细信息
运营合同:
包含IMemberServices
GetLoggedInBuddies(int profileID)代码>
包含ISessionServices
GetProfileIDFromSessionID(字符串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);
}
我只需要一个人帮我把它组装起来
谢谢。我不确定您的问题,但您可以使用不带参数的ctor,并为每次回购使用参数
public MemberService()
{
this.memberRepository = new MemberRepository();
this.sessionRepository = new SessionRepository();
}
我创建了一个中央存储库,它接受我要连接到的数据库的连接字符串的名称
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;
}
}
.
.
.
这是答案还是问题?如果它是答案,请标记它。
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")) { ...