Skip to content

Commit

Permalink
同步未提交项
Browse files Browse the repository at this point in the history
  • Loading branch information
Halifa committed Jun 28, 2017
1 parent 8ee426a commit e49dcfe
Show file tree
Hide file tree
Showing 8 changed files with 440 additions and 0 deletions.
13 changes: 13 additions & 0 deletions TonyBlogs.IRepository/IBlogArticleRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TonyBlogs.Entity;

namespace TonyBlogs.IRepository
{
public interface IBlogArticleRepository : IBaseRepository<BlogArticle>
{

}
}
64 changes: 64 additions & 0 deletions TonyBlogs.IRepository/TonyBlogs.IRepository.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.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>{D6CF2021-B095-4103-9D1C-294E19C9EB4F}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TonyBlogs.IRepository</RootNamespace>
<AssemblyName>TonyBlogs.IRepository</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<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' ">
<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.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="IBaseRepository.cs" />
<Compile Include="IBlogArticleRepository.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TonyBlogs.Entity\TonyBlogs.Entity.csproj">
<Project>{cadabaec-616d-4135-8337-03d8efbfcb9e}</Project>
<Name>TonyBlogs.Entity</Name>
</ProjectReference>
<ProjectReference Include="..\TonyBlogs.Framework\TonyBlogs.Framework.csproj">
<Project>{943df536-760c-40b0-b3fc-3ec3e29a2c83}</Project>
<Name>TonyBlogs.Framework</Name>
</ProjectReference>
</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>
48 changes: 48 additions & 0 deletions TonyBlogs.IService/IBaseService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using TonyBlogs.Framework;

namespace TonyBlogs.IService
{
public interface IBaseServices<TEntity> : IDependency where TEntity : class
{
#region 查询
/// <summary>
/// 单表查询
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
List<TEntity> QueryWhere(Expression<Func<TEntity, bool>> predicate);

TEntity Single(Expression<Func<TEntity, bool>> predicate);
#endregion

#region 编辑
/// <summary>
/// 通过传入的model加需要修改的数据
/// </summary>
/// <param name="model"></param>
/// <param name="propertys"></param>
void Update(TEntity model, Expression<Func<TEntity, bool>> where);

/// <summary>
/// 修改指定字段
/// </summary>
/// <param name="model"></param>
void UpdateOnly<TKey>(TEntity model, Expression<Func<TEntity, TKey>> onlyFields, Expression<Func<TEntity, bool>> where);
#endregion

#region 删除
void Delete(Expression<Func<TEntity, bool>> predicate);
#endregion

#region 新增
void Add(TEntity model);

long Add(TEntity model, bool selectIdentity);
#endregion
}
}
12 changes: 12 additions & 0 deletions TonyBlogs.IService/IBlogArticleService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TonyBlogs.Entity;

namespace TonyBlogs.IService
{
public interface IBlogArticleService : IBaseServices<BlogArticle>
{
}
}
64 changes: 64 additions & 0 deletions TonyBlogs.IService/TonyBlogs.IService.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.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>{2DEC0442-EFAA-4C3B-B610-21A95099802F}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TonyBlogs.IService</RootNamespace>
<AssemblyName>TonyBlogs.IService</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<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' ">
<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.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="IBaseService.cs" />
<Compile Include="IBlogArticleService.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TonyBlogs.Entity\TonyBlogs.Entity.csproj">
<Project>{cadabaec-616d-4135-8337-03d8efbfcb9e}</Project>
<Name>TonyBlogs.Entity</Name>
</ProjectReference>
<ProjectReference Include="..\TonyBlogs.Framework\TonyBlogs.Framework.csproj">
<Project>{943df536-760c-40b0-b3fc-3ec3e29a2c83}</Project>
<Name>TonyBlogs.Framework</Name>
</ProjectReference>
</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>
153 changes: 153 additions & 0 deletions TonyBlogs.Repository/BaseRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
using ServiceStack.Data;
using ServiceStack.OrmLite;
using ServiceStack.OrmLite.MySql;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Remoting.Messaging;
using System.Text;
using TonyBlogs.IRepository;

namespace TonyBlogs.Repository
{
public class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : class
{
private static string connStr = "server=localhost;port=3306;User Id=root;pwd=123456;Database=tony_blogs";
private static IDbConnectionFactory connFactory = new OrmLiteConnectionFactory(connStr, MySqlDialect.Provider);

protected IDbConnection db
{
get
{
//先从线程缓存CallContext中根据key查找EF容器对象,如果没有则创建,同时保存到缓存中
object obj = CallContext.GetData(typeof(IDbConnection).FullName);
if (obj == null)
{
//例化EF的上下文容器对象
obj = connFactory.Open();
//将EF的上下文容器对象存入线程缓存CallContext中
CallContext.SetData(typeof(IDbConnection).FullName, obj);
}
//将当前的EF上下文对象返回
return obj as IDbConnection;

}
}

#region 查询
/// <summary>
/// 单表查询
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
public List<TEntity> QueryWhere(Expression<Func<TEntity, bool>> predicate)
{
return ExecRead(conn => conn.Select(predicate));
}

public TEntity Single(Expression<Func<TEntity, bool>> predicate)
{
return ExecRead(conn => conn.Single(predicate));
}
#endregion

#region 编辑
/// <summary>
/// 通过传入的model加需要修改的数据
/// </summary>
/// <param name="model"></param>
/// <param name="propertys"></param>
public void Update(TEntity model, Expression<Func<TEntity, bool>> where)
{
ExecWrite(conn => conn.Update(model, where));
}

/// <summary>
/// 修改指定字段
/// </summary>
/// <param name="model"></param>
public void UpdateOnly<TKey>(TEntity model, Expression<Func<TEntity, TKey>> onlyFields, Expression<Func<TEntity, bool>> where)
{
ExecWrite(conn => conn.UpdateOnly(model, onlyFields, where));
}
#endregion

#region 删除
public void Delete(Expression<Func<TEntity, bool>> predicate)
{
ExecWrite(conn => conn.Delete(predicate));
}
#endregion

#region 新增
public void Add(TEntity model)
{
this.Add(model, false);
}

public long Add(TEntity model, bool selectIdentity)
{
return ExecWrite(conn=> conn.Insert(model, selectIdentity));
}
#endregion


private T ExecWrite<T>(Func<IDbConnection, T> func, IDbConnection connection = null)
{
if (connection == null)
{
connection = db;
}

try
{
var result = func(connection);

return result;
}
catch (Exception ex)
{
throw ex;
}
finally
{
CloseConnection(connection);
}
}

private T ExecRead<T>(Func<IDbConnection, T> func, IDbConnection connection = null)
{
if (connection == null)
{
connection = db;
}

try
{
var result = func(connection);

return result;
}
catch (Exception ex)
{
throw;
}
finally
{
CloseConnection(connection);
}
}

/// <summary>
/// 关闭数据库连接
/// </summary>
/// <param name="connection"></param>
/// <param name="IsReadConnection">是否读链接 默认不是</param>
public void CloseConnection(IDbConnection connection)
{
connection.Close();
}
}
}
14 changes: 14 additions & 0 deletions TonyBlogs.Repository/BlogArticleRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TonyBlogs.Entity;
using TonyBlogs.IRepository;

namespace TonyBlogs.Repository
{
public class BlogArticleRepository : BaseRepository<BlogArticle>, IBlogArticleRepository
{

}
}
Loading

0 comments on commit e49dcfe

Please sign in to comment.