第一次

master
锄头 3 months ago
parent aa910c815b
commit db903a131e

@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Chloe" Version="5.33.0" />
<PackageReference Include="Chloe.Extension" Version="5.33.0" />
<PackageReference Include="Chloe.PostgreSQL" Version="5.20.0" />
<PackageReference Include="Npgsql" Version="8.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\RfidTool\RfidTool.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,81 @@
using System.Data;
using System.Diagnostics;
using System.Text;
using Chloe.Infrastructure.Interception;
namespace DB;
public class DbCommandInterceptor : IDbCommandInterceptor
{
public void ReaderExecuting(IDbCommand command, DbCommandInterceptionContext<IDataReader> interceptionContext)
{
ToDb(command, interceptionContext.Exception);
}
public void ReaderExecuted(IDbCommand command, DbCommandInterceptionContext<IDataReader> interceptionContext)
{
ToDb(command, interceptionContext.Exception);
}
public void NonQueryExecuting(IDbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
ToDb(command, interceptionContext.Exception);
}
public void NonQueryExecuted(IDbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
ToDb(command, interceptionContext.Exception);
}
public void ScalarExecuting(IDbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
ToDb(command, interceptionContext.Exception);
}
public void ScalarExecuted(IDbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
ToDb(command, interceptionContext.Exception);
}
private void ToDb(IDbCommand command, Exception? exception)
{
#if DEBUG
Trace.WriteLine(command.CommandText);
Trace.WriteLine(AppendDbCommandInfo(command));
#endif
}
private string AppendDbCommandInfo(IDbCommand command)
{
var sb = new StringBuilder();
foreach (IDbDataParameter param in command.Parameters)
{
if (param == null)
continue;
object value;
if (param.Value == null || param.Value == DBNull.Value)
{
value = "NULL";
}
else
{
value = param.Value;
if (param.DbType == DbType.String || param.DbType == DbType.AnsiString ||
param.DbType == DbType.DateTime)
value = "'" + value + "'";
}
sb.AppendFormat("{3} {0} {1} = {2};", Enum.GetName(typeof(DbType), param.DbType), param.ParameterName,
value, Enum.GetName(typeof(ParameterDirection), param.Direction));
sb.AppendLine();
}
return sb.ToString();
}
}

@ -1,6 +1,28 @@
namespace DB; using System.Data;
using Chloe;
using Chloe.PostgreSQL;
using Npgsql;
using RfidTool;
namespace DB;
public class DbFactory public class DbFactory
{ {
public static IDbContext GetContext
{
get
{
PostgreSQLContext context = new PostgreSQLContext(() =>
{
IDbConnection conn = new NpgsqlConnection(RfidSetting.Current.Db); //需要自己引入数据库驱动
return conn;
});
context.Options.ConvertToLowercase = false;
return context;
}
}
} }

@ -0,0 +1,56 @@
using Chloe.Entity;
using Chloe.Infrastructure.Interception;
using Chloe.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace DB
{
public class MappingHelper
{
public static IEntityTypeBuilder[] FindMapsFromAssembly(Assembly assembly)
{
var typeOf_IMap = typeof(IEntityTypeBuilder);
var mapTypes = assembly.GetTypes().Where(a => a.IsClass && !a.IsAbstract && typeOf_IMap.IsAssignableFrom(a))
.ToList();
var maps = mapTypes.Select(a => (IEntityTypeBuilder)Activator.CreateInstance(a));
return maps.ToArray();
}
}
public static class DbInfo
{
public static void Init()
{
IDbCommandInterceptor interceptor = new DbCommandInterceptor();
DbInterception.Add(interceptor);
}
public static void Init(Assembly assembly)
{
Init();
var findMapsFromAssembly = MappingHelper.FindMapsFromAssembly(assembly);
DbConfiguration.UseTypeBuilders(findMapsFromAssembly);
}
public static void Init(List<Assembly> assemblys)
{
List<IEntityTypeBuilder> list = new List<IEntityTypeBuilder>();
foreach (var assembly in assemblys)
{
var findMapsFromAssembly = MappingHelper.FindMapsFromAssembly(assembly);
list.AddRange(findMapsFromAssembly);
}
DbConfiguration.UseTypeBuilders(list.ToArray());
}
}
}

