c# - 类中的组方法

标签 c# oop class inheritance

我有包含许多方法的 DataAccess 类,要访问我使用代码的方法之一

var dataAccess = new DataAccess();
var s = dataAccess.GetDailyStatistic(...);

我想将这些方法分组到非静态类中并像那样访问它们

var dataAccess = new DataAccess();
var s = dataAccess.Statistic.GetDaily(...);

实现此目标的最佳方法是什么?嵌套类?

DataAccess 类结构。

public class DataAccess : DataAccessor
{
    public int AddUser(string orderId, string email, string userPassword, string firstName, string lastName,
                               int membershipId, string store, string country)
    public void DeleteBlockedUser(string email)
    public void DeleteUserById(int userId)
    public MembershipTypes GetMembershipType(int membershipId)
    public MembershipTypes GetUserMembershipType(int userId)
    public int? GetUserId(string email)
    public void UpdateUserExpirationDate(string orderId)
    public void UpdateUserCredits(int userId, int membershipId)
    public List<UserEntity> LoadUser(string email, string password)
    public string GetUserIdFromAuthList(string url)
    public UserEntity LoadUserById(int id)
    public UserEntity LoadUserByOrderId(string orderId)
    public void UpdateUser(int id, string email, string password, string firstName, string lastName)
    public void UpdateStatistic(string id, string url, string agent, string ip, string pageTitle)
    public List<StatisticEntity> GetStatistic(int userId, DateTime from)
    public void GetDailyStatistic(int userId, int days, string url, out string[] arrLabels, out int[] arrValues, out int maxValue)
    public void GetWeeklyStatistic(int userId, string url, DateTime from, out string[] arrLabels, out int[] arrValues, out int maxValue)
    ...skip...
}

最佳答案

为什么不使 DataAccess 类抽象,并让您的子类继承它?我假设您使用某种数据库连接在 DataAccess 中拥有所有方法,如果您从它继承,您仍然可以在子级之间共享该连接。你不会再有那个单一的构造函数了,但是,你可以做类似的事情

var statistic= new Statistic();
var s = statistic.GetDaily(...);

如果您真的喜欢 DataAccess 这个名字,请将这些子类放在命名空间 DataAccess 中,这样它看起来就像。

var statistic= new DataAccess.Statistic();
var s = statistic.GetDaily(...);

关于c# - 类中的组方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6858525/

相关文章:

c# - 在一个命名空间下的多个子文件夹中重新组合 Razor 组件 - Blazor

c#反射改变属性的get方法

c# - 有什么方法可以让 .NET 代码在电子邮件到达时运行和处理它们?

c# - 你需要 ref 或 out 参数吗?

Python dict 是本地的,而不是引用

c# - WPF。是否可以执行椭圆 "rectangle bounds"hittest?

matlab - MATLAB 中的嵌套类

c# - 无法针对对象断言

java - 创建对象时,左侧类表示什么,右侧类表示什么?

C++/阿杜伊诺 : dynamic int array