pamidur / aspect-injector

AOP framework for .NET (c#, vb, etc)
Apache License 2.0
745 stars 112 forks source link

add LazyAttribute sample #161

Closed jerviscui closed 3 years ago

jerviscui commented 3 years ago

Fixes #152 Duplicate of #154

Should I continue to submit Lazy?

Sorry, I don't know how to modify a PR. I create a new PR. 😂

jerviscui commented 3 years ago

Discuss: https://github.com/pamidur/aspect-injector/pull/154#discussion_r653425093

I think the PropertyName used for dictionary key is enough.

  1. LazyAspect is Scope.PerInstance and LazyAspect_backFields is a instance field,
  2. Properties are not duplicated within a class.
pamidur commented 3 years ago

Hi @jerviscui, I've seen your update, it looks good to me,I'll make sure to properly review and merge it ASAP once return from vacation at the end of the next week!

jerviscui commented 3 years ago

All looks good! Before I merge, last question, do we still need [Aspect(Scope.PerInstance)] since we don't use an instance anywhere I think we might simplify and speedup things using Scope.Global

Hi, @pamidur This question, I think the Scope.Global will make private readonly Dictionary<string, object> _backFields a single instance. And, objects cached in _backFields will also become a singleton.

I think Scope.Global will destroy objects lifetime when used for a scoped lifetime dependency service.

example:

class MyClass
{
    private readonly IServiceProvider _serviceProvider;

    public MyClass(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }

    private static Service _service;

    public Service Service => _service ??= _serviceProvider.GetService<Service>();
}

[Aspect(Scope.PerInstance)] can used for Single and Scope lifetime components. Use careful for Transient components.

pamidur commented 3 years ago

Published here https://www.nuget.org/packages/Aspects.Lazy/