Best way to make methods easily callable and configurable by third party - c#

I have a solution with several self-contained classes and methods. For example, I have:
a FileDownloader class that has multiple different methods that download files based on passing in a URL or multiple URLs
a DataTransformations class that has multiple methods that transform data depending on what is necessary for a given operation
a FileWriter class that writes some data to some kind of file type or file format
etc.
I have all of these classes as .cs files under the same solution. I can consider the order of executions for some specific operation and call the methods from MAIN in the correct order and it produces the output that I expect. I will eventually, however, need to call some or all of these methods in many different configurations for several different processes and I don't know how to do that. I know how to pass in configuration through command line arguments, but even that requires the specific order and number of methods called stays the same between processes. This is not tenable because I will not need to download files in some instances and I will not need to transform data in some instances etc. I am very new to .NET development and I have not yet wrapped my head around how to truly decouple these classes from each other. Do I have to deploy a different solution for each class? I would like to just be able to say "call file downloader with these parameters" and then "perform data transformations based on these parameters" basically like steps in an execution job.

Dirty Answer, Compile as a library and then add a reference in whatever project you want to use those methods for. you can then call the methods by name (LibraryName).MethodName(Parameters). of course you will need to always have that DLL in whatever other project need access. if you have any questions on how to do this let me know.

Related

Selenium C#, Runing multiple test with predefined parameters

I am looking for a way to predefine some parameters (URL's) and then run a selenium test for each of them in C#.
Where should I specify those URL's?
How to implement reading of those parameters in my code?
[Test]
public void Test(string URLParameter)
{
driver.Navigate().GoToUrl(URLParameter);
}
And how should I run those tests?
If you are looking for a way to run the same test with a different set of test data i.e: a different URL in your case and If you are using
Either Nunit or xUnit
Please note all built-in attributes allow for data to be provided programmatically, but NUnit does not have any attributes that fetch the data from a file or other external source.
You can make use of
[TestCaseSource]
attribute
Please read more about it from Nunit Documentation
References:
Parameterized-Tests
Custom-Attributes
There are several ways to predefine and keep such test data.
You can keep it in some external file. It can be JSON, XML or plain text formatted file.
It may be kept in some resource C# class file inside the project.
It can be kept in some kind of DB etc.
Actual implementation of how to read this data will vary according to the way you decided to keep the test data, according to your project structure etc.
There are several best practices how to do that, not only one way to do that.
There are also several ways to run such tests. You can learn about these best practices on many online tutorials and other resources.
For that purpose you should use multiple different ways to keep test data.
1- Kept in the resources folder of the project for example in
properties file.
2- Kept data in DB and read from there while
needed.
3- Kept in some external sources and read/retrieve if from
there.
Note: Should use that method which will be convenient for you later on.

How to select between different Resource files in .Net?

I'm trying to figure out how to choose between two different (identically designed) Resources files in code. All of the examples I can find online are in reference to having different language specific Resource files which are chosen based on setting the culture value. That won't work for my probelm.
This is a web service which returns an image from one of several different image repository systems. Depending on a parameter passed in to the method, the service will need to access an image repository system in order to pull the image being requested. When accessing the image repository, there are a bunch of "magic string" GUID values that represent different IDs for various lookups in that system. One of the purposes of this service is to hide all of that complexity from the user. Rather than hard-code these GUIDs into the code, I have put them into a Resources file.
The problem is this: Each different image repository system has the same set of magic string IDs that need to be used. However, the actual GUID values for these magic strings are different depending on which repository you are connecting to. For example, there is a value called "GroupIDPrompt" which might be "8a48f393642a753f0164418b670a7cdf" on one system, but "63aa28c3637b58680163b25f7e5a5d96" on a different system. In code, I'd like to refer to this value as just "Resources.GroupIDPrompt" or something similar, but I need to be able to set which Resources file will be used at runtime, based on what the consumer of the service sent me.
Normally, I might solve a problem like this by using an interface, and instantiating a specific implementation of that interface based on the request. There are two reasons that doesn't work here - #1, Resource code files are generated automatically, and if I edit them to make them inherit from an interface, this will get broken everytime the file is regenerated. #2, All resource values are created to be static members, and interfaces aren't allowed to declare static members.
I could throw the Resources files away and instead build a class to expose these values, but that means re-introducing magic hard-coded strings to my code. That isn't too terrible, I suppose, but the Resource editor is really quite handy for managing and editing these values.

How do I find all methods used in solution

Whats the cleanest way to get a list of methods called from certain assemblies which are in use by a particular VS solution?
I'm about to refactor out interface to our clients code.
We supply a bunch of assemblies with 10s of classes which their C# projects consume.
I want to know if theres a simple solution to getting a list of all the classes/methods being used, so that I can create interfaces.
I've just used the VS2010 Architecture->Generate Dependancy Graph, a useful tool but its doesnt give me a list.

Output one C# class per file with protogen?

Is it possible to have protogen output multiple files (one per class) based on a single .proto file?
I'm working with a very large .proto file that outputs approx 200 classes, currently all in a single file. One of the places where I need to use the generated classes is in a highly memory constrained environment (a Windows Phone background agent).
I'd like to be able to only include the necessary classes in the assembly loaded in the constrained environment but can't easily do this when the generated classes are all in a single file. If I could have them outputted to multiple files I could only link in the ones I need in the assembly for the constrained environment.
Is there a way to have protogen output the classes in separate files? I can't see an option for this and am currently only using the umbrella-classname option.
Manually editing the generated file is not an option so if protogen can't do it, is there another commandline tool available which can split up a file containing multiple classes? (To save reinventing the wheel.)
Update
I'm using Google.ProtocolBuffers.dll an inherited decision and not easily changable.
Editing/splitting the .proto file is also not a posibility. (Unless as a custom step.)
We have an option for this in csharp_options, but it's not implemented:
// Whether to generate a single file for everything within the
// .proto file (false), or one file per message (true).
// This option is not currently honored; please log a feature
// request if you really want it.
optional bool multiple_files = 4;
Given that you'd have to remove relevant files and make sure you got all the dependencies right it sounds like it wouldn't actually save you much work over the solution I'd suggest, which is to split your proto file into separate ones You say this is "also not a possibility" but basically that's all I can suggest at the moment - why is it not a possibility?
EDIT: I've just had another idea. You could potentially run the protoc step of protogen (which is now done automatically if you don't specify otherwise) to parse the .proto file into its descriptor. Then load the descriptor in another program, mutate it as you would any other protobuf message (create a builder from it, edit the message, build) and then save the descriptor. You can then use protogen on the remaining descriptor, and generate only the classes you want...

