-1

I am having few static methods that are repeated in multiple projects (Multiple Solutions) like below. Few Table structures are same in all projects.

Project 1

public static decimal GetBalance(string CustID, MyProj1Context _context)
{
    // My Line 1
    // My Line 2
    // My Line ...N
}

Project 2

public static decimal GetBalance(string CustID, MyProj2Context _context)
{
    // My Line 1
    // My Line 2
    // My Line ...N
}

I want to create a new solution with class library and reference new classlibrary.dll in both projects so I changed _context type to dynamic in class library as to work in both projects but it throws

Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type.

public static decimal GetBalance(string CustID, dynamic _context)
{
    // My Line 1
    // My Line 2
    // My Line ...N
}

Is there any alternative way to use dynamic DBContext as parameter?

3
  • try var in place of dynamic Commented Feb 27, 2021 at 3:41
  • dynamic means the type is resolved at runtime instead of at compile time and is generally something you would want to avoid using Commented Feb 27, 2021 at 7:09
  • I think you could try to create a ContextFactory, in this factory, you could create DBContext based on the connection string. Then, when you call this factory, just tell the factory which database you want to use, then, it will return the DB context, and you could get data from the DB context. Please refer to the following threads: Thread 1 and Thread 2. Commented Mar 2, 2021 at 9:05

1 Answer 1

0

Have you tried using type arguments? And if you want to get rid of static you can do this:

public interface ICustomerRepository
{
    decimal GetBalance<TContext>(string customerId, TContext dbContext) where TContext : DbContext;
}

internal class CustomerRepository : ICustomerRepository
{
    public decimal GetBalance<TContext>(string customerId, TContext dbContext) where TContext : DbContext
    {
        ...
    }
}

I also would avoid directly referincing dlls in other projects. Dlls have no version, and this makes bugtracking difficult.

Try to use nuget packages instead. That's what they're meant for when consuming code from other solutions.

  • You can upload your package to nuget.org, but you only want to do this for packages that benefit the community.
  • You can upload your package to your github feed. I think you can make your package private
  • Or, what I would recommend in your use case, is to place the package on your disk in a common location or on a server in the network. Install the package in your target project.
    • You can then even add a nuget.config to your target project which specifies your custom nuget feed (you point to the network folder where they reside) and any collegue will still be able to open your project rightaway
Sign up to request clarification or add additional context in comments.

1 Comment

'TContext' does not contain a definition for 'MyTable' and no accessible extension method 'MyTable' accepting a first argument of type 'TContext' could be found (are you missing a using directive or an assembly reference?). Here MyTable is added at Derived class of DBContext.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.