v1.0.0
This commit is contained in:
40
CC.Yi.DALFactory/CC.Yi.DALFactory.csproj
Normal file
40
CC.Yi.DALFactory/CC.Yi.DALFactory.csproj
Normal 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>
|
||||
21
CC.Yi.DALFactory/DbSession.cs
Normal file
21
CC.Yi.DALFactory/DbSession.cs
Normal 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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
35
CC.Yi.DALFactory/DbSessionFactory.cs
Normal file
35
CC.Yi.DALFactory/DbSessionFactory.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
24
CC.Yi.DALFactory/StaticDalFactory.cs
Normal file
24
CC.Yi.DALFactory/StaticDalFactory.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
18
CC.Yi.DALFactory/T4DbSession.cs
Normal file
18
CC.Yi.DALFactory/T4DbSession.cs
Normal 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(); }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
36
CC.Yi.DALFactory/T4DbSession.tt
Normal file
36
CC.Yi.DALFactory/T4DbSession.tt
Normal 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(); }
|
||||
}
|
||||
|
||||
<# } #>
|
||||
}
|
||||
}
|
||||
25
CC.Yi.DALFactory/T4StaticDalFactory.cs
Normal file
25
CC.Yi.DALFactory/T4StaticDalFactory.cs
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
43
CC.Yi.DALFactory/T4StaticDalFactory.tt
Normal file
43
CC.Yi.DALFactory/T4StaticDalFactory.tt
Normal 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;
|
||||
}
|
||||
|
||||
<# } #>
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user