需要基于类型构建请求对象.你会怎么做呢?

本文关键字:请求 构建 对象 你会怎么做 类型 于类型 | 更新日期: 2023-09-27 18:01:58

我对使用哪种模式最适合我的需求感到困惑。我希望你能澄清。不确定是使用构建器还是工厂

场景是这样的。创建一个请求对象并将其传递给我无法控制的第三方组件

Request可以是多种类型——employeerrequest - workerrequest -等等…并且每个请求都有类似"GetXXX" -GetYYY"等方法。

笨人的例子我做了如下的事情:

        class Program
            {
                static void Main()
                {
                    Request employeeRequest = new EmployeeRequest { Id=1,FirstName = "Foo", LastName = "Bar" };
                    Service myservice=new Service();
                    var response = myservice.DoSomething(employeeRequest);
                    //do something with the response ...
                }
            }
            public class Service
            {
                public Response DoSomething(Request request)
                {
                    Factory factory = new Factory();
                    Request myRequest = factory.Create(request);
                    //Call my third party component
                    //Response myResponse = MyThirdPartyComponent.DoSomething(myRequest);
                    //return myResponse;
                    return new Response();
                }
            }
            public class Factory
            {
                public Request Create(Request request)
                {
                    if (request is EmployeeRequest)
                    {
                        EmployeeRequest employeeRequest = new EmployeeRequest();
                        employeeRequest.FirstName = request.FirstName;
                        employeeRequest.LastName = request.LastName;
                        return employeeRequest;
                    }
                    else
                    {
                        WorkerRequest workerRequest = new WorkerRequest();
                        workerRequest.FirstName = request.FirstName;
                        workerRequest.LastName = request.LastName;
                        return workerRequest;
                    }
                }
            }
            public abstract class Request
            {
                public int Id { get; set; }
                public string FirstName { get; set; }
                public string LastName { get; set; }
            }
            public class Response
            {
                public string FirstName { get; set; }
                public string LastName { get; set; }
            }
            public class EmployeeRequest : Request
            {}
            public class WorkerRequest : Request
            {}
            public class EmployeeResponse : Response
            {}
            public class WorkerResponse : Response
            {}

如果你要实现它,你会怎么做:

谢谢

需要基于类型构建请求对象.你会怎么做呢?

如果您只是想为继承Request的类型填写Request基本类型的属性,请使用泛型方法(您可能需要稍微更改您的模型):

void Main()
{
    var request = new WorkRequest();
    request.FirstName = "joe";
    request.LastName ="smith";
    var employeeRequest = RequestFactory.Create<EmployeeRequest>(request);
    System.Diagnostics.Debug.Assert(employeeRequest.FirstName == request.FirstName);
}
public class RequestFactory
{
    public static T Create<T>(Request request)
        where T : Request, new()
    {
        return (T)Activator.CreateInstance(typeof(T), request);
    }
}
public abstract class Request
{
    protected Request() {}
    protected Request(Request request)
    {
        this.FirstName = request.FirstName;
        this.LastName = request.LastName;
    }
    public string FirstName {get;set;}
    public string LastName {get;set;}
}
public class EmployeeRequest : Request
{
    public EmployeeRequest(){}
    public EmployeeRequest(Request request) 
        : base(request)
    {
    }
}   
public class WorkRequest : Request
{
    public WorkRequest(){}
    public WorkRequest(Request request) 
        : base(request)
    {
    }
}   

我个人,而不是让Factory来构建我的请求,可能会添加抽象方法request GetRequest()Request抽象类,并在每个EmployeeRequest, WorkerRequest,....中实现其具体实现

因为,基本上,至少从提供的代码来看,你要做的是:

  1. 创建EmployerRequest并分配给Request

  2. 传递给Factory的方法后,使is和创建EmployerRequest"克隆"传递给服务器。我不知道你是否真的需要这个,但假设是,在这一点上,为什么不要求对象本身创建另一种类型的本身呢?

希望我的观点清楚了。

问候。