Code snippets for the blog article /2016/12/01/class-libraries-in-net-core/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "1.0.0-*", | |
"buildOptions": { | |
"debugType": "portable" | |
}, | |
"frameworks": { | |
"netstandard1.6": { | |
"dependencies": { | |
"NETStandard.Library": "1.6.0" | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"projects": [ | |
"src" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | |
}; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Library | |
{ | |
public class Response | |
{ | |
public string Key { get; set; } | |
public string Value { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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