Commit fe65a304 authored by Steve Smith's avatar Steve Smith

Trying out custom keys

WIP - doesn't work.
parent c70ff69e
......@@ -3,7 +3,7 @@ using CleanArchitecture.Core.SharedKernel;
namespace CleanArchitecture.Core.Entities
{
public class ToDoItem : BaseEntity
public class ToDoItem : BaseEntity<ToDoItemKey>
{
public string Title { get; set; }
public string Description { get; set; }
......@@ -15,4 +15,24 @@ namespace CleanArchitecture.Core.Entities
Events.Add(new ToDoItemCompletedEvent(this));
}
}
public struct ToDoItemKey
{
private int _id;
public ToDoItemKey(int id)
{
_id = id;
}
public static implicit operator ToDoItemKey(int value)
{
return new ToDoItemKey(value);
}
public static implicit operator int(ToDoItemKey me)
{
return me._id;
}
}
}
\ No newline at end of file
......@@ -3,7 +3,7 @@ using CleanArchitecture.Core.SharedKernel;
namespace CleanArchitecture.Core.Interfaces
{
public interface IRepository<T> where T : BaseEntity
public interface IRepository<T> where T : BaseEntity<int>
{
T GetById(int id);
List<T> List();
......
......@@ -3,9 +3,9 @@
namespace CleanArchitecture.Core.SharedKernel
{
// This can be modified to BaseEntity<TId> to support multiple key types (e.g. Guid)
public abstract class BaseEntity
public abstract class BaseEntity<TId>
{
public int Id { get; set; }
public TId Id { get; set; }
public List<BaseDomainEvent> Events = new List<BaseDomainEvent>();
}
......
......@@ -23,7 +23,7 @@ namespace CleanArchitecture.Infrastructure.Data
int result = base.SaveChanges();
// dispatch events only if save was successful
var entitiesWithEvents = ChangeTracker.Entries<BaseEntity>()
var entitiesWithEvents = ChangeTracker.Entries<BaseEntity<int>>()
.Select(e => e.Entity)
.Where(e => e.Events.Any())
.ToArray();
......
......@@ -6,7 +6,7 @@ using Microsoft.EntityFrameworkCore;
namespace CleanArchitecture.Infrastructure.Data
{
public class EfRepository<T> : IRepository<T> where T : BaseEntity
public class EfRepository<T> : IRepository<T> where T : BaseEntity<int>
{
private readonly AppDbContext _dbContext;
......
......@@ -45,7 +45,7 @@ namespace CleanArchitecture.Web
config.Scan(_ =>
{
_.AssemblyContainingType(typeof(Startup)); // Web
_.AssemblyContainingType(typeof(BaseEntity)); // Core
_.AssemblyContainingType(typeof(ValueObject)); // Core
_.Assembly("CleanArchitecture.Infrastructure"); // Infrastructure
_.WithDefaultConventions();
_.ConnectImplementationsToTypesClosing(typeof(IHandle<>));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment