This commit is contained in:
454313500@qq.com
2021-03-20 14:12:24 +08:00
parent 02146b4d05
commit 59a42ae48f
48 changed files with 1465 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\CC.Yi.DAL\CC.Yi.DAL.csproj" />
<ProjectReference Include="..\CC.Yi.IDAL\CC.Yi.IDAL.csproj" />
</ItemGroup>
<ItemGroup>
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
</ItemGroup>
<ItemGroup>
<Compile Update="T4DbSession.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>T4DbSession.tt</DependentUpon>
</Compile>
<Compile Update="T4StaticDalFactory.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>T4StaticDalFactory.tt</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Update="T4DbSession.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>T4DbSession.cs</LastGenOutput>
</None>
<None Update="T4StaticDalFactory.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>T4StaticDalFactory.cs</LastGenOutput>
</None>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,21 @@
using CC.Yi.DAL;
using CC.Yi.IDAL;
using CC.Yi.Model;
using Microsoft.EntityFrameworkCore;
using System;
namespace CC.Yi.DALFactory
{
public partial class DbSession : IDbSession
{
public int SaveChanges()
{
return DbContentFactory.GetCurrentDbContent().SaveChanges();
}
public DataContext GetDbContent()
{
return DbContentFactory.GetCurrentDbContent();
}
}
}

View File

@@ -0,0 +1,35 @@
using CC.Yi.IDAL;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace CC.Yi.DALFactory
{
public class DbSessionFactory
{
public static IDbSession GetCurrentDbSession()
{
IDbSession db = CallContext.GetData("DbSession") as IDbSession;
if (db == null)
{
db = new DbSession();
CallContext.SetData("DbSession", db);
}
return db;
}
private static class CallContext
{
static ConcurrentDictionary<string, AsyncLocal<object>> state = new ConcurrentDictionary<string, AsyncLocal<object>>();
public static void SetData(string name, object data) =>
state.GetOrAdd(name, _ => new AsyncLocal<object>()).Value = data;
public static object GetData(string name) =>
state.TryGetValue(name, out AsyncLocal<object> data) ? data.Value : null;
}
}
}

View File

@@ -0,0 +1,24 @@
using CC.Yi.DAL;
using CC.Yi.IDAL;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace CC.Yi.DALFactory
{
public partial class StaticDalFactory
{
private static class CallContext
{
static ConcurrentDictionary<string, AsyncLocal<object>> state = new ConcurrentDictionary<string, AsyncLocal<object>>();
public static void SetData(string name, object data) =>
state.GetOrAdd(name, _ => new AsyncLocal<object>()).Value = data;
public static object GetData(string name) =>
state.TryGetValue(name, out AsyncLocal<object> data) ? data.Value : null;
}
}
}

View File

@@ -0,0 +1,18 @@
using CC.Yi.DAL;
using CC.Yi.IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CC.Yi.DALFactory
{
public partial class DbSession : IDbSession
{
public IstudentDal studentDal
{
get { return StaticDalFactory.GetstudentDal(); }
}
}
}

View File

@@ -0,0 +1,36 @@
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#@ output extension=".cs" #>
<#
string solutionsPath = Host.ResolveAssemblyReference("$(SolutionDir)");//获取解决方案路径
string txt;
StreamReader sr = new StreamReader(solutionsPath+@"\T4Model\T4Model.txt");
txt=sr.ReadToEnd();
sr.Close();
string[] ModelData= txt.Split(',');
#>
using CC.Yi.DAL;
using CC.Yi.IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CC.Yi.DALFactory
{
public partial class DbSession : IDbSession
{
<# foreach(string k in ModelData){
#>
public I<#=k #>Dal <#=k #>Dal
{
get { return StaticDalFactory.Get<#=k #>Dal(); }
}
<# } #>
}
}

View File

@@ -0,0 +1,25 @@
using CC.Yi.DAL;
using CC.Yi.IDAL;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace CC.Yi.DALFactory
{
public partial class StaticDalFactory
{
public static IstudentDal GetstudentDal()
{
IstudentDal Data = CallContext.GetData("studentDal") as IstudentDal;
if (Data == null)
{
Data = new studentDal(DbSessionFactory.GetCurrentDbSession().GetDbContent());
CallContext.SetData("studentDal", Data);
}
return Data;
}
}
}

View File

@@ -0,0 +1,43 @@
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#@ output extension=".cs" #>
<#
string solutionsPath = Host.ResolveAssemblyReference("$(SolutionDir)");//获取解决方案路径
string txt;
StreamReader sr = new StreamReader(solutionsPath+@"\T4Model\T4Model.txt");
txt=sr.ReadToEnd();
sr.Close();
string[] ModelData= txt.Split(',');
#>
using CC.Yi.DAL;
using CC.Yi.IDAL;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace CC.Yi.DALFactory
{
public partial class StaticDalFactory
{
<# foreach(string k in ModelData){
#>
public static I<#=k #>Dal Get<#=k #>Dal()
{
I<#=k #>Dal Data = CallContext.GetData("<#=k #>Dal") as I<#=k #>Dal;
if (Data == null)
{
Data = new <#=k #>Dal(DbSessionFactory.GetCurrentDbSession().GetDbContent());
CallContext.SetData("<#=k #>Dal", Data);
}
return Data;
}
<# } #>
}
}