这是一个基本的 dotnet core 命令行 exe
注意,这是本文内容的松散迷你实现:https://docs.microsoft.com/en-us/dotnet/architecture/modern-web-apps-azure/common-web-application-architectures
https://github.com/granadacoder/dotnet-core-on-linux-one/tree/master/src/ConsoleOne
典型的 appsettings.json 内容
https://github.com/granadacoder/dotnet-core-on-linux-one/blob/master/src/ConsoleOne/appsettings.json
{
"ConnectionStrings": {
"MyConnectionString": "Data Source=someServer\someInstance,1433;Database=master;User Id=sa;Password=Password1#;"
},
"Logging": {
"LogLevel": {
"Default": "Information"
}
},
"AllowedHosts": "*"
}
这个顶层拥有并定义了这些值。在我的示例中,“ConsoleOne”是顶层。
我还有一个 BAL 和 DAL 层。
https://github.com/granadacoder/dotnet-core-on-linux-one/tree/master/src/Bal
https://github.com/granadacoder/dotnet-core-on-linux-one/tree/master/src/Dal
我的示例使用了 Dapper,它会查找连接字符串
https://github.com/granadacoder/dotnet-core-on-linux-one/blob/master/src/Dal/EmployeeDataLayer.cs
public class EmployeeDataLayer : IEmployeeDataLayer
{
private readonly Microsoft.Extensions.Configuration.IConfiguration config;
public EmployeeDataLayer(Microsoft.Extensions.Configuration.IConfiguration config)
{
this.config = config;
}
public IDbConnection Connection
{
get
{
string connectionString = this.config.GetConnectionString("MyConnectionString");
return new SqlConnection(connectionString);
}
}
public async Task<Employee> GetByID(int id)
{
using (IDbConnection conn = this.Connection)
{
string sql = "SELECT ID, FirstName, LastName, DateOfBirth FROM Employee WHERE ID = @ID";
sql = "SELECT TOP 1 id as ID, 'FName' + name as FirstName, 'LName' + name as LastName, crdate as DateOfBirth FROM sysobjects order by id";
conn.Open();
var result = await conn.QueryAsync<Employee>(sql, new { ID = id });
return result.FirstOrDefault();
}
}
public async Task<ICollection<Employee>> GetByDateOfBirth(DateTime dateOfBirth)
{
using (IDbConnection conn = this.Connection)
{
string sql = "SELECT ID, FirstName, LastName, DateOfBirth FROM Employee WHERE DateOfBirth = @DateOfBirth";
sql = "SELECT TOP 3 id as ID, 'FName' + name as FirstName, 'LName' + name as LastName, crdate as DateOfBirth FROM sysobjects order by id";
conn.Open();
var result = await conn.QueryAsync<Employee>(sql, new { DateOfBirth = dateOfBirth });
return result.ToList();
}
}
}
(注意,我上面的 DAL 代码并没有打到真正的后端表,这是我做的一个简单的演示)
但回到配置:
我通过将 Configuration 对象“注入”到类中来完成此操作(请参阅构造函数)
使用 dotnet-core,您通常使用内置的 IoC/DI
你可以在这里看到:
https://github.com/granadacoder/dotnet-core-on-linux-one/blob/master/src/ConsoleOne/Program.cs
private static IServiceProvider BuildDi(IConfiguration config)
{
string connectionString = config.GetConnectionString("MyConnectionString");
return new ServiceCollection()
.AddSingleton<IEmployeeManager, EmployeeManager>()
.AddTransient<IEmployeeDataLayer, EmployeeDataLayer>()
.AddLogging(loggingBuilder =>
{
// configure Logging with NLog
loggingBuilder.ClearProviders();
loggingBuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
loggingBuilder.AddNLog(config);
})
.AddSingleton<IConfiguration>(config)
.BuildServiceProvider();
}