简体   繁体   English

C#/ ASP.NET MVC 4实例化从工厂方法中的接口派生的对象

[英]C#/ASP.NET MVC 4 Instantiate Object Derived From Interface In Factory Method

Currently have a Factory class that features a GetSelector function, which returns a concrete implementation of ISelector . 当前具有一个具有GetSelector函数的Factory类,该函数返回ISelector的具体实现。 I have several different classes that implement ISelector and based on a setting I would like to receive the appropriate ISelector back. 我有几个实现ISelector的不同类,并且基于一个设置,我希望收到适当的ISelector

public interface ISelector
{
    string GetValue(string Params);
}

public class XmlSelector : ISelector
{
    public string GetValue(string Params)
    {
        // open XML file and get value
    }
}

public static class SelectorFactory
{
    public static ISelector GetSelector()
    {
        return new XmlSelector(); // Needs changing to look at settings
    }
}

My question is what is the best way to store the setting? 我的问题是存储设置的最佳方法是什么? I am aware of using AppSettings etc. but I'm not sure whether I want to have to store strings in the web.config and perform a switch on it - just seems to be really tightly coupled in that if a new implementation of ISelector is made, then the Factory would need to be changed. 我知道使用AppSettings等,但是我不确定是否要在web.config中存储字符串并对其进行切换-似乎真的紧密地联系在一起,如果ISelector的新实现是制作,则需要更改工厂。 Is there any way of perhaps storing an assembly name and instantiating based on that? 有没有办法存储程序集名称并基于该实例化?

Thanks, 谢谢,

Chris 克里斯

It is hard to say, because I don't know the architecture of your particular project, but at a first glance what I would do is if the objects associated with ISelector can be decoupled from your web application, I would put these objects in a class library along with the factory. 很难说,因为我不知道您的特定项目的体系结构,但是乍一看我会做的是, 如果可以将与ISelector关联的对象与Web应用程序分离,我会将这些对象放在一个类库以及工厂。 Your factory will need to be changed if you implement a new ISelector, but if you can decouple the whole ISelector family from your actual web application the depth of the refactoring you will have to do will be minimal compared to a monolithic architecture. 如果实现新的ISelector,则需要更改工厂,但是如果您可以将整个ISelector系列与实际的Web应用程序分离,则与单片架构相比,重构的深度将很小。

Personally, I tend to avoid AppSettings, web.config settings and the like for mission-critical design questions. 就个人而言,我倾向于避免针对关键任务设计问题使用AppSettings,web.config设置等。 Using the web.config as an example, I have seen applications where architectural data is stored for ease of configurability. 以web.config为例,我看到了存储体系结构数据以简化可配置性的应用程序。 The problem is that after compilation your web.config can be changed (that is the purpose of it after all) and if the implementation of your classes depends on very specific values being chosen, you are running a risk of a crash when someone inadvertently modifies the wrong value. 问题在于,编译后可以更改web.config(毕竟是这样做的目的),并且如果类的实现取决于所选择的非常特定的值,那么当有人无意间修改时,您就有崩溃的风险。错误的值。

Like I said all this depends entirely on your application architecture, but my reflex would be to split out the components that could be subject to future modification into a class library. 就像我说的那样,所有这些都完全取决于您的应用程序体系结构,但是我的反思是将可能会在将来进行修改的组件拆分为一个类库。 Loose coupling is your friend ;). 松耦合是你的朋友;)。

我认为不是在AppSettings中这样做,而是一种更好的方法是创建一个单独的XML文件,该文件仅保存映射,您可以从该文件中迭代映射并在GetSelector()返回正确的实例。

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

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