开发者

C# static class and data members question

开发者 https://www.devze.com 2023-02-18 04:36 出处:网络
I am not sure how to implement what I have in mind using C# .Net 3.5. I have a static class called Common which contains common methods. One of the method is PrepareReportParameters. This method accep

I am not sure how to implement what I have in mind using C# .Net 3.5. I have a static class called Common which contains common methods. One of the method is PrepareReportParameters. This method accepts a string ReportParams and parse it to get the parameter values. I load this ReportParams string into a Dictionary . And then verify whether the required elements exist. I check that like:

if (ReportParamList.ContainsKey("PAccount"))
{
    ReportParamList.TryGetValue("PAccount", out PrimaryAccount);
}

where PrimaryAccount is a static variable in my Common class. And I can access this elsewhere as Common.PrimaryAccount.

Though, this approcah of accessing the report parameters will work but I want PrimaryAccount to be accessed as Common.ReportParameters.PrimaryAccount. Here is the problem, I don't know what type ReportParameters should be and how can I开发者_开发百科 have all the report parameters added to this type? How should I define ReportParameters? Does it sound feasible or it doesn't make any sense. Please H E L P!


It sounds like you're basically used to using global variables to pass around state. That's generally a really bad idea.

Why doesn't your method just return the primary account value? That can then be passed to other things which need it.

If you find yourself with a lot of static members - and in particular if other classes are fetching mutable static variables - consider whether there's a more OO design you could apply. It'll be easier to understand, easier to test, and easier to maintain.

EDIT: Okay, so currently you have:

public static class Common
{
    public static int PrimaryAccount;
    // other static fields

    public static void PrepareReportParameters(string reportParameters)
    {
        // Code to set the fields
    }
}

Instead of that, use a normal class:

public class ReportParameters
{
    public int PrimaryAccount { get; private set; }
    // Other properties

    private ReportParameters(int primaryAccount, ....)
    {
        this.PrimaryAccount = primaryAccount;
    }

    // Could use a constructor instead, but I prefer methods when they're going to
    // do work
    public static ReportParameters Parse(string report)
    {
        // Parse the parameter, save values into local variables, then
        return new ReportParameters(primaryAccount, ...);
    }
}

Then call this from the rest of your code, and pass the ReportParameters reference to anything that needs it.


You could create a class called ReportParameters with the relevant strongly-typed properties, and give Common a static instance of it?


I'm not sure this is the best design. Theres a certain amount of 'code smell' to having Common.PrimaryAccount only to be allowed to be accessed after PrepareReportParameters is called. Maybe you'd consider an instance class, passing in the parameters in the constructor?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号