如何在运行时解析类型以避免乘法 [英] How to resolve type at run time to avoid multipe if else

查看:77
本文介绍了如何在运行时解析类型以避免乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的代码,该代码根据请求的类型进行网络服务调用。

I have my code which makes a webservice Call based on type of request.

为此,我有以下代码;

To do that , I have following code;

public class Client
{
    IRequest request;


    public Client(string requestType)
    {
        request = new EnrolmentRequest();
        if (requestType == "Enrol")
        {
            request.DoEnrolment();
        }
        else if (requestType == "ReEnrol")
        {
            request.DoReEnrolment();
        }
        else if (requestType == "DeleteEnrolment")
        {
            request.DeleteEnrolment();
        }
        else if (requestType == "UpdateEnrolment")
        {
            request.UpdateEnrolment();
        }
    }

}

按照打开关闭原则,我可以将其子类化为:

So as per open close principle, I can subclass like:

Class EnrolmentRequest:IRequest
{
    CallService();
}
Class ReEnrolmentRequest:IRequest
{
    CallService();
}
Class UpdateEnrolmentRequest:IRequest
{
    CallService();
}

现在我的客户类看起来像这样:

Now my client class will look something like this:

public class Client
{
    public Client(string requestType)
    {
        IRequest request;

        if (requestType == "Enrol")
        {
            request = new EnrolmentRequest();
            request.CallService();
        }
        else if (requestType == "ReEnrol")
        {
            request = new REnrolmentRequest();
            request.CallService();
        }
        else if (requestType == "DeleteEnrolment")
        {
            request = new UpdateEnrolmentRequest();
            request.CallService();
        }
        else if (requestType == "UpdateEnrolment")
        {
            request = new UpdateEnrolmentRequest();
            request.CallService();
        }
    }

}

现在,我仍然必须使用if和else,并且如果有任何新的请求类型,将不得不更改我的代码。

Now , I still have to use if and else , and will have to change my code if there are any new request type.

因此,这肯定是不可以修改的。

So, it's definitely, not closed to modification.

我是否缺少有关SOLID的任何东西?

Am I missing any thing with respect to SOLID?

我可以使用依赖项注入来解析类型吗?

Can I use dependency injection, to resolve the types at Run time?

推荐答案

您可以添加简单的工厂类,如下所示:

You can add simple factory class like below:

public class ServiceFactory : Dictionary<string, Type>
{
    public void Register(string typeName, Type serviceType) {
        if (this.ContainsKey(typeName)) {
            throw new Exception("Type registered");
        }
        this[typeName] = serviceType;
    }

    public IRequest Resolve(string typeName) {
        if (!this.ContainsKey(typeName)) {
            throw new Exception("Type not registered");
        }
        var type = this[typeName];
        var service = Activator.CreateInstance(type);
        return service as IRequest;
    }
}

然后在一个位置注册服务,例如:

then register services in one place like:

 var serviceFactory = new ServiceFactory();
        serviceFactory.Register("Enrol", typeof(EnrolmentRequest));
        serviceFactory.Register("ReEnrol", typeof(REnrolmentRequest));
        serviceFactory.Register("DeleteEnrolment", typeof(UpdateEnrolmentRequest));
        serviceFactory.Register("UpdateEnrolment", typeof(UpdateEnrolmentRequest));

并称之为:

var service = serviceFactory.Resolve(requestType);
service.CallService();

还需要添加适当的错误处理

also need to add proper error handling

这篇关于如何在运行时解析类型以避免乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