I have a simple project (ABP version: 3.1.2, Database: EF Core).
I run GetAsync:
var author = await _authorRepository.GetAsync(id, includeDetails: true);
But author.Films was not included. What may I have forgotten?
Author (AggregateRoot):
public class Author : FullAuditedAggregateRoot<Guid>
{
public string Name { get; private set; }
public DateTime BirthDate { get; set; }
public string ShortBio { get; set; }
public List<Film> Films { get; set; }
private Author()
{
Films = new List<Film>();
/* This constructor is for deserialization / ORM purpose */
}
internal Author(
Guid id,
[NotNull] string name,
DateTime birthDate,
[CanBeNull] string shortBio = null)
: base(id)
{
Name = name;
BirthDate = birthDate;
ShortBio = shortBio;
Films = new List<Film>();
}
}
Film (Entity):
public class Film : Entity<Guid>
{
public virtual Guid AuthorId { get; internal set; }
public string Name { get; set; }
}
SeedAsync in DataSeeder class (I checked whether data exists in database after DbMigrator ran, there are these data in tables as expected):
public async Task SeedAsync(DataSeedContext context)
{
if (await _authorRepository.GetCountAsync() == 0)
{
var authorId = _guidGenerator.Create();
await _authorRepository.InsertAsync(
new Author(authorId, "J. R. R. Tolkien", DateTime.Now.AddYears(-60), "bio1"),
autoSave: true
);
await _filmRepository.InsertAsync(
new Film { AuthorId = authorId, Name = "The Return of the King1" },
autoSave: true);
await _filmRepository.InsertAsync(
new Film { AuthorId = authorId, Name = "The Return of the King2" },
autoSave: true);
await _filmRepository.InsertAsync(
new Film { AuthorId = authorId, Name = "The Return of the King3" },
autoSave: true);
}
}
AuthorAppService:
public class AuthorAppService : BookStoreAppService, IAuthorAppService
{
private readonly IAuthorRepository _authorRepository;
private readonly AuthorManager _authorManager;
public AuthorAppService(
IAuthorRepository authorRepository,
AuthorManager authorManager)
{
_authorRepository = authorRepository;
_authorManager = authorManager;
}
public async Task<AuthorDto> GetAsync(Guid id)
{
var author = await _authorRepository.GetAsync(id, includeDetails: true);
return ObjectMapper.Map<Author, AuthorDto>(author);
}
}
From https://docs.abp.io/en/abp/latest/Best-Practices/Entity-Framework-Core-Integration:
Do create a IncludeDetails extension method for the IQueryable<TEntity> for each aggregate root which has sub collections.
...
Do override WithDetails method of the repository for aggregates root which have sub collections.
public static class AuthorEfCoreQueryableExtensions
{
public static IQueryable<Author> IncludeDetails(this IQueryable<Author> queryable, bool include = true)
{
if (!include)
{
return queryable;
}
return queryable
.Include(x => x.Films);
}
}
public class AuthorRepository : EfCoreRepository<IMyDbContext, Author, Guid>, IAuthorRepository
{
...
public override IQueryable<Author> WithDetails()
{
return GetQueryable().IncludeDetails(); // Uses the extension method defined above
}
}
Related
I am getting the following error when try and add a new Author object to the AuthorsDbSet and unsure of how to resolve it.
{"No coercion operator is defined between types
'System.Collections.Generic.List1[Persistence.Entities.Book]' and 'System.Collections.ObjectModel.ReadOnlyCollection1[Persistence.Entities.Book]'."}
Method that causes exception when adding entity to db set:
public async Task<Guid> CreateAuthorAsync(string name)
{
ArgValidator.ThrowIfNullEmptyOrWhiteSpace(name);
using var context = _dbContextFactory.CreateDbContext();
var author = Author.Create(name);
context.Authors.Add(author);//this line throws the exception
await context.SaveChangesAsync();
return author.Id;
}
Author Class
public class Author
{
public Guid Id { get; }
public string Name { get; }
public ReadOnlyCollection<Book> Books => _books.AsReadOnly();
private List<Book> _books = new List<Book>();
private Author(Guid id, string name)
{
Id = id;
Name = name;
}
public static Author Create(string name)
{
ArgValidator.ThrowIfNullEmptyOrWhiteSpace(name);
return new Author(Guid.NewGuid(), name);
}
}
Book Class
public class Book
{
public Guid Id { get; }
public string Title { get; }
public string Description { get; }
public Author Author { get; }
public Guid AuthorId { get; }
public bool Available { get; private set; }
private Book(Guid id, string title, string description, bool available, Guid authorId)
{
Id = id;
Title = title;
Description = description;
Available = available;
AuthorId = authorId;
}
public static Book Create(string title, string description, Guid authorId)
{
ArgValidator.ThrowIfNullEmptyOrWhiteSpace(title);
description = description ?? string.Empty;
return new Book(Guid.NewGuid(), title, description, true, authorId);
}
DbContext
public class LibraryContext: DbContext
{
public DbSet<Book> Books { get; set; }
public DbSet<Author> Authors { get; set; }
public LibraryContext(DbContextOptions<LibraryContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Book>(e =>
{
e.ToTable("Books", b => b.IsTemporal());
e.HasKey(x => x.Id);
e.Property(x => x.Title).IsRequired();
e.Property(x => x.Description).IsRequired();
e.Property(x => x.Available).IsRequired().HasDefaultValue(false);
e.HasOne(book => book.Author).WithMany(author => author.Books).HasForeignKey(book => book.AuthorId);
});
modelBuilder.Entity<Author>(e =>
{
e.ToTable("Authors", a => a.IsTemporal());
e.HasKey(x => x.Id);
e.Property(x => x.Name).IsRequired();
e.HasMany(author => author.Books).WithOne(book => book.Author);
//not explicitly configuring field as there is no proper fluent api method for it.
//https://stackoverflow.com/questions/60617430/ef-core-3-configure-backing-field-of-navigation-property
//simply allow it to be pickedup in the background by convention.
});
}
I'm not sure if it is as a result of the the onModelCreating code not being correct or something else. Perhaps I need to define some sort of coercion operator for List and Readonly collection, how would I do that?
Resolved this by changing to IList and IReadonlyCollection. Leaving question up as I couldn't find similar issue when searching.
public class Author
{
public Guid Id { get; }
public string Name { get; }
public IReadOnlyCollection<Book> Books => (_books as List<Book>).AsReadOnly();
private IList<Book> _books = new List<Book>();
private Author(Guid id, string name)
{
Id = id;
Name = name;
}
public static Author Create(string name)
{
ArgValidator.ThrowIfNullEmptyOrWhiteSpace(name);
return new Author(Guid.NewGuid(), name);
}
}
I will try to explain my problem as thoroughly as possible with a simplified example. Please note that I am NOT using Entity Framework.
I have this model:
public class Person
{
[Key]
public Guid Id { get; set; }
public string GivenName { get; set; }
public string FamilyName { get; set; }
public List<Employment> Employments { get; set; }
}
public class Employment
{
public string Role { get; set; }
public Guid? ManagerId { get; set; }
public virtual Person Manager { get; set; }
}
I create an in-memory data source:
public class MyDataSource
{
private static MyDataSource instance = null;
public static MyDataSource Instance
{
get
{
if (instance == null)
{
instance = new MyDataSource();
}
return instance;
}
}
public List<Person> Persons { get; set; }
private MyDataSource()
{
this.Persons = new List<Person>();
this.Persons.AddRange(new List<Person>
{
new Person()
{
Id = Guid.Parse("00000000-0000-0000-0000-000000000001"), //Just for simplicity
GivenName = "John",
FamilyName = "Doe",
Employments = new List<Employment>()
{
new Employment()
{
Role = "Boss"
}
}
},
new Person()
{
Id = Guid.Parse("00000000-0000-0000-0000-000000000002"), //Just for simplicity
GivenName = "Clark",
FamilyName = "Kent",
Employments = new List<Employment>()
{
new Employment()
{
Role = "Worker",
ManagerId = Guid.Parse("00000000-0000-0000-0000-000000000001"), //Just for simplicity
}
}
}
});
}
}
I have this controller:
[EnableQuery]
public class PersonsController : ODataController
{
public IHttpActionResult Get()
{
return Ok(MyDataSource.Instance.Persons)
}
}
I configure the EndPoint:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapODataServiceRoute("ODataRoute", "odata", CreateEdmModel());
config.Select().Expand().Filter().OrderBy().MaxTop(null).Count()
}
public static IEdmModel CreateEdmModel()
{
var builder = new ODataConventionModelBuilder();
var persons = builder.EntitySet<Person>("Persons");
builder.ComplexType<Employment>().HasOptional(e => e.Manager, (e, p) => e.ManagerId == p.Id);
return builder.GetEdmModel();
}
}
Checking the $metadata I see this:
<NavigationProperty Name="Manager" Type = "MyNamespace.Person">
<ReferentialConstraint Property="ManagerId" ReferenceProperty="Id" />
</NavigationProperty
Everything looks fine from what I can tell but:
https://example.com/odata/persons?$expand=Employments/Manager
receives everything fine but:
Manager is null for both persons. I was expecting to see John Doe on Clark Kents employment.
What am I missing?
I have solved it myself.
I realised that it doesn't work like I thought and that I have to add a reference to the manager directly in MyDataSource. After that it works to $expand the manager.
I have my models setup like this...
public class Model1 : IEquatable<Model1>
{
public int Model1Id { get; set; }
public string Name1 { get; set; }
public Model2 Model2 { get; set; }
public int Model2Id { get; set; }
public bool Equals(Model1 other)
{
return this.Model2.Equals(other.Model2)
&& this.Name1 == other.Name1;
}
}
public class Model2 : IEquatable<Model2>
{
public int Model2Id { get; set; }
public string Name2 { get; set; }
public bool Equals(Model2 other)
{
return this.Name2 == other.Name2;
}
}
public class ModelContext : DbContext
{
public DbSet<Model1> Model1 { get; set; }
public DbSet<Model2> Model2 { get; set; }
public ModelContext(DbContextOptions<ModelContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Model1>(b =>
{
b.HasOne(m1 => m1.Model2).WithMany().HasForeignKey(m1 => m1.Model2Id);
});
}
}
then I get a null reference exception when I do this...
static void Main(string[] args)
{
var myModel1 = new Model1
{
Name1 = "myName1",
Model2 = new Model2
{
Name2 = "myName2"
}
};
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
try
{
var options = new DbContextOptionsBuilder<ModelContext>()
.UseSqlite(connection)
.Options;
//create database
using(var ctx = new ModelContext(options))
{
ctx.Database.EnsureCreated();
}
//add model objects
using (var ctx = new ModelContext(options))
{
ctx.Database.EnsureCreated();
ctx.Model1.Add(myModel1);
ctx.SaveChanges();
}
//check if exists
using(var ctx = new ModelContext(options))
{
//exception here
bool isExists = ctx.Model1.Include(m1 => m1.Model2).Contains(myModel1);
Console.WriteLine(isExists);
}
}
finally
{
connection.Close();
}
Console.ReadKey();
}
I'm expeting the Model2 instance of my m1 to be populated when I call the Include but it is still null.
but If I add AsEnumerable() to my query like..
ctx.Model1.Include(m1 => m1.Model2).AsEnumerable().Contains(model1);
then everything works fine.
EDIT:
my question is... why do I need to call AsEnumerable()? I was expecting it to work without calling AsEnumerable()..
The difference is one is an entityframe work call the other is linq to objects
Entity Framework Does not understand contains for a CLR Object
public void AddIfNotExists(Model1 model1)
{
//No Need for the include this is executed in sql, assuming the model 2
//property has already been included in your model1 this should work fine
if(false == _context.Model1.Any(x => x.Name1 == model1.Name1
&& x.Model2.Name2 == model1.Model2.Name2))
{
_context.Model1.Add(model1);
}
}
I made this based off of your logic, but chances are you really just want to check if model1.id is the the model1 set. But I have no Idea what your architecture is doing so this is what you probably want
Environment:
I am working in Webapi. There is 2 entity classes which are follows;
public class Class1
{
public Class1()
{
this.items = new HashSet<Class2>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Class2> items { get; set; }
}
public class Class2
{
public int Id { get; set; }
public string Name { get; set; }
public int Class1Id { get; set; }
public virtual Class1 class1 { get; set; }
}
Business Layer:
The buniess layer have the following codes;
public class Class1Logic : IClass1Logic
{
private readonly IClass1Repository _repo;
public Class1Logic(IClass1Repository repository)
{
_repo = repository;
}
public async Task<bool> AddClass1ItemAsync(Class1 item)
{
_repo.Add(item);
bool status = await _repo.SaveAsync();
return status;
}
public async Task<Class1> GetClass1ItemAsync(int id)
{
return await _repo.GetAsync(id);
}
}
public class Class2Logic : IClass1Logic
{
private readonly IClass2Repository _repo;
public Class2Logic(IClass2Repository repository)
{
_repo = repository;
}
public async Task<bool> AddClass2ItemAsync(Class2 item)
{
_repo.Add(item);
bool status = await _repo.SaveAsync();
return status;
}
public async Task<Class2> GetClass2ItemAsync(int id)
{
return await _repo.GetAsync(id);
}
}
ViewModels:
public class Class1Model
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Class2Model
{
public int Id { get; internal set; }
public string Name { get; set; }
public int Class1Id { get; set; }
public string Class1Name { get; internal set; }
}
Controllers:
There are 2 contrtollers like Class1Controller and Class2Controller. Both have all CRUD operations.
[RoutePrefix("api/class1items")]
public class Class1Controller : ApiController
{
private readonly IClass1Logic _class1Logic;
private ModelFactory TheFactory;
public Class1Controller(IClass1Logic class1Logic)
{
_class1Logic = class1Logic;
TheFactory = new ModelFactory();
}
[Route("")]
public async Task<IHttpActionResult> Post(Class1Model class1Model)
{
var item = TheFactory.Parse(class1Model);
bool result = await _class1Logic.AddClassItemAsync(item);
if (!result)
{
return BadRequest("Error");
}
string uri = Url.Link("GetLabById", new { id = item.Id });
return Created(uri, TheFactory.Create(item));
}
[Route("{id:int}", Name = "GetClass1ItemById")]
public async Task<IHttpActionResult> GetClass1Item(int id)
{
Class1 item = await _class1Logic.GetClassItemAsync(id);
if (item == null)
{
return NotFound();
}
return Ok(TheFactory.Create(item));
}
}
[RoutePrefix("api/class2items")]
public class Class2Controller : ApiController
{
private readonly IClass2Logic _class2Logic;
private ModelFactory TheFactory;
public Class2Controller(IClass2Logic class2Logic)
{
_class2Logic = class2Logic;
TheFactory = new ModelFactory();
}
[Route("")]
public async Task<IHttpActionResult> Post(Class2Model class2Model)
{
var item = TheFactory.Parse(class2Model);
***//Here item should include Class1 object even if user give ClassId in class2Model***
bool result = await _class2Logic.AddClassItemAsync(item);
if (!result)
{
return BadRequest("Error");
}
string uri = Url.Link("GetClass2ItemById", new { id = item.Id });
return Created(uri, TheFactory.Create(item));
}
}
There is not dependecies in Class1. So all operations are fine. In Class2Controller post method, I got the model object as following to create Class2.
{
"id": 0,
"name": "string",
"class1Id": 1
}
Understanding:
I need to return this viewmodel to user after the create the record. The record created successfully but when mapping to viewmodel i got null exception as Class1 object not in the Class2 object.
In order to get the Class2 object including class1 object, I need to give the class1Object in the request object.
For this i need to find the Class1 object with Class1Id in the request object.
ViewMapper Code:
public class ModelFactory
{
public Class1Model Create(Class1 item)
{
return new Class1Model
{
Id = item.Id,
Name = item.Name
};
}
public Class2Model Create(Class2 item)
{
return new Class2Model
{
Id = item.Id,
Name = item.Name,
Class1Id = item.class1.Id,
Class1Name = item.class1.Name
};
}
public Class1 Parse(Class1Model modelItem)
{
return new Class1
{
Id = modelItem.Id,
Name = modelItem.Name
};
}
public Class2 Parse(Class2Model modelItem)
{
return new Class2
{
Id = modelItem.Id,
Name = modelItem.Name,
Class1Id = modelItem.Class1Id,
***/*Issue Place*/
//class1 = Need to set property by getting object using modelItem.Class1Id***
};
}
}
Issue:
Now i need to call get method of Class1Controller by passing Class1Id.
How to call and is this correct? or my design is bad?
This is initial case. If my Class3 have both Class1 and Class2 again i need to call methods of Class1 and Class2.
Please help to find the correct solution in this case
Note: I added comments the issue area to understand
Well, just to fix this issue you need to manually call _class1Logic.GetClass1ItemAsync after saving. However this doesn't look good.
More elegant ways to fix it:
1) If you always need Class2.Class1 field to be filled use Include when you fetch data (in repository): dbContext.Set<Class2>().Include(c => c.class1).
2) Also you can turn on LazyLoading for EF - I assume it should work in your case.
3) Inject class1Repo to class2Logic and fix up class1 reference after saving - in case if you don't want to enable lazy loading or item was detached from context after save method
Thoughts about design:
I suggest you to look at Automapper or simular libraries instead of ModelFactory where you going to have all mapping logic
Edit: About generic repository: you can modify you GetAsync method
public async Task<T> GetAsync<T>(int id, params Expression<Func<T, object>>[] includes)
where T: class, IEntity
{
var query = context.Set<T>().AsQueryable();
if (includes.Length > 0)
{
query = includes.Aggregate(query,
(current, include) => current.Include(include));
}
return await query.FirstOrDefaultAsync(x => x.Id == id);
}
IEntity interface:
interface IEntity
{
int Id { get; }
}
With this implementation you can use
await _repo.GetAsync<Class2>(id, x => x.class1);
I just added dependency inject into my application, and would like to run mock test on my methods to ensure they are working properly.
I have a domain class Person with the following properties:
public class Person : DomainBase
{
public string FirstName { get; set; }
public string LastName { get; set; }
public char Gender { get; set; }
public DateTime DOB {get; set; }
}
I have a partial manager class that is shared with other domain type mangers:
public partial class Manager : IManager1
{
private IHiveTiesContext _ctx;
public Manager(IHiveTiesContext context)
{
_ctx = context;
}
}
The interface IManager1 was extracted from my PersonManager class and contains all need methods.
My PersonManager CreatePerson() method is being called and tested:
public partial class Manager : IManager1
{
public Person CreatePerson(string fn, string ln, DateTime dob, char gender, Guid RId)
{
var _person =_ctx.People.Add(new Person
{
FirstName = fn,
LastName = ln,
DOB = dob,
Gender = gender,
RowId = RId});
_ctx.SaveChanges();
return _person;
}
}
}
My context follows and once again I extracted an interface IHiveTiesContext from it.
public class HiveTiesContext : DbContext, IHiveTiesContext
{
public HiveTiesContext() : base("hiveties")
{
Database.SetInitializer<HiveTiesContext>(null);
}
public virtual IDbSet<Person> People { get; set; }
}
Finally I am trying to test CreatePerson like this:
public class PersonManagerMockTest
{
private static Guid personGuid;
[ClassInitialize]
public static void Init(TestContext test)
{
personGuid = Guid.NewGuid();
}
[TestMethod]
public void AddNewPerson()
{
//Arrange
var mockDbSet = new Mock<DbSet<Person>>();
var mockContext = new Mock<IHiveTiesContext>();
mockContext.Setup(x => x.People)
.Returns(mockDbSet.Object);
var manager = new Manager(mockContext.Object);
//Assert
var _person = manager.CreatePerson("Winston", "Gabriel", DateTime.Now, 'M', personGuid);
if(_person == null)
{
throw new Exception("NOT WORKING MAN!!!!");
}
var personid = mockContext.Object.People.Single(x => x.RowId == personGuid).Id;
//Act
mockDbSet.Verify(x => x.Add(It.IsAny<Person>()));
mockContext.Verify(x => x.SaveChanges());
}
}
My problem is that my Person object is never created, that is, it always returns a null value and the exception is always thrown. I believe I am telling it to return a Person entity, but I am not sure where I am going wrong. This is my first time using Moq and running Mock Unit Tests.
I appreciate any suggestions. Thank you.
You didn't set any behavior on the Add method of mockDbSet.
Add:(This line initialize the method to return the given person)
mockDbSet.Setup(x => x.Add(It.IsAny<Person>()))
.Returns <Person>(p => p);
Between the mocks:
var mockDbSet = new Mock<DbSet<Person>>();
mockDbSet.Setup(x => x.Add(It.IsAny<Person>()))
.Returns<Person>(p => p);
var mockContext = new Mock<IHiveTiesContext>();
mockContext.Setup(x => x.People)
.Returns(mockDbSet.Object);
...
I created a new signature/function in IHiveTiesContext to save Person object. (It's up to you now how you implement the interface).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SampleManager
{
public class Manager : IManager
{
private IHiveTiesContext _ctx;
public Manager(IHiveTiesContext context)
{
_ctx = context;
}
public Person CreatePerson(string fn, string ln, DateTime dob, char gender, Guid RId)
{
var person = _ctx.Save(fn, ln);
return person;
}
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public char Gender { get; set; }
public DateTime DOB { get; set; }
public Guid RowId { get; set; }
}
public interface IManager
{
Person CreatePerson(string fn, string ln, DateTime dob, char gender, Guid RId);
}
public interface IHiveTiesContext
{
Person Save(string fn, string ln);
}
}
/// <summary>
///A test for CreatePerson
///</summary>
[TestMethod()]
public void CreatePersonTest1()
{
var mock = new Mock<IHiveTiesContext>();
//fill up your expected object
mock.Setup(m => m.Save(It.IsAny<string>(), It.IsAny<string>())).Returns(new Person { FirstName = "William" });
Manager t = new Manager(mock.Object);
var results = t.CreatePerson(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<DateTime>(), It.IsAny<Char>(), It.IsAny<Guid>());
Assert.AreEqual("William", results.FirstName);
}