I am using same constants in two different project in different visual studio solutions.
Project 1 is ASP.NET WEB API and
Project 2 is client web application.
Constants example:
1. BUYING_RATE
2. THRESHOLD_LIMIT
etc
My question is.
Should I create a new library project just for handling constants and share this dll in both projects? I want to avoid
duplacate code in two different projects
Note 1: I cannot add both projects (API project and client project) in one solution due to some limitations so ignore this
Note 2: Both projects are managed by me
One more scenario is. ASP.NET WEB API will be consumed by 3rd parties as well. So should I provide the dll-s to them or API documentation is enough.
Or Am i thinking wrong about the design. If yes have a better solution, then what other options are to solve this problem
Thanks in advance
You can use a NuGet to share constants but I wouldn't. The main problem is that you will have to plan your constants beforehand or the NuGet publication will delay your progress.
When using a shared library, consider using variables instead of constants. It often happens that you need to change a constant value and you do not want to issue a full rebuild. Constant values are compiled into the assemblies referencing them. Variables get evaluated at run-time.
When making a shared library, consider providing a statically accessible instance container. It is often usable to provide different semi-constant value sets for different reader instances.
//Constant-provider assembly code. Separate library that is.
public class MyConstants
{
//Gets compiled into user code. Changing value -> rebuild all clients.
//public const double BUYING_RATE = 0.5;
//Gets evaluated in run-time. Changing value -> clients get new value.
public readonly double BUYING_RATE = 0.5;
private static MyConstants _default;
static MyConstants()
{
_default = new MyConstants();
}
//Provide default instance. You can change it in run-time when needed.
public static Default
{
get
{
return _default;
}
}
}
//Constant-user assembly code. Sample usage code for reader assemblier.
private class User
{
public void Method1()
{
... = MyConstants.Default.BUYING_RATE;
}
}
Related
At first I have a Multi-platform Project where I created a Class which transfers Data and my problem is that as example if I click a Button a method in this class should be called but I cannot reach the method.
This is my project structure:
The red part is where the Data Handler is located
the green part from where I get the clicked event and call the method.
I'll hope someone can help me with this problem!
As Jason mentioned in comment, you can not reach code from platform specific just like that, because you are not referencing platform specific projects, and there is something called DependencyService (which Jason also mentioned) and that will help you to solve this "issue" that you have.
This is how you can use DependencyService, inside your shared code project, create one interface in my case that will be:
public interface IDataHandler
{
string GetSomeStringValue();
}
Go to your iOS or other platform specific project and create new class DataHandler.cs (which you already have). And it should implement this interface that we created. Something like this:
[assembly: Dependency(typeof(DataHandler))]
namespace provisioning.ios
{
public class DataHandler: IDataHandler
{
public DataHandler()
{
}
public string GetSomeStringValue()
{
return "Some string value or whatever";
}
}
}
After that when you want to reach this method you will use DepedencyService inside of your shared code project like this:
private void SomeMethod()
{
string fromSpecificProject = DependencyService.Get<IDataHandler>().GetSomeStringValue();
}
If you want or need you can use this to pass some values to platform specific project and to return the data like I did it this mini example.
Note that implementations must be provided for each platform project
in your solution. Platform projects without implementations will fail
at runtime!
Strongly recommend you to take a look at official docs here.
Also I made this mini blogpost about usage of Dependency Service in Xamarin.Forms apps you can find it here.
I am quite new to fitnesse, I really like the ideas. But how do you do it in real life?
I have a solution that contains several dll projects in visual studio.
The projects use each other.
It's basically WPF projects so it should be relatively easy to make Fitnesse a new view that uses the viewmodels.
I think the solution here is to make a project for fitnesse, a.proj. that project will link to other assemblies that we make. lets say that b is where our business logic is and c, contains some lower level logic. In b I have my business class.
namespace b {
public class SomeBusinesslogic {
public C:SomeValue something;
public bool DoSomething(C:SomeOtherValue value1,C:Somevalue value2){
... somelogic ....
}
}
}
namespace c {
public class SomeValue{
public int a;
public int b;
}
public class SomeOtherValue{
public float c;
public string textd;
}
}
in my fitnesse wiki page how would I write the paths to include a.dll that is my fitness wrapping. and b.dll that is under test. And the c.dll that is also called through b.dll.
!path ..\xxx\bin\c.dll
!path ..\xxx\bin\b.dll
!path ..\xxx\bin\a.dll
or is there any smarter way of doing this?
thanks
See if this helps you get started http://www.asoftwarecraft.com/2011/07/starting-fitnesse-project-with-fitsharp.html
Try to use one path variable with comma-separated list.
Also there are different behaviours. FitSharp runner requires the list of the namespaces or classes which are defined in the configuration (see the project above). NetRunner plugin requires the dlls list in the path only. Then it will find all classes inherited from the BaseTestContainer class and add them to the functions containers list. Then it will union all these function to the one list and will use this list for test execution.
And important note for the configuration file: for the fitSharp you have to show the configuration file directly. For the NetRunner the configuration file will be used from the first library availble. So, for example you have this path variable:
!path a.dll, b.dll, c.dll, d.dll
b.dll and c.dll contains configuration, e.g. there is two existing files: b.dll.config and c.dll.config. And there are any configuration for the a.dll and d.dll. Then the b.dll.config will be used for the test domain.
I want to share a property between two projects in the same solution, so I created a simple static class in a separate (third) project with a static property. However, when I set it in one project, the change doesn't seem to occur when I try to get the value of the property from the other project.
Since the property is static shouldn't there only be one instance of it? I've debugged and the value is indeed set after the assignment statement, why doesn't this apply when its referenced in the other project?
Here's the code:
namespace Shared
{
public static class Shared
{
public static string old { get; set; }
}
}
Assignment statement in first project
Shared.Shared.old = messageData.Items[0].DateTime;
Trying to access property in 2nd project
if (messageData.Items[0].DateTime.CompareTo(Shared.Shared.old) > 0)
A static property has one instance per process (technically, per AppDomain). If you're trying to share it between two executables, each process will get a unique value.
If you want to communicate between two executables, you'll need to use some form of Interprocess Communication, or serialize to some external source (the file system, a database, etc).
I have a dll named ExpensiveAndLargeObfuscatedFoo.dll.
Lets says it defines a type named ExpensiveAndLargeObfuscatedFooSubClass.
It's been compiled for .NET.
Are there any tools (free, paid, whatever) that will generate c# or vb class files that will do nothing but wrap around everything defined in this expensive dll? That way I can add functionality, fix bugs (that CorpFUBAR won't fix), add logging, etc?
Literally, I want output that looks like this
namespace easytoread {
public class SubClass {
private ExpensiveAndLargeObfuscatedFoo.SubClass _originalSubClass;
public SubClass() {
this._originalSubClass = new ExpensiveAndLargeObfuscatedFoo.SubClass ();
}
public string StupidBuggyMethod(string param1,int param2) {
return _originalSubClass.StupidBuggyMethod(param1, param2);
}
}
}
It would have to handle custom return types as well as primitives
namespace easytoread {
public class SubFooClass {
private ExpensiveAndLargeObfuscatedFoo.SubFooClass _originalSubFooClass;
public SubFooClass() {
this._originalSubFooClass= new ExpensiveAndLargeObfuscatedFoo.SubFooClass ();
}
private SubFooClass(ExpensiveAndLargeObfuscatedFoo.SubFooClass orig) {
this._originalSubFooClass = orig;
}
public SubFooClass StupidBuggyMethod(string param1,int param2) {
return new SubFooClass(_originalSubFooClass.StupidBuggyMethod(param1, param2));
}
}
}
And so on and so forth for every single defined class.
Basically, poor mans dynamic proxy? (yay, Castle Project is awesome!)
We'd also like to rename some of our wrapper classes, but the tool doesn't need to do that.
Without renaming, we'd be able to replace the old assembly with our new generated one, change using statements and continue on like nothing happened (except the bugs were fixed!)
It just needs to examine the dll and do code generation. the generated code can even be VB.NET, or ironpython, or anything CLR.
This is a slippery slope and I'm not happy that I ended up here, but this seems to be the way to go. I looked at the Castle Project, but unless I'm mistaken that won't work for two reasons: 1) I can't rename anything (don't ask), 2) none of the assemblies methods are declared virtual or even overridable. Even if they were, there's hundreds of types I'd have to override manually, which doesn't sound fun.
ReSharper can do much of the work for you.
You will need to declare a basic class:
namespace easytoread {
public class SubClass {
private ExpensiveAndLargeObfuscatedFoo.SubClass _originalSubClass;
}
}
Then, choose ReSharper > Edit > Generate Code (Alt+Ins), select "Delegating Members", select all, and let it generate the code.
It won't wrap return values with custom classes (it will return the original type), so that would still have to be added manually.
It seems the best answer is "There is no such tool". So, I'll be taking a stab at writing my own later as an off-hours project. If I ever get something useful working I'll github it and update here.
UPDATE
Visual Studio 2012 Fakes seem to be promising. http://msdn.microsoft.com/en-us/library/tfs/hh549175(v=vs.110).aspx - we've moved on but I might try creating a fake and then dropping it in as a replacement dll sometime in the future
If you have access to the source code, rename and fix in the source
code.
If you don't have access (and you can do it legally) use some
tool like Reflector or dotPeek to get the source code and then,
goto to the first point.
I have to be able to connect to two different versions of the an API (1.4 and 1.5), lets call it the Foo API. And my code that connects to the API and processes the results is substantially duplicated - the only difference is the data types returned from the two APIs. How can I refactor this to remove duplication?
In Foo14Connector.cs (my own class that calls the 1.4 API)
public class Foo14Connector
{
public void GetAllCustomers()
{
var _foo = new Foo14WebReference.FooService();
Foo14WebReference.customerEntity[] customers = _foo.getCustomerList;
foreach (Foo14WebReference.customerEntity customer in customers)
{
GetSingleCustomer(customer);
}
}
public void GetSingleCustomer(Foo14WebReference.customerEntity customer)
{
var id = customer.foo_id;
// etc
}
}
And in the almost exact duplicate class Foo15Connector.cs (my own class that calls the 1.5 API)
public class Foo15Connector
{
public void GetAllCustomers()
{
var _foo = new Foo15WebReference.FooService();
Foo15WebReference.customerEntity[] customers = _foo.getCustomerList;
foreach (Foo15WebReference.customerEntity customer in customers)
{
GetSingleCustomer(customer);
}
}
public void GetSingleCustomer(Foo15WebReference.customerEntity customer)
{
var id = customer.foo_id;
// etc
}
}
Note that I have to have two different connectors because one single method call (out of hundreds) on the API has a new parameter in 1.5.
Both classes Foo14WebReference.customerEntity and Foo15WebReference.customerEntity have identical properties.
If the connectors are in different projects, this is an easy situation to solve:
Add a new class file, call it ConnectorCommon and copy all of the common code, but with the namespaces removed. Make this class a partial class and rename the class (not the file) to something like Connector.
You will need to add a link to this to each project.
Next, remove all of the code from your current connector classes, rename the class (not necessarily the file) to the same as the partial class, and add a using statement that references the namespace.
This should get what you are looking for.
So, when you are done you will have:
File ConnectorCommon:
public partial class Connector
{
public void GetAllCustomers()
{
var _foo = new FooService();
customerEntity[] customers = _foo.getCustomerList;
foreach (customerEntity customer in customers)
{
GetSingleCustomer(customer);
}
}
public void GetSingleCustomer(customerEntity customer)
{
var id = customer.foo_id;
// etc
}
}
File Magento15Connector
using Foo15WebReference;
partial class Connector
{
}
File Magento14Connector
using Foo14WebReference;
partial class Connector
{
}
Update
This process can be a little confusing at first.
To clarify, you are sharing source code in a common file between two projects.
The actual classes are the specific classes with the namespaces in each project. You use the partial keyword to cause the common file to be combined with the actual project file (i.e. Magneto14) in each project to create the full class within that project at compile time.
The trickiest part is adding the common file to both projects.
To do this, select the Add Existing Item... menu in the second project, navigate to the common file and click the right-arrow next to the Add button.
From the dropdown menu, select Add as link. This will add a reference to the file to the second project. The source code will be included in both projects and any changes to the common file will be automatically available in both projects.
Update 2
I sometimes forget how easy VB makes tasks like this, since that is my ordinary programming environment.
In order to make this work in C#, there is one more trick that has to be employed: Conditional compilation symbols. It makes the start of the common code a little more verbose than I would like, but it still ensures that you can work with a single set of common code.
To employ this trick, add a conditional compilation symbol to each project (ensure that it is set for All Configurations). For example, in the Magento14 project, add Ver14 and in the Magento15 project add Ver15.
Then in the common file, replace the namespace with a structure similar to the following:
#if Ver14
using Magneto14;
namespace Magento14Project
#elif Ver15
using Magneto15;
namespace Magento15Project
#endif
This will ensure that the proper namespace and usings are included based on the project the common code is being compiled into.
Note that all common using statements should be retained in the common file (i.e., enough to get it to compile).
If the FooConnectors are not sealed and you are in control to create new instances, then you can derive your own connectors and implement interfaces at the same time. In c# you can implement members by simply inheriting them from a base class!
public IFooConnector {
void GetAllCustomers();
}
public MyFoo14Connector : Foo14Connector, IFooConnector
{
// No need to put any code in here!
}
and then
IFooConnector connector = new MyFoo14Connector();
connector.GetAllCustomers();
You should introduce an interface that is common to both of the implementations. If the projects are written in the same language and are in different projects, you can introduce a common project that both projects reference. You are then making a move towards having dependencies only on your interface which should allow you to swap in different implementations behind the scenes somewhere using inversion of control (google, dependency injection or service locator or factory pattern).
Difficulties for you could be:
1) Public static methods in the implementations are not able to be exposed staticly via an interface
2) Potentially have code in one implementation class ie Foo14Connector or Foo15Connector that doesnt make sense to put into a generic interface