samedi 16 juin 2018

C# alternative for using enum as method parameter

I have the following method that downloads a set of item prices or license prices and stores it on the local HDD. The only difference between the two is the input list that dictates what items that should be queried for. The application is being deployed as a clickonce app so the storage of the file differs from debug to deployment build.

    private const string PRICEFILE = "Resources\\prices.xml";
    private const string RATEFILE = "Resources\\rates.xml";
    private const string INPUTFILE = "Resources\\ItemsList.txt";
    private const string INPUTFILELICENSE = "Resources\\Licenses.txt";
    private const string LICENSEFILE = "Resources\\Licenses.xml";

   public string getFromInventoryTableOnServer(InventoryTypeEnum type)
    {
        string _input = "";
        string _output = "";
        // Get items from items file in resources
        if (ApplicationDeployment.IsNetworkDeployed)
        {
            if (type.Equals(InventoryTypeEnum.Prices))
            {
                _input = ApplicationDeployment.CurrentDeployment.DataDirectory + @"\" + INPUTFILE;
                _output = ApplicationDeployment.CurrentDeployment.DataDirectory + @"\" + PRICEFILE;
            }
            else if (type.Equals(InventoryTypeEnum.Licences))
            {
                _input = ApplicationDeployment.CurrentDeployment.DataDirectory + @"\" + INPUTFILELICENSE;
                _output = ApplicationDeployment.CurrentDeployment.DataDirectory + @"\" + LICENSEFILE;
            }
        }
        else
        {
            if (type.Equals(InventoryTypeEnum.Prices))
            {
                _input = INPUTFILE;
                _output = PRICEFILE;
            }
            else if (type.Equals(InventoryTypeEnum.Licences))
            {
                _input = INPUTFILELICENSE;
                _output = LICENSEFILE;
            }
        }


        // Read file
        List<string> items = new List<string>();
        using (var sr = new StreamReader(_input))
        {


            while (!sr.EndOfStream)
            {
                items.Add(sr.ReadLine().Split(new[] { Environment.NewLine }, StringSplitOptions.None)[0]);
            }

        }

        // Connection to database and table
        List<InventTableDW> it;
        using (AXDataContext ax = new AXDataContext())
        {
            var table = ax.GetTable<InventTableDW>();

            // Query AX for item info, specially prices
            var query =
                from p in table
                where items.Contains(p.ItemID) && p.ItemGroupID == "100"
                orderby p.ItemID
                select p;

            try
            {
                it = query.ToList();
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }


        // Write to the price file
        try
        {
            using (var sw = new StreamWriter(_output))
            {
                {
                    var ser = new XmlSerializer(typeof(List<InventTableDW>));
                    ser.Serialize(sw, it);
                }
            }
        }
        catch (Exception e)
        {
            return e.Message;
        }
        return "";
    }

enum InventoryTypeEnum
{
    Prices,
    Licences
}

I'm not very proud of the current implementation as it looks a bit messy. Do you have any improvement suggestions?

Many thanks!

Aucun commentaire:

Enregistrer un commentaire