mrousavy / Jellyfish

🐟 An incredibly lightweight and type safe MVVM library for .NET WPF, Silverlight, Xamarin and UWP
MIT License
21 stars 4 forks source link
csharp dotnet enum fish jellyfish library light modern mvvm notifying-properties preferences silverlight uwp wpf xamarin

Jellyfish

AppVeyor badge NuGet downloads badge
Buy Me a Coffee at ko-fi.com

🐟

An incredibly light and type safe MVVM library for .NET WPF, Silverlight, Xamarin and UWP

Jellyfish is on NuGet:

PM> Install-Package Jellyfish

Make sure to also check out the Jellyfish Visual Studio Extension 📦!

Compared to other MVVM Frameworks like MVVM Light, Prism or Caliburn.Micro, this framework is

Usage

For description, documentation and usage, please view the Jellyfish wiki 📖 or the Getting Started guide 📖. For usage-example projects, please see Jellyfish.Demo or GameFinder.

📝 View Models

Every ViewModel needs to implement the ViewModel class:

public class LoginViewModel : ViewModel
{
    private User _user;
    public User User
    {
        get => _user;
        set => Set(ref _user, value);
    }
}

See View Models 📖

⚡ Commands

The RelayCommand is an ICommand implementation.

<Window ...>
    <Button Command="{Binding LoginCommand}" />
</Window>

Initialize the ICommand with a non-generic RelayCommand instance and the given action/callback:

ICommand LoginCommand = new RelayCommand(LoginAction, CanLogin);
// ...
void LoginAction(object parameter)
{ ... }
bool CanLogin(object parameter)
{ ... }

See Commands 📖

💉 Dependency Injection

Provide dependencies for types using the IInjector

At app startup:

Injector.Register<IUser>(() => new User("John", "Smith"));
Injector.Register<IDatabaseService>(() => OpenDatabaseService(username, password));

Some ViewModel:

class LoginViewModel : ViewModel
{
    IUser User { get; set; }
    IDatabaseService _service;

    LoginViewModel()
    {
        this.Inject();
    }
}

See Dependency Injection 📖

💾 Enums

The enum binding source extension allows for better binding support on enums.

Just use the EnumBindingSource extension to bind an enum to any ItemsSource:

<ComboBox ItemsSource="{Binding Source={jellyfish:EnumBindingSource {x:Type local:Status}}}"
      SelectedItem="{Binding SelectedStatus}" />

See Enums 📖

⚙️ Preferences

An abstract class definition for any application Preferences.

public class DemoPreferences : Preferences
{
    public int SomeInt { get; set; } = 400;
    public string SomeString { get; set; } = "test string";
    public bool SomeBool { get; set; } = false;

    public object SomeObject { get; set; } = new
    {
        Name = "Marc",
        IsValid = true
    };
}

See Preferences 📖

🔔 Feeds

The IFeed<T> allows notifying any subscribers in this feed about sudden changes within the application domain in realtime.

class CustomViewModel : INode<NotifyReason>
{
    public CustomViewModel
    {
        this.Subscribe();
    }

    public void MessageReceived(NotifyReason reason)
    { ... }
}

Feed.Notify(NotifyReason.RefreshView);

See Feeds 📖

Results

With Jellyfish

public class LoginViewModel : ViewModel
{
    private User _user;
    public User User
    {
        get => _user;
        set => Set(ref _user, value);
    }
}

Without Jellyfish

public class LoginViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
          PropertyChangedEventHandler handler = PropertyChanged;
          if (handler != null)
          {
              handler(this, new PropertyChangedEventArgs(propertyName));
          }
    }

    private string _username;
    public string Username
    {
        get
    {
        return _username;
    }
    set
    {
        _username = value;
        OnPropertyChanged("Username");
    }
    }
}