neowu / core-ng-project

webapp framework
Apache License 2.0
127 stars 50 forks source link

有支持接口注入的实现方式吗 #61

Closed lchaofu closed 4 months ago

lchaofu commented 4 months ago

源码中都是获取具体的实现类进行注入,实现上只能依赖具体的实现类,这种设计导致两个不同对象严重耦合再一起,不符合依赖倒置原则,对系统的扩展性和可维护性不是那么友好,为何不用依赖接口的方式,注入时去获取具体的实现类。

neowu commented 4 months ago

Module 里的这两个方法,就是期望你用interface进行bind,所以根据你的业务场景,你可以自己选择依赖interface还是concrete class

public T bind(Class<? super T> instanceClass, T instance) public T bind(Type type, @Nullable String name, T instance)

On Jun 13, 2024, at 1:16 PM, lchaofu @.***> wrote:

源码中都是获取具体的实现类进行注入,实现上只能依赖具体的实现类,这种设计导致两个不同对象严重耦合再一起,不符合依赖倒置原则,对系统的扩展性和可维护性不是那么友好,为何不用依赖接口的方式,注入时去获取具体的实现类。 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>

lchaofu commented 4 months ago

Module 里的这两个方法,就是期望你用interface进行bind,所以根据你的业务场景,你可以自己选择依赖interface还是concrete class public T bind(Class<? super T> instanceClass, T instance) public T bind(Type type, @nullable String name, T instance) On Jun 13, 2024, at 1:16 PM, lchaofu @.> wrote: 源码中都是获取具体的实现类进行注入,实现上只能依赖具体的实现类,这种设计导致两个不同对象严重耦合再一起,不符合依赖倒置原则,对系统的扩展性和可维护性不是那么友好,为何不用依赖接口的方式,注入时去获取具体的实现类。 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.>

希望能支持@Autowired List list方式的注入和@Order

neowu commented 4 months ago

List本来就可以bind

bind(Types.list(Item.class), "someName", someList);

不过不推荐这么用,在实际项目中一般会做一个wrapper class,因为List的接口太泛化。

@Autowired, @Order是不会支持的,一方面这是spring自己的协议,另一方面这和core-ng的设计理念不符,core-ng是strong opinion的framework。 和spring的设计目标完全不同。参见wiki

https://github.com/neowu/core-ng-project/wiki/Design#rationality Design github.com

On Jun 13, 2024, at 2:06 PM, lchaofu @.***> wrote:

