Skip to content

Instantly share code, notes, and snippets.

@GaProgMan
Last active December 5, 2016 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GaProgMan/b9c2defc8e0fdc731ff338d8d75e744d to your computer and use it in GitHub Desktop.
Save GaProgMan/b9c2defc8e0fdc731ff338d8d75e744d to your computer and use it in GitHub Desktop.
Code snippets for the blog article /2016/12/01/class-libraries-in-net-core/
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {
"library": {
"target": "project"
}
},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
},
"imports": "dnxcore50"
}
}
}
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable"
},
"frameworks": {
"netstandard1.6": {
"dependencies": {
"NETStandard.Library": "1.6.0"
}
}
}
}
mkdir consoleAppWithClassLibrary
cd consoleAppWithClassLibrary
mkdir src
mkdir app
mkdir library
cd library
dotnet restore
dotnet build
Project library (.NETStandard,Version=v1.6) will be compiled because inputs were modified
Compiling library for .NETStandard,Version=v1.6
Compilation succeeded.
0 Warning(s)
0 Error(s)
Time elapsed 00:00:01.4420829
cd ..
cd app
dotnet restore
dotnet build
Project app (.NETCoreApp,Version=v1.0) will be compiled because inputs were modified
Compiling app for .NETCoreApp,Version=v1.0
Compilation succeeded.
0 Warning(s)
0 Error(s)
Time elapsed 00:00:01.8966733
dotnet run
Project library (.NETStandard,Version=v1.6) was previously compiled. Skipping compilation.
Project app (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
The Answer to 19 plus 23
42
{
"projects": [
"src"
]
}
namespace library
{
public class Procedure
{
public Response Addition (int left, int right)
{
return new Response
{
Key = $"The Answer to {left} plus {right}",
Value = $"{left + right}"
};
}
}
}
// Static link to the console so that we can simplfy out calls to
// Console.WriteLine later
using static System.Console;
// Our class library's namespace
using Library;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
// Create an instance of the Procedure class
// from the class library
var proc = new Procedure();
// Call the Addition method and store the response
var resp = proc.Addition(19, 23);
// Write the response data to the Console
WriteLine(resp.Key);
WriteLine(resp.Value);
}
}
}
namespace Library
{
public class Response
{
public string Key { get; set; }
public string Value { get; set; }
}
}
namespace MyApp.CommonCodeLibrary
{
public static class MyClassLibrary
{
public static string PromptUserForAnswer()
{
var userChoice = (default)int;
Console.WriteLine("Please choose from the following options:"
+ Environment.NewLine + "1. Hello, World!"
+ Environment.NewLine + "2. Goodbye, World!");
userChoice = int.Parse(Console.ReadLine());
switch(userChoice)
{
case 1:
return "Hello, World!";
case 2:
return "Goodybe, World!";
}
return String.Empty;
}
}
}
namespace MyApp.UserInterface
{
public class LoginForm
{
public void PromptUserForOptions()
{
var message = MyApp.CommonCodeLibrary.MyClassLibrary.PromptUserForAnswer();
Console.WriteLine(message);
}
}
}
namespace MyApp.ResourceManager
{
public class Resources
{
public void PromptUserForOptions()
{
var message = MyApp.CommonCodeLibrary.MyClassLibrary.PromptUserForAnswer();
Console.WriteLine(message);
}
}
}
namespace MyApp.UserInterface
{
public class LoginForm
{
public void PromptUserForOptions()
{
// what do we do if we want to call PromptUserForAnswer here?
}
}
}
namespace MyApp.ResourceManager
{
public class Resources
{
public void PromptUserForOptions()
{
// what do we do if we want to call PromptUserForAnswer here?
}
}
}
public void SomeMethod()
{
var userChoice = (default)int;
Console.WriteLine("Please choose from the following options:"
+ Environment.NewLine + "1. Hello, World!"
+ Environment.NewLine + "2. Goodbye, World!");
userChoice = int.Parse(Console.ReadLine());
switch(userChoice)
{
case 1:
Console.WriteLine("Hello, World!");
break;
case 2:
Console.WriteLine("Goodybe, World!");
}
}
public void SomeOtherMethod()
{
var userChoice = (default)int;
Console.WriteLine("Please choose from the following options:"
+ Environment.NewLine + "1. Hello, World!"
+ Environment.NewLine + "2. Goodbye, World!");
userChoice = int.Parse(Console.ReadLine());
switch(userChoice)
{
case 1:
Console.WriteLine("Hello, World!");
break;
case 2:
Console.WriteLine("Goodybe, World!");
}
}
public void PromptUserForAnswer()
{
var userChoice = (default)int;
Console.WriteLine("Please choose from the following options:"
+ Environment.NewLine + "1. Hello, World!"
+ Environment.NewLine + "2. Goodbye, World!");
userChoice = int.Parse(Console.ReadLine());
switch(userChoice)
{
case 1:
Console.WriteLine("Hello, World!");
break;
case 2:
Console.WriteLine("Goodybe, World!");
}
}
public void SomeMethod()
{
PromptUserForAnswer();
}
public void SomeOtherMethod()
{
PromptUserForAnswer();
}
// BaseController.cs
namespace dwCheckApi.Controllers
{
public class BaseController : Controller
{
}
}
// dwContext.cs
namespace dwCheckApi.DatabaseContexts
{
public class DwContext : DbContext
{
}
}
// BookService.cs
namespace dwCheckApi.Services
{
public class BookService : IBookService
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment