Reputation: 47
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
Upvotes: 1
Views: 714
Reputation: 3719
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;
}
}
Upvotes: 1