简体   繁体   English

实现可配置的工厂模式

[英]Implementing Configurable Factory Pattern

Existing Implementation: 现有实施:

I have implemented factory pattern for DB connection. 我已经为DB连接实现了工厂模式。 I am having three DB like sql,oracle and sybase. 我有三个DB像sql,oracle和sybase。 All three classes like sqlConnector,OracleConnector and SybaseConnector has implemented in Iconnect interface and in the factory method i have created the object of these concrete classes(sqlConnector,OracleConnector and SybaseConnector) based on switch case and returned specific object. 所有三个类(如sqlConnector,OracleConnector和SybaseConnector)都已在Iconnect接口中实现,并且在工厂方法中,我已根据switch case创建了这些具体类(sqlConnector,OracleConnector和SybaseConnector)的对象并返回了特定对象。

Scenario: 场景:

I want to modify this factory pattern and want to remove all switch case from factory method and want to configure through config file/xml and also without using reflection and spring.net. 我想修改这个工厂模式,并希望从工厂方法中删除所有switch case,并希望通过config file / xml配置,也不需要使用reflection和spring.net。 Suppose tommorow i introduce new db type , i don't want to change the code.I will make entry in the config/xml ,it should returned the object to client without changing factory. 假设tommorow我引入了新的db类型,我不想更改代码。我将在config / xml中输入,它应该将对象返回给客户端而不更改工厂。

Question: 题:

How i can implement configurable factory pattern means suppose tomorrow introducing new db type like "MySql", i don't want to change the code of factory. 我如何实现可配置的工厂模式意味着假设明天引入新的数据库类型如“MySql”,我不想更改工厂的代码。

This is a very broad question, but I think that the technology that will best serve you is Managed Extensibility Framework , or MEF. 这是一个非常广泛的问题,但我认为最适合您的技术是Managed Extensibility Framework ,或MEF。

MEF is an integral part of .NET that allows you to include additional components into your solution without having to know what those components are beforehand . MEF是.NET不可或缺的一部分,它允许您在解决方案中包含其他组件, 而无需事先知道这些组件是什么 MEF automatically discovers and includes components into your solution after your solution is built. 构建解决方案后, MEF会自动发现并将组件包含解决方案中。

You define the points where your solution can be extended; 您可以定义可以扩展解决方案的点; each component is written to take advantage of those extensibility points; 编写每个组件以利用这些可扩展性点; MEF ties them together automatically. MEF自动将它们联系在一起。

If you have used an Inversion of Control framework, like a Dependency Injection library (Ninject, Castle Windsor, SimpleInjector, etc.), then you have already done something a little like this, and the concepts should be somewhat familiar to you. 如果您使用了Inversion of Control框架,比如依赖注入库(Ninject,Castle Windsor,SimpleInjector等),那么您已经做了类似这样的事情,并且您应该对这些概念有所了解。

Explaining it and showing you how to use it will take a book. 解释它并向您展示如何使用它将需要一本书。 However, I'm reasonably certain that it will solve your problem best. 但是,我有理由相信它会最好地解决您的问题。 There are books, blogs, videos, and many, many examples. 有书籍,博客,视频和许多例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM