Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add readme "How To Implement Dependency Injection with Pure.DI code generator" #541

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

NikolayPianikov
Copy link

No description provided.

@thevortexcloud
Copy link
Contributor

Is there a reason this can't be added to the main DI article? There is a lot of overlap between them. Although other people may disagree about merging it.

Copy link
Contributor

@thevortexcloud thevortexcloud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have put some of my thoughts in. But it's also not really up to me either. I am not in charge around here.

string Greetings { get; }
}

public class MainViewModel(IBusinessService businessService)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Primary constructors are a fairly new feature. They should probably be avoided for now to ensure anybody copying and pasting the code can use it without worrying about their language version.

void RegisterSomething();
}

public class Repository: IRepository
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the formatting on this line

This will be useful if the main window will require dependencies to be injected.

Advantages over classical DI container libraries:
- No explicit initialisation of data contexts is required. Data contexts are configured directly in `.axaml` files according to the MVVM approach.
Copy link
Contributor

@thevortexcloud thevortexcloud Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not clear how what you have done is any different from the MS DI example. You are still explicitly resolving things from the container, just with more steps. This also sounds like you are implying the MS DI approach violates MVVM somehow?

There is also really nothing stopping you from setting up MS DI in a similar way. It just requires slightly more effort (and also a static locator).


- You can set a shared resource as a data context

`DataContext="{StaticResource Composition}"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may(?) cause side effects when running inside the previewer, as the previewer will run your real app code. You really should have a separate set of test services that sets the design time data context


Advantages over classical DI container libraries:
- No explicit initialisation of data contexts is required. Data contexts are configured directly in `.axaml` files according to the MVVM approach.
- The code is simpler, more compact, and requires less maintenance effort.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fairly opinionated, which I would argue does not belong in a tutorial like this. You should present facts and let people make their own decision

`Title="{Binding MainViewModel.Title}"`

Advantages over classical DI container libraries:
- The code-behind `.cs` files for views are free of any logic.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are many ways to do DI without using a static locator or putting any DI logic on code behind that work in basically every DI framework. The original example code for MSDI mentioned one possible way, it was just removed due to the lack of practical examples in the guide.

https://github.com/cerobe/avalonia-docs/blob/9ea95b7540c6eaf9a59ef88776d543ecc707244a/docs/guides/implementation-guides/how-to-implement-dependency-injection.md?plain=1#L11


Advantages over classical DI container libraries:
- No performance impact or side effects when creating composition of objects.
- All logic for analyzing the graph of objects, constructors and methods takes place at compile time. Pure.DI notifies the developer at compile time of missing or cyclic dependencies, cases when some dependencies are not suitable for injection, etc.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This paragraph is just an elaborated version of the previous one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants