0

i have a file which will be used across many app projects. the only difference of these files is the webservice reference name. code like this:

public void Test(){
    Kevin.ServiceReference1.Service1Client client = new Kevin.ServiceReference1.Service1Client();
    // do something....
}

like code above, the 'Kevin.ServiceReference1' will be replace by specified app project namespace. so, according to DRY(don't repeat yourself), i shouldn't just copy the file to many projects and rename the specified part manually. is there any way i can easily replace some parts of my template file to something related to the project?

Dinah
  • 52,922
  • 30
  • 133
  • 149
Narutokk
  • 964
  • 1
  • 8
  • 20
  • Do the service clients share a similar interface? – Joseph Yaduvanshi May 06 '10 at 18:36
  • they 're consuming the exact web service interface – Narutokk May 08 '10 at 04:04
  • You are already repeating yourself by having the proxy code in each project. You can avoid repeating by having a library project for proxy code only and reference the project from other projects. And yes, in this solution, the proxy code does have their own namespace. – Codism Jun 24 '10 at 13:18

3 Answers3

2

This isn't a question of DRY; while the files might look similar, *you aren't repeating yourself because the one and only operation -- the declaration and assignment of a variable -- is different for every type**.

While you might want to look into giving your classes a common parent if they share a common purpose, there's nothing in your example that suggests that the classes are, in fact, related in any way.

If you're looking for ways to automate the process to make it easier on yourself, check out T4 Templates (free from Microsoft), or PostSharp. There are many other threads on here about code generation.

Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
  • i dont agree with you adam. the logic is totally is same. and the web service these apps use is the same too, except the namespace of generated code. but i cant control the namespace of the generated service referrence code, can i? if i can, the problem may be solved using link file. – Narutokk May 08 '10 at 04:05
2

Have you looked into Microsoft's T4 code templating system ? It might be just what you need.

Links: http://msdn.microsoft.com/en-us/vstudio/cc308634.aspx
http://visualstudiomagazine.com/articles/2009/05/01/visual-studios-t4-code-generation.aspx

Dinah
  • 52,922
  • 30
  • 133
  • 149
RyBolt
  • 1,764
  • 3
  • 22
  • 34
0

If the only thing changing in the file is the namespace and class name, you can extract all of the other common functionality into a base class. Then, for each different usage, make a derived class inheriting from the base class. The derived class could just have a simple method that would return the specific namespace/class.

Paul Kearney - pk
  • 5,435
  • 26
  • 28
  • yes, that's what i've done now. but the problem is, the code is relying on the web service generated entities, which come back to the namespace problem again. – Narutokk May 08 '10 at 04:13