外观设计模式和子类化

发布于 2024-08-26 07:20:32 字数 886 浏览 15 评论 0原文

我正在为 C# 程序使用外观设计模式。程序基本上 看起来像这样...

 public class Api
    {
        #region Constants

        private const int version = 1;

        #endregion


        #region Private Data

        private XProfile _profile;
        private XMembership _membership;
        private XRoles _role;

        #endregion Private Data


        public Api()
        {
            _membership =  new XMembership(); 
            _profile = new XProfile();
            _role = new XRoles();

        }

       public int GetUserId(string name)
       {
            return _membership.GetIdByName(name);
       }


}

现在,我想将我的方法分为三类:角色、配置文件和成员。 这对开发人员来说会更容易,因为 Profile 和 Membership 都公开了许多看起来相似的方法(还有一些按角色划分的方法)。例如,获取用户的 ID 看起来像:

int _id = Namespace.Api.Member.GetUserId("Henry222");

有人可以“说明”在这种情况下子类化应该如何工作以实现我正在寻找的效果吗?

提前致谢。

I am using a facade design pattern for a C# program. The program basically
looks like this...

 public class Api
    {
        #region Constants

        private const int version = 1;

        #endregion


        #region Private Data

        private XProfile _profile;
        private XMembership _membership;
        private XRoles _role;

        #endregion Private Data


        public Api()
        {
            _membership =  new XMembership(); 
            _profile = new XProfile();
            _role = new XRoles();

        }

       public int GetUserId(string name)
       {
            return _membership.GetIdByName(name);
       }


}

Now, as I would like subclass my methods into three categories: Role, Profile, and Member.
This will be easier on the developers eye because both Profile and Membership expose a lot of methods that look similar (and a few by Role). For example, getting a user's ID would look like:

int _id = Namespace.Api.Member.GetUserId("Henry222");

Can somebody "illustrate" how subclassing should work in this case to achieve the effect I am looking for?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很酷又爱笑 2024-09-02 07:20:33

我认为从你的问题的上下文来看,你可能指的是“内部阶级”。您可以尝试类似以下的操作...

public class Api
{
    public class MemberApi
    {
      private readonly Api _parent;

      internal MemberApi(Api parent) { _parent = parent; }

      public int GetUserId(string name)
      {
         return _parent._membership.GetIdByName(name);
      }
    }

    #region Constants

    private const int version = 1;

    #endregion


    #region Private Data

    private XProfile _profile;
    private XMembership _membership;
    private XRoles _role;

    public MemberApi { get; private set; }

    #endregion Private Data

    public Api()
    {
        _membership =  new XMembership(); 
        _profile = new XProfile();
        _role = new XRoles();

        Member = new MemberApi(this);
    }
}

I would argue that you might mean "inner class" from the context of your question. You might try something like the following...

public class Api
{
    public class MemberApi
    {
      private readonly Api _parent;

      internal MemberApi(Api parent) { _parent = parent; }

      public int GetUserId(string name)
      {
         return _parent._membership.GetIdByName(name);
      }
    }

    #region Constants

    private const int version = 1;

    #endregion


    #region Private Data

    private XProfile _profile;
    private XMembership _membership;
    private XRoles _role;

    public MemberApi { get; private set; }

    #endregion Private Data

    public Api()
    {
        _membership =  new XMembership(); 
        _profile = new XProfile();
        _role = new XRoles();

        Member = new MemberApi(this);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文