public class Service : IService
{
internal Configuration configuration;
public Response response;
public Service()
{
configuration = new Configuration();
configuration.Fetch();
}
public Response Execute(Request request)
{
switch case request.processtype
{
case ProcessType.Import:
Import import = new Import();
import.Configuration = configuration;
Response response = import.Execute(request)
case ProcessType.Export:
Export export = new Export();
export.Configuration = configuration;
Response response = export.Execute(request)
}
}
public class Import
{
public Configuration configuration;
public Response response;
public Response Execute(Request request)
{
response.AddMessage("doing something");
//some code
ImportSomething something = new ImportSomething();
something.Configuration = configuration;
something.Response = response;
response.AddMessage("doing more thing");
//more code
ImportSomethingElse somethingelse = new ImportSomethingElse();
somethingelse.Configuration = configuration;
somethingelse.Response = response;
return response;
}
public class ImportSomething
{
public Configuration configuration;
public Response response;
public Response Execute(Request request)
{
response.AddMessage("doing something");
//some code
response.AddMessage("doing more thing");
//more code
}
}
public class ImportSomethingElse
{
public Configuration configuration;
public Response response;
public Response Execute(Request request)
{
response.AddMessage("doing something");
开发者_Go百科 //some code
response.AddMessage("doing more thing");
//more code
}
}
}
public class Export
{
public Configuration configuration;
public Response Execute(Request request)
{
}
}
public class Configuration
{
List<NameValue> Items;
public void Fetch()
{
//fetch from database
Items.Add("data");
}
}
public class Response
{
List<Message> Messages;
Public string Data;
public void AddMessage()
{
}
}
}
You would notice that I'm passing the Configuration and Response objects to the classes I'm calling. I would like to know if this is right. Especially the response object I'm passing to every class is because the final response needs to include the messages from all the objects.
There is nothing really 'wrong' with your implementation. It can be combersome to pass those objects to every constructor. This is one of the things that Factories are designed to make easier if you want to go that route, create a factory, passing in the config and response objects required, and then create all your other objects from the factory, relying on it to get those objects passed in to the right places.
This could be done quite beautifully using IoC containers. But since it's pure C# question...
I'd define something like Context and use its instance (initialized only once) instead of all these separated settings:
public class CommandContext
{
public Configuration Configuration { get; protected set; }
public Request Request { get; protected set; }
public CommandContext(Configuration configuiration, ...) { ... }
}
...
public Response Execute(Request request)
{
var context = new CommandContext(configuration, request);
switch case request.processtype
{
case ProcessType.Import: return new Import().Execute(context);
case ProcessType.Export: return new Export().Execute(context);
}
}
Also, Factory pattern could help with building objects indeed reducing the count of lines even more.
精彩评论