C# code re-use via namespaces

I like to create a file full of custom functions which I have made, which I may use in another project or something. Now I don't fully understand how to go about this, normally in a language like php, you'd just create the php file and then go include("cust_lib.php") or whatever the file is called.
Now I think that the process involves the library having its own namespace, then either go using custom_lib; or custom_lib:: within the script (I don't want to get into a discussion over which is the best way to go here).
Is this right? Or should I create the library and convert it to a .dll, if so how do I go about this, what sort of syntax does a dll have inside it etc.
However if its just file within one project then I don't need to go down that route do I? I can just create the namespace and use that?
This is what I'm working for at the moment, and thought it would be something like this
namespace Custom_Lib{
~~functions to go here~~
}
However the functions have to exist within a class don't they? So that becomes something like
namespace Custom_Lib{
class custom_lib{
public string function1(string input){
return input;
}
}
}
So some help, pointers, examples would be appreciated so I can wrap my head around this
Thanks,
Psy.
(Yes I call them functions, that just comes from a long php/js etc background)
The normal approach would be to create a Class Library project, put your classes and methods in that project, making sure that those you want to expose are public. Then you add a reference to the resulting dll file in the client projects and you will have the functionality from the class library available to you.
Even if you decide to put it all into one single file, I would still recommend you to make it a class library since I imagine that will make it easier to maintain. For instance, consider the following scenarios:
You decide to put it in a file and include a copy of that file in all projects where you want to use it. Later you find a bug in the code. Now you will have a number of copies of the file in which to correct the bug.
You decide to put it in a file and include that same file in all projects. Now, if you want to change some behaviour in it, you will alter the behavior for all projects using it.
In those two cases, keeping it as a separate project will facilitate things for you:
You will have only one copy of the code to maintain
You can decide whether or not to update the dll used by a certain project when you make updates to the class library.
Regarding the syntax issues: yes all methods must exist within a class. However, if the class is merely a container of the methods, you can make it (and the methods static):
public static class CustomLib
{
public static string GetSomethingInteresting(int input)
{
// your code here...
}
}
That way you will not need to create an instance of CustomLib, but can just call the method:
string meaningOfLife = CustomLib.GetSomethingInteresting(42);
In addition to Fredrik Mörk's well-written and spot-on response, I'd add this:
Avoid creating a single class that is a kitchen-sink collection of functions/methods.
Instead, group related methods into smaller classes so that it's easier for you and consumers of your library to find the functionality they want. Also, if your library makes use of class-level variables, you can limit their scope.
Further, if you decide later on to add threading capabilities to your library, or if your library is used in a multi-threaded application, static methods will likely become a nightmare for you. This is a serious concern, and shouldn't be overlooked.
There no question here. You answered it yourself. Yes, you have to construct a class to include all helper methods. And yes, you can either compile it to a dll if you want to reuse in multiple projects it or just add the source file to the project.
Usually I declare the helper class and all functions as static to avoid initiating the class each time I use it.

Categories

Resources