Mendz.Library v1.2.1 Adds More Goodies

Mendz.Library is my class library project for anything that can be used shared and used by various projects. Version 1.2.1 adds new classes, types and extensions that can help spark new ideas and inspirations. Here are the highlights.

CommandProcess and ShellProcess

As-is, the Process class in .Net is not that hard to use. However, there are some combinations of settings that can be confusing, or are easy to forget, no matter how many times you read the documentation. If you just want to hit it and get it right most of the time, Mendz.Library's CommandProcess and ShellProcess might be able to help. In a nutshell, you can use them to launch applications from within your applications.

CommandProcess.Start() provides an easy way to start window-less programs. Use CommandProcess to start processes that need no human intervention to complete. Console commands, back-end jobs and batch processing applications are good candidates, for example.

using Mendz.Library;
...
    private StringBuilder _output = new StringBuilder();
    ...
        int exitCode = CommandProcess.Start(new ProcessStartInfo()
        {
            FileName = "cmd.exe",
            Arguments = "/c dir",
            RedirectStandardOutput = true
        }, Process_Output);
    ...
    private void Process_Output(object sender, DataReceivedEventArgs e)
    {
        _output.Append(e.Data);
    }
...

ShellProcess.Start() provides an easy way to start windowed programs, or an external program, or programs that can be launched by the platform's shell. You can also use it to launch the platform's default application for a specific file type. For example, if you pass in a URL, it will launch the web site in the platform's default browser.

using Mendz.Library;
...
    ShellProcess.Start(new ProcessStartInfo() { FileName = "https://www.bing.com" });
...

By default, both CommandProcess and ShellProcess wait for the launched program to exit. There are limitations to this capability specially with ShellProcess, mostly dependent on how the platform's shell launches applications or how the application itself launches and registers its process to the system.

IGenericMapper and IStreamingMapper

This interfaces are nifty little tools that you can use to define classes that map data between two completely different objects. For example, if you create the following:

    public class Person
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

    public class PersonCSVToPerson : IGenericMapper<string, Person>
    {
        public Person Map(string input, Func<Person> instance)
        {
            string[] p = input.Split(',');
            Person person = instance();
            person.ID = (int)p[0];
            person.Name = p[1];
            return person;
        }
    }

You can call it in a program where the conversion may be needed like so:

...
    PersonCSVToPerson personCSVToPerson = new PersonCSVToPerson();
    string personCSV = "1,Mendz";
    Person person = personCSVToPerson.Map(personCSV, () => new Person());
...

A streaming version of this can be created like the following:

    public class PersonCSVsToPersons : IStreamingMapper<string, Person>
    {
        public IEnumerable<Person> Map(IEnumerable<string> input, 
            Func<Person> instance)
        {
            PersonCSVToPerson personCSVToPerson = new PersonCSVToPerson();
            foreach (var item in input)
            {
                yield return personCSVToPerson.Map(item, instance);
            }
        }
    }

Which can be used like so:

    string[] persons = new string[] { "1,Mendz", "2,AsI", "3,SeeTech" };
    PersonCSVsToPersons personCSVsToPersons = new PersonCSVsToPersons();
    foreach (var person in personCSVsToPersons.Map(persons, () => new Person()))
    {
        ...
    }

String and TextReader Extensions

Mendz.Library.Extensions define a StringExtensions class. Mendz.Library.Extensions.IO defines a TextReaderExtensions class.

String's IsMatch() and ReplaceMatch() extensions provide shortcuts to using regular expressions to string instances.

TextReader's ReadLineMatch() and ReadAllMatch() extensions provide shortcuts to using regular expressions when reading TextReader instances. The YieldLine() shorthands an enumerable/iterable read. YieldLineMatch() also shorthands an enumerable/iterable read, but yields only lines that match a regular expression.

using Mendz.Library.Extensions.IO;
...
    // yield only lines starting with "MSG*"...
    foreach (string line in reader.YieldLineMatch(@"^MSG\*.+"))
    {
        ...
    }
...

Mendz.Library is available at NuGet. Find codes and samples at GitHub.

Have fun!

Comments