MASA.Framework icon indicating copy to clipboard operation
MASA.Framework copied to clipboard

fix: IgnoreInjection support ImplementationType

Open xiaoxiaotank opened this issue 2 years ago • 3 comments

Description

Look this:

using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection();

services.AddAutoInject();

var sp = services.BuildServiceProvider();

var userServices = sp.GetServices<IUserService>();

// We expect to output only UserService1, but it includes UserService2
Console.WriteLine(string.Join(Environment.NewLine, userServices.Select(s => s.ToString())));

public interface IUserService : ITransientDependency { }

public class UserService1 : IUserService { }

[IgnoreInjection]
public class UserService2 : IUserService { }

Issue reference

None

Checklist

Please make sure you've completed the relevant tasks for this PR, out of the following list:

  • [✔] Code compiles correctly
  • [✔] Created/updated tests
  • [✔] Extended the documentation

xiaoxiaotank avatar Feb 05 '24 08:02 xiaoxiaotank

这种使用场景一般不存在吧,如果存在多个实现,一般会用构造方法进行区别创建

Qinyouzeng avatar Feb 22 '24 08:02 Qinyouzeng

这种使用场景一般不存在吧,如果存在多个实现,一般会用构造方法进行区别创建

目前没有遇到一个特别适合的场景,但我认为从功能设计上来说,它确实应该支持

xiaoxiaotank avatar Feb 22 '24 09:02 xiaoxiaotank

之所以想要支持该功能,是因为编写下方代码时会使人产生疑惑:

var services = new ServiceCollection();

services.AddAutoInject();

var sp = services.BuildServiceProvider();

// UserService1
var userServices1 = sp.GetService<UserService1>();
// null
var userServices2 = sp.GetService<UserService2>();

public abstract class UserServiceBase : ITransientDependency { }
public class UserService1 : UserServiceBase { }
[IgnoreInjection]
public class UserService2 : UserServiceBase { }

当服务继承的是类时,userServices2 是可以被忽略的,但是当继承(实现)的是接口时(最上方代码),它却没有被忽略。这很容易让人怀疑人生。

xiaoxiaotank avatar Feb 22 '24 09:02 xiaoxiaotank