Module 里的这两个方法,就是期望你用interface进行bind,所以根据你的业务场景,你可以自己选择依赖interface还是concrete class public T bind(Class<? super T> instanceClass, T instance) public T bind(Type type, @nullable https://github.com/nullable String name, T instance) … <x-msg://5/#> On Jun 13, 2024, at 1:16 PM, lchaofu @.> wrote: 源码中都是获取具体的实现类进行注入,实现上只能依赖具体的实现类,这种设计导致两个不同对象严重耦合再一起,不符合依赖倒置原则,对系统的扩展性和可维护性不是那么友好,为何不用依赖接口的方式,注入时去获取具体的实现类。 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.>

@. https://github.com/Autowired List @. https://github.com/order — Reply to this email directly, view it on GitHub https://github.com/neowu/core-ng-project/issues/61#issuecomment-2164520500, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIMY3OMMPXI555YOKBGCL3ZHEZGFAVCNFSM6AAAAABJHT5AY6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNRUGUZDANJQGA. You are receiving this because you commented.

lchaofu commented 4 months ago

List本来就可以bind bind(Types.list(Item.class), "someName", someList); 不过不推荐这么用,在实际项目中一般会做一个wrapper class,因为List的接口太泛化。 @Autowired, @order是不会支持的,一方面这是spring自己的协议,另一方面这和core-ng的设计理念不符,core-ng是strong opinion的framework。 和spring的设计目标完全不同。参见wiki https://github.com/neowu/core-ng-project/wiki/Design#rationality Design github.com On Jun 13, 2024, at 2:06 PM, lchaofu @.> wrote: Module 里的这两个方法,就是期望你用interface进行bind,所以根据你的业务场景,你可以自己选择依赖interface还是concrete class public T bind(Class<? super T> instanceClass, T instance) public T bind(Type type, @nullable https://github.com/nullable String name, T instance) … <x-msg://5/#> On Jun 13, 2024, at 1:16 PM, lchaofu @.> wrote: 源码中都是获取具体的实现类进行注入,实现上只能依赖具体的实现类,这种设计导致两个不同对象严重耦合再一起,不符合依赖倒置原则,对系统的扩展性和可维护性不是那么友好,为何不用依赖接口的方式,注入时去获取具体的实现类。 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.> @. https://github.com/Autowired List @.*** https://github.com/order — Reply to this email directly, view it on GitHub <#61 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIMY3OMMPXI555YOKBGCL3ZHEZGFAVCNFSM6AAAAABJHT5AY6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNRUGUZDANJQGA. You are receiving this because you commented.

感觉解答

lchaofu commented 4 months ago

List本来就可以bind bind(Types.list(Item.class), "someName", someList); 不过不推荐这么用,在实际项目中一般会做一个wrapper class,因为List的接口太泛化。 @Autowired, @order是不会支持的,一方面这是spring自己的协议,另一方面这和core-ng的设计理念不符,core-ng是strong opinion的framework。 和spring的设计目标完全不同。参见wiki https://github.com/neowu/core-ng-project/wiki/Design#rationality Design github.com On Jun 13, 2024, at 2:06 PM, lchaofu @.> wrote: Module 里的这两个方法,就是期望你用interface进行bind,所以根据你的业务场景,你可以自己选择依赖interface还是concrete class public T bind(Class<? super T> instanceClass, T instance) public T bind(Type type, @nullable https://github.com/nullable String name, T instance) … <x-msg://5/#> On Jun 13, 2024, at 1:16 PM, lchaofu @.> wrote: 源码中都是获取具体的实现类进行注入,实现上只能依赖具体的实现类,这种设计导致两个不同对象严重耦合再一起,不符合依赖倒置原则,对系统的扩展性和可维护性不是那么友好,为何不用依赖接口的方式,注入时去获取具体的实现类。 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.> @. https://github.com/Autowired List @.*** https://github.com/order — Reply to this email directly, view it on GitHub <#61 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIMY3OMMPXI555YOKBGCL3ZHEZGFAVCNFSM6AAAAABJHT5AY6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNRUGUZDANJQGA. You are receiving this because you commented.

我这里的意思是,类似于spring的那种方式,只需要@Inject List,就会自动将beans里面所有 S implements T的类注入到List里面,从而不需要自己去bind(Types.list(Item.class), "someName", someList);

neowu commented 4 months ago

core-ng 的一个主要设计目标是为了维护/review容易,也就是说倾向显式的指明bean的构造,这样reviewer从module里就可以清楚看到所有 业务对象是怎么组装起来的,也是IOC原则。

所以没有classpath bean scanning,或者auto wire的意思,不是为了让你快速开发方便的,这是和Spring或者Ruby On Rails的一个主要分歧。core-ng的项目都是维护时间远远大于开发时间的项目。

On Jun 14, 2024, at 9:38 AM, lemony @.***> wrote:

Reopened #61 https://github.com/neowu/core-ng-project/issues/61.

— Reply to this email directly, view it on GitHub https://github.com/neowu/core-ng-project/issues/61#event-13154404584, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIMY3JRTLIR4IA23GQVVLDZHJCSDAVCNFSM6AAAAABJHT5AY6VHI2DSMVQWIX3LMV45UABCJFZXG5LFIV3GK3TUJZXXI2LGNFRWC5DJN5XDWMJTGE2TINBQGQ2TQNA. You are receiving this because you commented.