Giter Site home page Giter Site logo

avalondi's Introduction

AvalonDI

A simple DI Container for WPF!

ViewModel's dependencies can be injected from constructor or by "AutoInject" attribute,just like controllers in MVC.

##Install

PM>Install-Package Ayx.AvalonDI

##Wire/Bind dependency

public partial class App : Application
{
    //DI Container
    public static AyxContainer Container = new AyxContainer();
    //View container
    public static AvalonContainer VM;

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        WireDependency();
        //show start window
        DI.GetView<MainWindow>().Show();
    }

    private void WireDependency()
    {
        //dependency
        Container.Wire<ITestDataRepo, TestDataRepo>();
        Container.WireSingleton<ILogger, SimpleLogger>();

        //view and viewmodel
        VM = new AvalonContainer(new DefaultContainer(Container));
        VM.WireVM<MainWindow, MainWindowViewModel>();
        VM.WireVM<TestOneView, TestOneViewModel>();
    }
}

##Inject from constructor

public class TestOneViewModel:NotificationObject
{
    private ITestDataRepo _repo;
    private ILogger _logger;

    public TestOneViewModel(ITestDataRepo repo, ILogger logger)
    {
        _repo = repo;
        _logger = logger;
    }
}

##Inject by "AutoInject" attribute

public class TestOneViewModel:NotificationObject
{
    [AutoInject]
    public ITestDataRepo Repo{get;set;}
    
    [AutoInject]
    public ILogger Logger { get; set; }
}

But I suggest use constructor to inject.

##Get a view

var view = App.VM.GetView<TestOneView>();

The DataContext of view will set to instance of TestOneViewModel automatically.

And the dependency of TestOneViewModel will be injected automatically.

##Use 3rd part DI container e.g Ninject

public partial class App : Application
    {
        //View and ViewModel container
        public static AvalonContainer VM;
        //Ninject container
        public static StandardKernel Ninject;

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            InitDependency();
            VM.GetView<MainWindow>()?.Show();
        }

        private void InitDependency()
        {
            Ninject = new StandardKernel();
            Ninject.Bind<ITestDataRepo>().To<TestDataRepo>().InSingletonScope();
            Ninject.Bind<ILogger>().To<SimpleLogger>().InSingletonScope();
            
            VM = new AvalonContainer(new NinjectContainer(Ninject));
            VM.WireVM<MainWindow, MainWindowViewModel>();
            VM.WireVM<TestOneView, TestOneViewModel>();
        }
    }

See NinjectSample for detail.

##Use token to make a View and ViewModel unique

VM.Wire<ViewA,ViewModelA>("A");
VM.Wire<ViewA,ViewModelAA>("AA");

var viewA = VM.GetView<ViewA>("A");   //view with DataContext ViewModelA
var viewAA = VM.GetView<ViewA>("AA"); //view with DataContext ViewModelAA

avalondi's People

Contributors

durow avatar

Watchers

James Cloos avatar

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.