Base solution for your next web application

Activities of "DennisAhlin"

Ok, good to know. Thank you!

In a test class in the TestModule.

Yes, this is true. I will use injection instead. But isn't it still weird that it doesn't work if I would have had a static factory that required the static Instance?

Because this Factory is overridden by multiple other factories, so there is much more code in form of constructors to add and maintain. Why would I not use it (except that it doesn't work)?

I get this problem too.

public class ComponentsInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store){
        container.Register(Component.For<ICustomerConnector>().ImplementedBy<CustomerConnector>().LifestyleTransient());
    }
}

MyCoreModule:
public override void Initialize()
...
//Here it works
var connector = IocManager.Resolve<ICustomerConnector>();
}

CustomerConnectorFactory : MyServiceBase
    readonly IWindsorContainer _container;

    public CustomerConnectorFactory(IWindsorContainer container)
    {
        _container = container;
    }

    public ICustomerConnector Create() {
        //This works!
         var conn = _container.Resolve<ICustomerConnector>();
    
        //This breaks with ComponentNotFoundException
        var connector = IocManager.Instance.IocContainer.Resolve<ICustomerConnector>();
        return connector;
    }
    
    
    
    
    

Is there any way of logging the SQL from the query?

Yes, I can use EnsureCollectionLoaded in this example where I use single. However if I want to select multiple parents I have to do like this? Which I assume is terrible performance wise?

var query = _parentsRepository
    .GetAll()
    .Where(p => *somePredicate*)
    .ToArray();

foreach (var parent in parents)
{
    await _parentsRepository.EnsureCollectionLoadedAsync(parent, p => p.Children);
}

After plenty of Googling and checking their issues I have not found anything that can help me.

The child have IMustHaveTenant because two different tenants should be able to see the Parent, but they should only see the Children they have created. Maybe there is another way of solving this?

Is there any way of logging the SQL from the query?

Ah, nice! Thank you for your help! :)

So, basically I have to do this:

public class MyDbContext : AbpZeroDbContext<Tenant, Role, User, MyDbContext>, IAbpPersistedGrantDbContext
{
     ...
    public override int SaveChanges()
    {
        var deletedIntegrations = ChangeTracker.Entries<Integration>()
            .Where(j => j.State == EntityState.Deleted)
            .Select(e => e.Entity)
            .ToArray();

        foreach (var deletedIntegration in deletedIntegrations)
        {
            IntegrationSteps.Remove(e => e.IntegrationId == deletedIntegration.Id);
        }

        return base.SaveChanges();
    }
}

Any suggestions on how to do this more maintainable?

I get null reference exception when trying to include children in tests and I cannot figure out why? Error:

System.NullReferenceException : Object reference not set to an instance of an object.
   at lambda_method(Closure , AnonymousObject )
   at System.Linq.Lookup`2.CreateForJoin(IEnumerable`1 source, Func`2 keySelector, IEqualityComparer`1 comparer)
   at System.Linq.Enumerable.<JoinIterator>d__37`4.MoveNext()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryBuffer.IncludeCollection(Int32 includeId, INavigation navigation, INavigation inverseNavigation, IEntityType targetEntityType, IClrCollectionAccessor clrCollectionAccessor, IClrPropertySetter inverseClrPropertySetter, Boolean tracking, Object entity, Func`1 relatedEntitiesFactory)
   at lambda_method(Closure , QueryContext , Parent , Object[] )
   at Microsoft.EntityFrameworkCore.Query.Internal.IncludeCompiler._Include[TEntity](QueryContext queryContext, TEntity entity, Object[] included, Action`3 fixup)
   at lambda_method(Closure , Parent )
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
   at lambda_method(Closure , QueryContext )
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass17_0`1.&lt;CompileQueryCore&gt;b__0(QueryContext qc)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
   at System.Linq.Queryable.Single[TSource](IQueryable`1 source, Expression`1 predicate)......

If I remove IMustHaveTenant from Child, the code works? My classes:

public class Parent : FullAuditedEntity
{
    public virtual ICollection&lt;Child&gt; Children { get; set; }
}

public class Child : FullAuditedEntity, IMustHaveTenant
{
    [Required]
    public int TenantId { get; set; }

    [ForeignKey("ParentId")]
    public virtual Parent Parent { get; set; }
    [Required]
    public virtual int ParentId { get; set; }
}

The test (that throws the exception):

var parentId = 1;
var tenantId = GetCurrentTenant().Id;

//Arrange
UsingDbContext(ctx =>
{
    ctx.Parents.Add(new Parent
    {
        Id = parentId
    });
                
    ctx.Children.Add(new Child
    {
        TenantId = tenantId,
        ParentId = parentId
    });
});

//Act
//I want to test stuff here that should change parent and/or children
            
//Assert
var parent = UsingDbContext(ctx =>
{
    return ctx.Parents.Include(p => p.Children).Single(i => i.Id == parentId); //Exception thrown here
});

parent.Id.ShouldBe(1);

parent.Children.Count.ShouldBe(1);
parent.Children.First().ParentId.ShouldBe(parentId);
parent.Children.First().TenantId.ShouldBe(tenantId);
Showing 1 to 10 of 34 entries