开发者

Need to build a request object based on type.How would you do it?

I am confused about which pattern to use/best for my requirement.I hope you can clarify. Not sure if to use a builder or factory.

The scenario is this . Create a request object and pass it to a third party component that I have no control over

Request can be of many types -- EmployeeRequest-WorkerRequest-etc... and each request will have methods like "GetXXX" -GetYYY" etc..

Noddy example I have done as follows:

        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开发者_如何学JAVA = 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
            {}

If you were to implement it how would you do it:

Thanks


If you are just trying to fill out properties of the Request base type for types that inherit Request, make a generic method (you might need to change your model a little):

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)
    {

    }
}   


I would personally, instead of having Factory to build my request, probabbly add abstract method Request GetRequest() to Request abstract class, and implement its concrete implementation in every EmployeeRequest, WorkerRequest,....

Cause, basically, at least looking at code provided, what you do is:

  1. Create EmployerRequestand assign it to Request

  2. After pass it to Factory's method to make is and create EmployerRequest "clone" to pass to server. Don't really know if you really need that, but assuming that yes, at this point, why just don't ask the object itself create another type of itself?

Hope that was clear my opinion.

Regards.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