Introduction
Nowadays, technology is growing faster and better. It often follows an agile methodology for continuous development and innovation with new versions of programming languages and frameworks. Every month or quarterly we get a new release of products and future roadmaps. It's a good sign of a growing open source community; through sharing knowledge these communities may faster solve problems and improve the quality of products.
In this blog, I’m going to share how to create NuGet packages for common functionality which is being shared & used across all type of frameworks in .NET net programming
Before we start, let’s set up the pre-requisites.
Step 1: Create an account in Nuget.org (https://www.nuget.org/)
Once you create an account, navigate to the profile and create API Keys. An API key is a token that can identify you to NuGet Gallery
API Key will look like oy0jbfbh9tmdlx3huhhalmwautb6dynebme4afcyaviz0iBefore
Step 2: Let’s create a small .Net Core class library POC with some common validation functions:
- namespace ValidationUtility
- {
-
-
-
- public static class Validation
- {
-
-
-
-
-
-
- public static bool CustomVisaCCValidate(string ccno)
- {
- return Regex.Match(ccno, @"^4[0-9]{12}(?:[0-9]{3})$").Success;
- }
-
-
-
-
-
-
- public static bool CustomMasterCardCCValidate(string ccno)
- {
- return Regex.Match(ccno, @"^5[0-9]{12}(?:[0-9]{3})$").Success;
- }
-
-
-
-
-
-
- public static bool CustomPhoneValidate(string phonenumber)
- {
- return Regex.Match(phonenumber, @"^[0-9]{10}$").Success;
- }
-
-
-
-
-
-
- public static string RemoveNonNumeric(this string phone)
- {
- return Regex.Replace(phone, @"[^0-9]+", "");
- }
-
-
-
-
-
-
- public static bool CustomDateValidate(string date)
- {
- return DateTime.TryParse(date, out DateTime dt);
- }
- }
- }
In order to make sure it will work in .Net core and .Net Standard framework, let's add the below configuration in project.
Once the changes are done, right-click on the project and click on the pack to create NuGet packages
Now that we have a NuGet package ready, let's deploy it to nuget.org
- dotnet nuget push CSharpValidationUtility.1.0.0.nupkg -k <API KEY> -s https:
Now let's check this package in NuGet portal
Ok, our package is ready and listed after indexing the NuGet packages list. Let's check and test in the client tester console application
Let's install this package and test some methods
Let's call some methods and test using .Net Core Console application:
Let's call some methods and test using .Net Framework (MVC 4.5) Console application:
Conclusion: We can create common functionalities and domain-specific logic under certain NuGet packages. Furthermore, we can deploy them as public and private NuGet repositories and use them across any frameworks. We may also create reusable libraries.