pamidur / aspect-injector

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

Feature: Interface triggers is call class method #198

Open timeshift92 opened 1 year ago

timeshift92 commented 1 year ago

Describe the solution you'd like

i have add attributes in interface,

but i don't catch triggers in the advice


    [Advice(Kind.Around)]
    public object Handle(
            [Argument(Source.Target)] Func<object[], object> target,
            [Argument(Source.Name)] string methodName,
            [Argument(Source.Arguments)] object[] args,
            [Argument(Source.Triggers)] Attribute[] triggers
            )
    {

$code

public interface IPerson
{
    [Raise(Depend = $"People")]
    Person Create(string name, string fullName, string email);
    [Raise(Depend = nameof(Person.People), Type = RaiseType.UPDATE)]
    Person Update(Person _person);
    [Raise(Depend = nameof(Person.People), Type = RaiseType.DELETE)]
    public void Delete(Guid Id);
    [Cache]
    List<Person> People();
    [Cache]
    List<Person> People(int count);
    [Cache]
    List<Person> Test();
}

public class Person : IPerson
{
    private static List<Person> _People = new();
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string FullName { get; set; }
    public string Email { get; set; }

    public override int GetHashCode() => HashCode.Combine(Id, nameof(Person));

    public Person Create(string name, string fullName, string email)
    {
        var p = new Person()
        {
            Email = email,
            FullName = fullName,
            Name = name,
            Id = Guid.NewGuid()
        };
        _People.Add(p);

        return p;

    }

    public Person Update(Person _person)
    {
        for (int i = 0; i < _People.Count; i++)
        {
            if (_People[i].Id == _person.Id)
            {
                _People[i] = _person;
            }
        }

        return _person;

    }
    public void Delete(Guid Id)
    {
        var person = _People.Where(x => x.Id.Equals(Id)).FirstOrDefault();
        if (person != null)
            _People.Remove(person);
        else
            throw new Exception("Not Found");
    }

    public List<Person> People()
    {
        return _People;
    }
    public List<Person> People(int count)
    {
        return _People.Take(count).ToList();
    }

    public List<Person> Test()
    {
        return _People.ToList();
    }

}

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context Add any other context or code snippets about the feature request here.

pamidur commented 1 year ago

Hi @timeshift92 , as of now you can only use interface instead of attribute. You can't mark members of attribute with aspects. But this is interesting idea! We'll see how we can implement that