C#



Hi all,
after some months of studies about MEF, Unity and EntityFramework I decided to share some “pre-cooked solution” about what I learned.

The first sample provide the basic knowledge about modularity architecture. You can download the sample application (VS2012) here.

Modularity

As displayed in the picture you have three main actors:

  • Program: the Program knows just the Core library and it is totally unaware about Plugins components. This means that your Program does not have references on Plugins.
  • Core Application: the Core Application defines the Plugin Interface (IPaymentPlugin) and provides the PaymentService, a way for retrieving Plugins implementations. We will discover in an other post about why I choose to define the IPaymentService interface (when I will discuss about Unity).
  • Plugins: each plugin implements the Plugin Interface (IPaymentPlugin).

    The core of the application is implemented by GetPaymentPlugins a method of PaymentService of course. Here the code that is able to dynamically retrieving Plugins:

            public IList<IPaymentPlugin> GetPaymentPlugins()
            {
                List<IPaymentPlugin> plugins = new List<IPaymentPlugin>();
    
                List<Assembly> allAssemblies = new List<Assembly>();
                string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    
                foreach (string dll in Directory.GetFiles(path, "*.dll"))
                {
                    Assembly assembly = Assembly.LoadFile(dll);
                    foreach (Type ti in assembly.GetTypes().Where(x => x.GetInterfaces().Contains(typeof(IPaymentPlugin))))
                    {
                       plugins.Add((IPaymentPlugin)Activator.CreateInstance(ti));
                    }
                }
    
                return plugins;
            }
    
    Advertisements

  • If you are creating Windows Services you may run into this error:

    Error 1001. The specified service has been marked for deletion

    At first I used to restart my machine to get rid of it, but there is a (much) simpler way: close the Services Manager and you will never encounter this error!

    Marzio.


    Hi,
    In these days I needed to increase the value of a “frequency” attribute contained into a certain XML file. Unfortunately this attribute appears several times into several XML files, so I wrote a simple WPF application that does this job for me 😉

    Click here to download the application.
    Click here to download the sample XML File.

    The following picture show the Application. Once you digit the number you want to add to all frequency attributes you should press Ok button and after that an Open File Dialog appears allowing you to choose the XML file. After choosing the file, all frequency attributes will be updated according to the delta value and a backup of the original file will be stored as “yourXMLfile.back_0” 😉

    How does it works?
    The file is opened using the .Net XmlDocument , after that the the application selects all nodes containing the “frequency” attribute via XmlNodeList. Finally all “frequency” attribute value is updated and the XML file is saved.

    Here the code:

                        // Open document
                        filename = dlg.FileName;
    
                         if (!File.Exists(filename))
                            throw new Exception("Satellites file not found!");
    
                        // Create a backup copy of source file
                        int counter = 0;
                        while (File.Exists(filename + ".bak_" + counter))
                            counter++;
    
                        backupFileName = filename + ".bak_" + counter;
                        File.Copy(filename, filename + ".bak_" + counter);
    
                        XmlDocument doc = new XmlDocument();
                        doc.Load(backupFileName);
    
                        XmlNodeList nodeList = doc.SelectNodes("satellites/sat/transponder");                      
    
                        foreach (XmlNode transponderNode in nodeList)
                        {
                            foreach (XmlAttribute transponderAttribute in transponderNode.Attributes)
                            {
                                if (transponderAttribute.Name.Equals("frequency", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    long currentValue = long.Parse(transponderAttribute.Value);
                                    long newValue = currentValue + delta;
                                    
                                    transponderAttribute.Value = newValue.ToString();
                                }
                            }
                        }
    
                        doc.Save(filename);
    

    Here how the XML file looks like:

    <satellites>
      <sat name="37.5W NSS 10/Telstar 11N" flags="0" position="-375">
        <transponder frequency="12651000" symbol_rate="4280000" polarization="0" fec_inner="3" system="0" modulation="1" onid="235" tsid="17185" />
        <transponder frequency="12665264" symbol_rate="4280383" polarization="0" fec_inner="3" system="0" modulation="1" onid="65535" tsid="1" />
        <transponder frequency="12697500" symbol_rate="3214359" polarization="0" fec_inner="3" system="0" modulation="1" onid="1" tsid="1" />
      </sat>
    ...
    </satellites>