基于使用C#传递给函数的参数创建不同对象的实例 [英] Create Instance of different Object Based on parameter passed to function using C#

查看:120
本文介绍了基于使用C#传递给函数的参数创建不同对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有基类FactoryBO,该基类具有创建不同对象的实例的方法。

I have base class FactoryBO which has a method to create Instance of different object.

public class FactoryBO
{
    //ctor
    public FactoryBO()
    {
    }

    public T CreateObject<T>(string BusinessObjectName)
    {
        if (string.IsNullOrEmpty(BusinessObjectName))
            return default(T);

        object valueObj = null;

        switch (BusinessObjectName.ToLower())
        {
           case "temporary registration": { valueObj = new TemporaryDriverRegistrationBO(); break; }
            case "driver registration": { valueObj = new TemporaryDriverRegistrationBO(); break; }            }

        return (T)valueObj;
    }

    public virtual SqlParameter[] GenerateQuery(params object[] StoredProcedureParameters)
    {
        return null;
    }
}

FactoryBO有2个派生类,它们覆盖了

There are 2 derived class from FactoryBO, which overrides the virtual method in FactoryBO.

public class TemporaryDriverRegistrationBO : FactoryBO
{
    public override SqlParameter[] GenerateQuery(params object[] StoredProcedureParameters)
    {
         //some Code
    }
}

 public class DriverRegistrationBO : FactoryBO
 {
      public override SqlParameter[] GenerateQuery(params object[] StoredProcedureParameters)
    {
         //some Code
    }
 }

现在,如果我执行以下代码,它将为我提供 TemporaryDriverRegistrationBO对象

Now If I execute below code it gives me object of "TemporaryDriverRegistrationBO"

   object fbo = new FactoryBO().CreateObject<object>("temporary registration");
   SqlParameter[]  xx = ((FactoryBO)(fbo)).GenerateQuery()

我做错了吗?这是否遵循任何设计模式?

关于功能的正确做法是什么?

What would be the correct way of doing about functionality?

推荐答案

在您的实现中,您并没有限制代码用户使用 T 。这听起来像个问题,请考虑使用以下类型参数限制,其中T:FactoryBo,new()
但这不是您想要的。我建议创建一个将某些业务对象名称映射到适当类的构造函数的函数字典:

In your implementation you are not limiting user of your code with what to pass as T. That sounds like an issue, consider using following type parameter limitation where T: FactoryBo, new(). Yet that isn't what you look for. What I'd suggest is creating a dictionary of functions mapping certain business object name to the constructor of appropriate class:

public class FactoryBO
    {
        public static Dictionary<string, Func<FactoryBO>> objects =
                new Dictionary<string, Func<FactoryBO>>
                    {
                            {"temporary registration", () => new TemporaryDriverRegistrationBO()},
                            {"driver registration", () => new DriverRegistrationBO()}
                    };

        public static FactoryBO CreateObject(string businessObjectName)
        {
            if (string.IsNullOrWhiteSpace(businessObjectName))
            {
                return new FactoryBO();
            }

            Func<FactoryBO> objectCtor = null;
            objects.TryGetValue(businessObjectName.ToLower(), out objectCtor);

            return objectCtor != null ? objectCtor() : new FactoryBO();
        }

        public virtual SqlParameter[] GenerateQuery(params object[] StoredProcedureParameters)
        {
            return null;
        }
    }

在我的实现中,我将返回base的一个实例class- FactoryBO ,以防在字典中找不到该条目。这与您为空字符串参数返回 default(T)的行为有些不同(这返回 null 作为参考)类型)。

In my implementation I'm returning an instance of base class - FactoryBO in case the entry is not found in the dictionary. That's a bit different from your behaviour where you return default(T) for empty string parameter(this returns null for reference types).

http ://msdn.microsoft.com/zh-CN/library/xwth0h0d.aspx

这篇关于基于使用C#传递给函数的参数创建不同对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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