gpt4 book ai didi

c# - autofac/c#: A generic factory for a specific base class?

转载 作者:行者123 更新时间:2023-11-30 14:17:20 25 4
gpt4 key购买 nike

autofac 是否可以创建一个只能解析特定基类类型的通用工厂?

我目前看到改造“棕地”c# 项目以使用 autofac 的可行性。该项目需要创建不同类型但具有相同基类的组件树。这些组件需要身份验证和数据库服务(除其他外),因此很难只有空的构造函数。

所以工厂就是这样,用户可以创建派生基类的对象,使用 autofac 扫描这些类,并为它们提供工厂。我认为如果不必为每个类提供工厂,构建组件对象树可能会更容易。

顺便说一句,或者这是糟糕设计的标志?组件和树是否应该做的越少越好,我们将这棵树传递给其他服务来提供处理和渲染?

类似于(哪里?是神秘工厂)

public MyBase { 
public Add(MyBase x) {..}
}

public DerivedA: MyBase {}

public DerivedB : MyBase {}

public DerivedC : DerivedA {}

public SomethingElse
{
private ? _myBaseFact;

public SomethingElse(? myBaseFact) {
_myBaseFact = myBaseFact;
}

public BuildComponentTree() {
DerivedA a = _myBaseFact<DerivedA>();
DerivedB b = _myBaseFact<DerivedB>();
DerivedC c = _myBaseFact<DerivedC>();
a.Add(b);
b.Add(c);
}
}

编辑:我被要求提供一个更具体的示例,这很公平:) 有框架组件,我希望允许开发人员创建他们自己的组件而无需注册 autofac。拥有工厂类意味着开发人员必须将他们的自定义组件添加到该工厂,对吧?请原谅这个例子,这和我遇到的框架的工作方式类似。

FwComponent {
public FwComponent (DataProvider, AuthManager, OtherModule)
public Add(FwComponent x);
public Setup();
public Process();
public Render();
}

Page : FwComponent {
public Page(DataProvider, AuthManager, OtherModule): base(..)
public string Title;
...
}

Input: FwComponent {
public Input(DataProvider, AuthManager, OtherModule): base(..)
public string Name;
public int length;
...
}

Button: FwComponent {
public Button(DataProvider, AuthManager, OtherModule): base(..)
public string Name;
...
}

----

MyCustomButton : Button {
public MyCustomButton(DataProvider, AuthManager, OtherModule): base(..)
}

MyPersonalPage : FwComponent {

IContext _container;

public MyPersonalPage(DataProvider, AuthManager, OtherModule, IContext container): base(..)

public Setup() {
var input = _container.Resolve<Input>();
input.Name = 'xxx';
input.length = 10;
Add(input);

var butt = _container.Resolve<MyCustomButton>();
butt.name = "xxx";
Add(butt);
}

public Process() {
DataProvider.DoStuff();
}
}

最佳答案

正如我在对 Steven 的回答的评论中提到的,您的问题似乎是基于对工厂模式和多态性目的的误解。我会把这些留给他回答,但让我们来看看你对依赖注入(inject)的误解......

1) 依赖注入(inject)适用于服务,而非实体

不清楚您的组件和树是什么,但我猜它们是您域中的实体,而不是对它们进行处理的服务。依赖注入(inject)用于将服务(在最一般意义上)注入(inject)其他服务或粘合层。示例包括将存储库注入(inject) MVC Controller ,或将 Twitter 发布 Web 服务注入(inject) View 模型,或将产品工厂注入(inject)产品操作服务。

2) 依赖注入(inject)及其框架与工厂模式无关

虽然它名为 AutoFac,但它不是某种自动自动工厂生成器,正如 Unity 不是一种将所有类(class)统一为一个或温莎城堡是……嗯……我不知道。这两个概念可能唯一的联系是工厂是否是您注入(inject)的上述服务之一。

3) 依赖注入(inject)依赖于好的设计

也就是说,您必须使用适当的面向对象的注意事项,例如使用构造函数来构造一个类。尽管我怀疑树中的组件是实体(如上所述),但如果它们是服务,则 Add 方法将违反这些设计原则:如果类需要 MyBase 的实例,它应该将其作为构造函数参数,并使用适当的保护子句在不满足此不变量时抛出异常。类似地,SomethingElse 显然需要一个组件树,它可以由根节点指定:好吧,那么它应该将该根节点作为构造函数参数。


换句话说,树状情况的依赖注入(inject)友好版本最终会看起来像

interface INotificationService { }
class TwitterNotificationService : INotificationService { }
class FacebookNotificationService : INotificationService { }
class CompositeNotificationService : INotificationService
{
public CompositeNotificationService(IEnumerable<NotificationService> services) { }
// implement composite pattern
}

class NotificationController : Controller
{
public NotificationController(INotificationService service)
{
if (service === null)
{
throw new ArgumentNullException("service");
}

// ...
}
}

以后,如果你使用穷人的 DI,你会做类似的事情

var controller = new NotificationController(
new CompositeNotificationService(new []
{
new TwitterNotificationService(),
new FacebookNotificationService()
}));

DI 框架的目的只是将诸如此类的样板构建代码移动到应用程序的一个中心位置(组合根),从而有效地消除应用程序中的服务构建问题并将其转化为配置细节。但是您的应用程序需要对 DI 友好,这实际上意味着要符合基本的 OO 原则,例如使用构造函数,DI 才能工作。

关于c# - autofac/c#: A generic factory for a specific base class?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6165385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com