mercredi 13 juillet 2016

c# How to implement the Factory Method Pattern for build in classes from DLL file

i got another problem...

I got a DLL file, which has like 100 build in classes... besides that, i got 100 files of the type .csv, in a directory...

each one of those files, represents a class in the DLL file

now, i go in a for loop through the dll file, getting all the possible classes... then i check each one of them and compare it to the file i currently use in the loop....

if i find it...then i make a dynamic variable of that type...and mapp the data from the file on the new variable...and then i place it all i a dictionary...

and it works...all fine...

here is a small example of what it does...

breaker.csv
Name|Alias|CustomID
name1|alias1|customid1
name2|alias2|customid2

and so on...

then, in my code i check for the breaker class in the DLL file... once i find it... i create a dynamic variable of that class (myObject) i mapp the Name, Alias and the CustomId on that variable...and place it into the CIM_Dictionary (which can accept the dynamic variable) and then i move on with the next file (.csv)

and now i need to implement the factory method pattern on it...but i have no clue how to do it...

its said that the factory method is used to give back the proper class itself...so it fits on my problem...bc i have a lot of classes...

can u help me?

here is the code

using GIS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CSVtoXMLConverterCP
{
class CIM_Model
{
    public Dictionary<string, List<dynamic>> CIM_dictyionary = new Dictionary<string, List<dynamic>>();
    public void createCIM(ObjectModel objModel)
    {
        string msgForLogger = "";
        Dictionary<string, ElementObjectModela> elemDictionary = new Dictionary<string,ElementObjectModela>();

        for (int i = 0; i < objModel.objList.Count(); i++)
        {
            elemDictionary = objModel.objList[i];

            foreach (KeyValuePair<string, ElementObjectModela> entry in elemDictionary)
            {
                string CIM_key = entry.Key;
                string sKey = Parser.getNameOf_GIS_Layer(entry.Key);
                ElementObjectModela elemOM = entry.Value;
                string tempString = System.Configuration.ConfigurationManager.AppSettings[sKey];
                string[] appConfigData_ofASingleClass = tempString.Split(',');

                //app.config data...
                //*****************************************************************************************************
                string nameOfDllCLass = appConfigData_ofASingleClass[0]; 
                string nameOfClass = appConfigData_ofASingleClass[1];
                string aliasOfClass = appConfigData_ofASingleClass[2];
                string customIdOfClass = appConfigData_ofASingleClass[3];

                //*****************************************************************************************************

                bool bName = true;
                bool bAlias = true;
                bool bCustomID = true;

                //from app.config file
                var myAssembly = Assembly.LoadFile(System.Configuration.ConfigurationManager.AppSettings["pathDll"]);

                foreach (Type t in myAssembly.GetTypes())//taking the list of types from the DLL file, which is added above
                {
                    var listOfDynamicObjects = new List<dynamic>();
                    if (t.Name.ToString().Equals(nameOfDllCLass))//if it finds the required type, go into the for loop
                    {                           
                        foreach (KeyValuePair<string, Dictionary<string, string>> data in elemOM.elemDictionary)//use the data to map the dynamic object
                        {
                            Dictionary<string, string> CIM_data = new Dictionary<string, string>();
                            CIM_data = data.Value;
                            dynamic myObject = Activator.CreateInstance(t);

                            foreach (KeyValuePair<string, string> par in CIM_data)
                            {
                                //Name
                                if (par.Key.Equals(nameOfClass) && bName)
                                {
                                    myObject.Name = par.Value;
                                    bName = false;
                                }//alias  >>  description
                                if (par.Key.Equals(aliasOfClass) && bAlias)
                                {
                                    myObject.Description = par.Value;
                                    bAlias = false;
                                }
                                if (par.Key.Equals(customIdOfClass) && bCustomID)
                                {//customID  >>  MRID
                                    myObject.MRID = par.Value;
                                    bCustomID = false;
                                }
                                if (!bName && !bAlias && !bCustomID)
                                {
                                    listOfDynamicObjects.Add(myObject);
                                    bName = true;
                                    bAlias = true;
                                    bCustomID = true;
                                }
                            }
                        }
                        msgForLogger = "The class from the file: " + CIM_key + ".csv is now being mapped to: " + nameOfDllCLass;
                        Program.call_Logger(msgForLogger);
                        CIM_dictyionary.Add(CIM_key, listOfDynamicObjects);
                        msgForLogger = "The class from the file: " + CIM_key + ".csv was successfuly mapped.";
                        Program.call_Logger(msgForLogger);
                    }
                }
            }
        }
    }


}

}

Aucun commentaire:

Enregistrer un commentaire