Coder Social home page Coder Social logo

purelib's Introduction

PureLib

Build status NuGet Version License MIT

A lightweight C# utility library.

Download

The library is available on nuget.org via package name PureLib, PureLib.Legacy and PureLib.EntityFramework.

To install PureLib, run the following command in the Package Manager Console

PM> Install-Package PureLib
PM> Install-Package PureLib.Legacy
PM> Install-Package PureLib.EntityFramework

More information about NuGet package avaliable at https://nuget.org/packages/PureLib

Features

Notify Object

NotifyObject implements INotifyPropertyChanged, which enables you to raise changes of properties.

public string StatusBarText {
    get { return _statusBarText; }
    set {
        _statusBarText = value;
        RaiseChange(() => StatusBarText); // or RaiseChange("StatusBarText");
    }
}

View Model Base

ViewModelBase inherits NotifyObject, which is designed to be the base class of ViewModels in MVVM pattern.

    public class MainWindowViewModel : ViewModelBase {
        public ObservableCollection<string> Files { get; set; }
        
        private void OnTaskStarted(object sender, TaskStartedEventArgs e) {
            RunOnUIThread(() => {
                if (!Files.Contains(e.File))
                    Files.Add(e.File);
            });
        }
    }

Relay Command

RelayCommand implements ICommand, which could be bound to UI controls.

private ICommand _openDescriptionCommand;
public ICommand OpenDescriptionCommand {
    get {
        if (_openDescriptionCommand == null)
            _openDescriptionCommand = new RelayCommand(p => {
                OpeningDescription(this, new EventArgs<string>(((WatFile)p).Description));
            }, p => !((WatFile)p).Description.IsNullOrEmpty());
        return _openDescriptionCommand;
    }
}

Single Instance App

SingleInstanceApp inherits Application. The application inherits SingleInstanceApp will not be able to run multiple instances.

public partial class App : SingleInstanceApp { 
}

Converters

PureLib provides commonly used converters for UI bindings.

  • BooleanToVisibilityConverter
  • InverseBooleanConverter

Web Requester

WebRequester is a wrapper for easily using HttpWebRequest. It features auto retry with specified times.

_requester = new WebRequester(_cookiePersister.CookieContainer) {
    UserAgent = agent,
    Referer = referer,
    Encoding = Encoding.GetEncoding("gbk"),
    RetryInterval = 2000,
    RetryLimit = 5
};
_requester.SetRequest += (s, e) => {
    e.Data.Proxy = null;
};
_requester.GotResponse += (s, e) => {
    if (!_ignoreCookie)
        _cookiePersister.Update(e.Data);
};

Web Downloader

WebDownloader contains essential functions of a download manager. It can dispatch any number of threads to download concurrently.

WebDownloader downloader = new WebDownloader(Global.Config.ThreadCount, null, false);
downloader.DownloadCompleting += OnDownloadCompleting;
downloader.AddItems(_itemPostMaps.Keys.ToList());

Utility

Get running time of specfic code segment:

TimeSpan ts = Utility.GetExecutingDuration(() => { 
    for (int i = 0; i < 1000000; i++) {
    }
});

Convert a wildcard to a regular expression:

string regex = "*.txt".WildcardToRegex();

Convert a byte array to a string in hex format:

string hex = Encoding.UTF8.GetBytes("hello").ToHexString();

Convert a string of enum list to an enum array:

DayOfWeek[] days = "Sunday,Saturday".ToEnum<DayOfWeek>();

Get human-friendly text from an exception:

try {
} catch (Exception ex) {
    string text = ex.GetTraceText();
}

Shortcuts for reading/writing files on disk:

"D:\\data.bin".WriteBinary(new byte[] { 0x00, 0x01 });
byte[] data = "D:\\data.bin".ReadBinary();

"D:\\text.txt".WriteText("hello", Encoding.Default);
string text = "D:\\text.txt".ReadText();

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.