@ -0,0 +1,74 @@
using Chloe.Annotations;
using Chloe.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DB
{
public abstract class SystemEntityTypeBuilder<T> : EntityTypeBuilder<T> where T : BaseChimsDb
{
protected SystemEntityTypeBuilder(string tableName)
{
this.MapTo(tableName);
this.Property(a => a.ID).IsAutoIncrement(false).IsPrimaryKey();
this.UpdateIgnore(x => x.CreateUserId);
this.UpdateIgnore(x => x.CreateDate);
this.UpdateIgnore(x => x.CreateUserName);
HasQueryFilter(x => x.IsDelete == 0 && x.IsEnable == 1);
}
}
public class BaseChimsDb
{
[Column(IsPrimaryKey = true)]
[NonAutoIncrement]
public long ID { get; set; }
/// <summary>
/// </summary>
public int IsEnable { get; set; } = 1;
/// <summary>
/// </summary>
public int IsDelete { get; set; } = 0;
/// <summary>
/// </summary>
[UpdateIgnore]
public DateTime CreateDate { get; set; } = DateTime.Now;
/// <summary>
/// </summary>
[UpdateIgnore]
public string CreateUserId { get; set; } = "";
/// <summary>
/// </summary>
[UpdateIgnore]
public string CreateUserName { get; set; } = "";
/// <summary>
/// </summary>
public DateTime LastModifyDate { get; set; } = DateTime.Now;
/// <summary>
/// </summary>
public string LastModifyUserId { get; set; } = "";
/// <summary>
/// </summary>
public string LastModifyUserName { get; set; } = "";
public int SortCode { get; set; } = 0;
}
}

@ -1,8 +1,22 @@
 
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DB", "DB\DB.csproj", "{17786C17-7536-4386-9292-B2B46E2A2ABC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RfidTool", "RfidTool\RfidTool.csproj", "{89746EE2-B3A7-4F88-989C-2AC91AFDE614}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU Release|Any CPU = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{17786C17-7536-4386-9292-B2B46E2A2ABC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{17786C17-7536-4386-9292-B2B46E2A2ABC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{17786C17-7536-4386-9292-B2B46E2A2ABC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{17786C17-7536-4386-9292-B2B46E2A2ABC}.Release|Any CPU.Build.0 = Release|Any CPU
{89746EE2-B3A7-4F88-989C-2AC91AFDE614}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{89746EE2-B3A7-4F88-989C-2AC91AFDE614}.Debug|Any CPU.Build.0 = Debug|Any CPU
{89746EE2-B3A7-4F88-989C-2AC91AFDE614}.Release|Any CPU.ActiveCfg = Release|Any CPU
{89746EE2-B3A7-4F88-989C-2AC91AFDE614}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal EndGlobal

@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("RfidTool")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("RfidTool")]
[assembly: AssemblyCopyright("Copyright © 2024")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("89746EE2-B3A7-4F88-989C-2AC91AFDE614")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

@ -0,0 +1,12 @@
using System.ComponentModel;
using NewLife.Configuration;
namespace RfidTool
{
[DisplayName("核心设置")]
[Config("Core")]
public class RfidSetting:Config<RfidSetting>
{
public string Db { get; set; } = "server=127.0.0.1;database=postgres;uid=postgres;pwd=yangwei";
}
}

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{89746EE2-B3A7-4F88-989C-2AC91AFDE614}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>RfidTool</RootNamespace>
<AssemblyName>RfidTool</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="RfidSetting.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="HslCommunication" Version="12.1.0" />
<PackageReference Include="NewLife.Core" Version="10.10.2024.902" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Loading…
Cancel
Save