Base solution for your next web application
Open Closed

Multiple UnitOfWork #8868


User avatar
0
andmattia created

I'm working on a solution wiht more EF and I'm able to call separete EF and use distributed query.

Last week I need to add a a connection to MongoDb to storage a Json data so I've created a custom repository connect my Entity to MongoDb and it work fine until I call that from an abstract base class.

The CreateQuery flow on correct repository because use concrete implementation (es Mongo or EF dipends on specific query body)

But when I call the CountAsync() how can I identify if my provider is Mongo or EF...

At the moment i create a SafeCountAsync() and cast to specific provider but I'm not sure that is the correct way to solve it.

Below some code from my solution

    public abstract IQueryable<TQueryReturnType> CreateQuery(TQueryInput input);
    
    public asbtract class MyBase{
    /// remove for brevity
    
     public virtual async Task<PagedResultDto<TListResult>> GetItems(TQueryInput input)
        {
            var query = CreateQuery(input).
                .ApplyFilter(input);

#if DEBUG
            Debug.WriteLine($"Query for {typeof(TQueryReturnType)}");
            Debug.Write(query?.ToString());
#endif

            //var count = await query.CountAsync();
            var count = await query.SafeCountAsync();
            
            }
}

// from static extension to call correct count
public static Task<int> SafeCountAsync<TSource>(this IQueryable<TSource> source)
        {
            IsValid(source);

            if (typeof(IMongoTable).IsAssignableFrom(typeof(TSource)))
            {
                return ((IMongoQueryable<TSource>)source).CountAsync();
            }
            else
            {
                // Default EF SQL
                return source.CountAsync();
            }
            
        }

4 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    hi

    But when I call the CountAsync() how can I identify if my provider is Mongo or EF...

    Abp's mongodb and ef core cannot work with the unit of work together.

    You can directly use mongodb's API instead of mongodb repository and other components.

    Actually AspNet Boilerplate doesn't officially support MongoDB at the moment.

  • User Avatar
    0
    andmattia created

    Hi

    I know that MongoDb is not supported but geneally speking is not possibile register a second UnitOfWork?

  • User Avatar
    1
    maliming created
    Support Team

    I think this is not possible, by design.

  • User Avatar
    0
    andmattia created

    Ok thanks for your support