简体   繁体   English

构建工厂模式的通用方法

[英]generic methods to build factory pattern

I have 3 objects that have same properties with some differences each object have Id, Name, and TypeCode.我有 3 个具有相同属性的对象,但每个对象都有 Id、Name 和 TypeCode。 In database we have a first table containing Id, Name and TypeCode fields, and there is 3 other tables containing each one the corresponding properties of each object:在数据库中,我们有一个包含 Id、Name 和 TypeCode 字段的第一个表,还有 3 个其他表包含每个对象的相应属性:

Table 1: Id |表 1:ID | Name |姓名 | TypeCode类型代码

Table 2: Id |表 2:ID | FunctionName函数名

Table 3: Id |表 3:ID | AnalysisName分析名称

Table 4: Id |表 4:ID | PropertyName属性名称

We have a storedProcdure called "GetProperties" that returns the properties of an object according to its TypeCode.我们有一个称为“GetProperties”的存储过程,它根据对象的类型代码返回对象的属性。 "GetProperties" takes one arguments which is the object Id. “GetProperties”接受一个参数,即对象 ID。 we don't know the TypeCode before calling the stored procedure.在调用存储过程之前我们不知道 TypeCode。

If TypeCode is a function is returns the following fields:如果 TypeCode 是一个函数,则返回以下字段:

Id, Name, TypeCode, FunctionName ID、名称、类型代码、功能名称

If TypeCode is an Analysis is returns the following fields:如果 TypeCode 是 Analysis 则返回以下字段:

Id, Name, TypeCode, AnalysisName ID、名称、类型代码、分析名称

And etc...等等...

I have created 4 classes: FunctionObject, AnalysisObject and PropertyObject that inherit from class MyObject.我创建了 4 个类:FunctionObject、AnalysisObject 和 PropertyObject,它们继承自 MyObject 类。

public class MyObject
{
     public decimal Id;
     public string Name;
     public string TypeCode;
}

public class FunctionObject: MyObject
{
    public string FunctionName;
}

public class AnalysisObject: MyObject
{
    public string AnalysisName;
}

public class PropertyObject: MyObject
{
    public string PropertyName;
}

I have created a generic function that allows me to create objects dynamically:我创建了一个通用函数,允许我动态创建对象:

    public T GetObject<T>(string storedProcedure, List<DataBaseParameteres> parameters) where T: new()
{
    T result = new T();
    OpenConnection();
    SqlDataReader reader = ExecuteSqlCommand(storedProcedure, parameters);
    PropertyInfo[] properties = typeOf(T).GetProperties();

    if(reader.Read())
    {
        for(int i = 0; i<properties.Count(); i++)
        {
            object value = Convert.ChangeType(reader[properties[i].Name], properties[i].PropertyType);
            properties[i].SetValues(result, value, null);
        }
    }
    reader.Close();
    CloseConnection();
    return result;
}

This function works well, i can call it as follow and it returns the created object:这个函数运行良好,我可以如下调用它并返回创建的对象:

MyObject obj1 = GetObject<MyObject>(obj1Id);

My problem now is that i want to know how to modify the generic method "GetObject" to create objects: FunctionObject, AnalysisObject and Propertyobject.我现在的问题是我想知道如何修改通用方法“GetObject”来创建对象:FunctionObject、AnalysisObject 和 Propertyobject。 I don't have any idea about TypeCode when calling the function, so I can't do this unless I have the TypeCode:我在调用函数时对 TypeCode 一无所知,所以除非我有 TypeCode,否则我不能这样做:

FunctionObject obj2 = GetObject<FunctionObject>(obj2Id);

I dont think, as per your scenario, it is possible to create the objects of these types.我不认为,根据您的情况,可以创建这些类型的对象。 The reason being you have embedded the call to the stored procedure and creation of the object in the same method and intermingled them.原因是您已经在同一方法中嵌入了对存储过程的调用和对象的创建并将它们混合在一起。

My suggestion is to remove the call to the stored procedure from that method to another method, execute that stored proc to a datatable and pass that datatable to this method.我的建议是删除从该方法到另一个方法对存储过程的调用,将该存储过程执行到数据表并将该数据表传递给该方法。 In this way you will have your TypeCode with you in advance and then you can use factory pattern to create objects of required type.通过这种方式,您将提前拥有您的TypeCode ,然后您可以使用工厂模式来创建所需类型的对象。

Also in this way you can also reduce your dependency on reflection and hence your code might be a little better performance wise.同样通过这种方式,您还可以减少对反射的依赖,因此您的代码在性能方面可能会更好一些。

Hope this helps.希望这可以帮助。

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

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