mirror of https://github.com/abpframework/abp.git
16 changed files with 340 additions and 31 deletions
@ -1,17 +1,27 @@ |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using AbpDesk.Tickets.Dtos; |
|||
using Volo.Abp.Application.Services.Dtos; |
|||
using Volo.Abp.Domain.Repositories; |
|||
|
|||
namespace AbpDesk.Tickets |
|||
{ |
|||
public class TicketAppService : ITicketAppService |
|||
{ |
|||
private readonly IRepository<Ticket, int> _ticketRepository; |
|||
|
|||
public TicketAppService(IRepository<Ticket, int> ticketRepository) |
|||
{ |
|||
_ticketRepository = ticketRepository; |
|||
} |
|||
|
|||
public ListResultDto<TicketDto> GetAll() |
|||
{ |
|||
return new ListResultDto<TicketDto>(new List<TicketDto> |
|||
{ |
|||
new TicketDto {Id = 1, Title = "Ticket 1 Title", Body = "Ticket 1 Body" } |
|||
}); |
|||
var tickets = _ticketRepository |
|||
.GetAllList() |
|||
.Select(t => new TicketDto { Id = t.Id, Title = t.Title, Body = t.Body }) |
|||
.ToList(); |
|||
|
|||
return new ListResultDto<TicketDto>(tickets); |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -1,14 +0,0 @@ |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace AbpDesk.EntityFrameworkCore |
|||
{ |
|||
[DependsOn(typeof(AbpDeskDomainModule))] |
|||
public class AbpDeskEntityFrameworkCoreModule : AbpModule |
|||
{ |
|||
public override void ConfigureServices(IServiceCollection services) |
|||
{ |
|||
services.AddAssemblyOf<AbpDeskEntityFrameworkCoreModule>(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
using AbpDesk.Tickets; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp.Domain.Repositories; |
|||
using Volo.Abp.Modularity; |
|||
using Volo.Abp.Repositories.EntityFrameworkCore; |
|||
|
|||
namespace AbpDesk.EntityFrameworkCore |
|||
{ |
|||
[DependsOn(typeof(AbpDeskDomainModule))] |
|||
public class AbpDeskEntityFrameworkCoreModule : AbpModule |
|||
{ |
|||
public override void ConfigureServices(IServiceCollection services) |
|||
{ |
|||
services.AddAssemblyOf<AbpDeskEntityFrameworkCoreModule>(); |
|||
|
|||
services.AddTransient<IRepository<Ticket, int>, EfCoreRepository<AbpDeskDbContext, Ticket, int>>(); |
|||
|
|||
services.AddDbContext<AbpDeskDbContext>(options => |
|||
{ |
|||
options.UseSqlServer("Server=localhost;Database=AbpDesk;Trusted_Connection=True;"); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,38 @@ |
|||
using System; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Microsoft.EntityFrameworkCore.Infrastructure; |
|||
using Microsoft.EntityFrameworkCore.Metadata; |
|||
using Microsoft.EntityFrameworkCore.Migrations; |
|||
using AbpDesk.EntityFrameworkCore; |
|||
|
|||
namespace AbpDesk.EntityFrameworkCore.Migrations |
|||
{ |
|||
[DbContext(typeof(AbpDeskDbContext))] |
|||
[Migration("20161211123930_Ticket_Added_Data_Annotations")] |
|||
partial class Ticket_Added_Data_Annotations |
|||
{ |
|||
protected override void BuildTargetModel(ModelBuilder modelBuilder) |
|||
{ |
|||
modelBuilder |
|||
.HasAnnotation("ProductVersion", "1.1.0-rtm-22752") |
|||
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); |
|||
|
|||
modelBuilder.Entity("AbpDesk.Tickets.Ticket", b => |
|||
{ |
|||
b.Property<int>("Id") |
|||
.ValueGeneratedOnAdd(); |
|||
|
|||
b.Property<string>("Body") |
|||
.HasMaxLength(65536); |
|||
|
|||
b.Property<string>("Title") |
|||
.IsRequired() |
|||
.HasMaxLength(256); |
|||
|
|||
b.HasKey("Id"); |
|||
|
|||
b.ToTable("DskTickets"); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,30 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Microsoft.EntityFrameworkCore.Migrations; |
|||
|
|||
namespace AbpDesk.EntityFrameworkCore.Migrations |
|||
{ |
|||
public partial class Ticket_Added_Data_Annotations : Migration |
|||
{ |
|||
protected override void Up(MigrationBuilder migrationBuilder) |
|||
{ |
|||
migrationBuilder.AlterColumn<string>( |
|||
name: "Title", |
|||
table: "DskTickets", |
|||
maxLength: 256, |
|||
nullable: false, |
|||
oldClrType: typeof(string), |
|||
oldNullable: true); |
|||
} |
|||
|
|||
protected override void Down(MigrationBuilder migrationBuilder) |
|||
{ |
|||
migrationBuilder.AlterColumn<string>( |
|||
name: "Title", |
|||
table: "DskTickets", |
|||
nullable: true, |
|||
oldClrType: typeof(string), |
|||
oldMaxLength: 256); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
using Microsoft.EntityFrameworkCore; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore |
|||
{ |
|||
public class AbpDbContext<TDbContext> : DbContext |
|||
where TDbContext : DbContext |
|||
{ |
|||
public AbpDbContext(DbContextOptions<TDbContext> options) |
|||
: base(options) |
|||
{ |
|||
|
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,88 @@ |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Volo.Abp.Domain.Entities; |
|||
using Volo.Abp.Domain.Repositories; |
|||
using Volo.Abp.EntityFrameworkCore; |
|||
|
|||
namespace Volo.Abp.Repositories.EntityFrameworkCore |
|||
{ |
|||
//TODO: Override async versions
|
|||
|
|||
public class EfCoreRepository<TDbContext, TEntity, TPrimaryKey> : RepositoryBase<TEntity, TPrimaryKey> |
|||
where TDbContext : AbpDbContext<TDbContext> |
|||
where TEntity : class, IEntity<TPrimaryKey> |
|||
{ |
|||
protected virtual TDbContext DbContext { get; } |
|||
|
|||
protected virtual DbSet<TEntity> DbSet => DbContext.Set<TEntity>(); |
|||
|
|||
public EfCoreRepository(TDbContext dbContext) |
|||
{ |
|||
DbContext = dbContext; |
|||
} |
|||
|
|||
public override List<TEntity> GetAllList() |
|||
{ |
|||
return GetQueryable().ToList(); |
|||
} |
|||
|
|||
public override Task<List<TEntity>> GetAllListAsync() |
|||
{ |
|||
return GetQueryable().ToListAsync(); |
|||
} |
|||
|
|||
public override TEntity Get(TPrimaryKey id) |
|||
{ |
|||
var entity = FirstOrDefault(id); |
|||
if (entity == null) |
|||
{ |
|||
throw new EntityNotFoundException(typeof(TEntity), id); |
|||
} |
|||
|
|||
return entity; |
|||
} |
|||
|
|||
public override TEntity FirstOrDefault(TPrimaryKey id) |
|||
{ |
|||
return DbSet.Find(id); |
|||
} |
|||
|
|||
public override TEntity Insert(TEntity entity) |
|||
{ |
|||
return DbSet.Add(entity).Entity; |
|||
} |
|||
|
|||
public override TEntity Update(TEntity entity) |
|||
{ |
|||
return DbSet.Update(entity).Entity; |
|||
} |
|||
|
|||
public override void Delete(TEntity entity) |
|||
{ |
|||
DbSet.Remove(entity); |
|||
} |
|||
|
|||
public override void Delete(TPrimaryKey id) |
|||
{ |
|||
var entity = FirstOrDefault(id); |
|||
if (entity == null) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
Delete(entity); |
|||
} |
|||
|
|||
public override int Count() |
|||
{ |
|||
return GetQueryable().Count(); |
|||
} |
|||
|
|||
protected virtual IQueryable<TEntity> GetQueryable() |
|||
{ |
|||
return DbSet; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,78 @@ |
|||
using System; |
|||
|
|||
namespace Volo.Abp.Domain.Entities |
|||
{ |
|||
/// <summary>
|
|||
/// This exception is thrown if an entity excepted to be found but not found.
|
|||
/// </summary>
|
|||
//[Serializable]
|
|||
public class EntityNotFoundException : AbpException |
|||
{ |
|||
/// <summary>
|
|||
/// Type of the entity.
|
|||
/// </summary>
|
|||
public Type EntityType { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Id of the Entity.
|
|||
/// </summary>
|
|||
public object Id { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Creates a new <see cref="EntityNotFoundException"/> object.
|
|||
/// </summary>
|
|||
public EntityNotFoundException() |
|||
{ |
|||
|
|||
} |
|||
|
|||
///// <summary>
|
|||
///// Creates a new <see cref="EntityNotFoundException"/> object.
|
|||
///// </summary>
|
|||
//public EntityNotFoundException(SerializationInfo serializationInfo, StreamingContext context)
|
|||
// : base(serializationInfo, context)
|
|||
//{
|
|||
|
|||
//}
|
|||
|
|||
/// <summary>
|
|||
/// Creates a new <see cref="EntityNotFoundException"/> object.
|
|||
/// </summary>
|
|||
public EntityNotFoundException(Type entityType, object id) |
|||
: this(entityType, id, null) |
|||
{ |
|||
|
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Creates a new <see cref="EntityNotFoundException"/> object.
|
|||
/// </summary>
|
|||
public EntityNotFoundException(Type entityType, object id, Exception innerException) |
|||
: base($"There is no such an entity. Entity type: {entityType.FullName}, id: {id}", innerException) |
|||
{ |
|||
EntityType = entityType; |
|||
Id = id; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Creates a new <see cref="EntityNotFoundException"/> object.
|
|||
/// </summary>
|
|||
/// <param name="message">Exception message</param>
|
|||
public EntityNotFoundException(string message) |
|||
: base(message) |
|||
{ |
|||
|
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Creates a new <see cref="EntityNotFoundException"/> object.
|
|||
/// </summary>
|
|||
/// <param name="message">Exception message</param>
|
|||
/// <param name="innerException">Inner exception</param>
|
|||
public EntityNotFoundException(string message, Exception innerException) |
|||
: base(message, innerException) |
|||
{ |
|||
|
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue