Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7076897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:21:17+00:00 2026-05-28T06:21:17+00:00

Why is it not possible to do this: public static void Init<TEntity>(params TEntity[] repositories)

  • 0

Why is it not possible to do this:

public static void Init<TEntity>(params TEntity[] repositories) where TEntity : Entity
{
    foreach (TEntity item in repositories)
    {
       Repository<item> rep = new Repository<item>();
    }
}

The above code won’t compile: Cannot resolve symbol item

Yet this works:

public static void Init<TEntity>(TEntity entity) where TEntity : Entity
{
    Repository<TEntity> rep = new Repository<TEntity>();
}

EDIT

I’m editing the OP to give a bigger picture of the problem. We are experiencing some issues with the entity framework Db Context being spawned by multiple repositories. Currently we access the repositories like:

Repository<Product> rep = new Repository<Product>()
Repository<Account> rep = new Repository<Account>()

Since there’s a relationship between product and account EF will complain about objects being attached to a different context. So we are trying to resolve this issue by consolidating repository access into a Unit of Work pattern:

Here’s an example of what I’m trying to achieve:

public class UnitOfWork
{
    protected List<Entity> Repositories = new List<Entity>();
    private readonly DbContext _context;
    protected UnitOfWork()
    {
       _context = new SqlDbContext();
    }

    public static UnitOfWork Init<TEntity>(params TEntity[] repositories) where TEntity : Entity
    {
            UnitOfWork uow = new UnitOfWork();
        foreach (TEntity item in repositories)
        {
            Repository<item> rep = new Repository<item>();
            uow.Repositories.Add(rep);
        }
            return uow;
    }

    public IRepository<T> GetRepository<T>() where T : Entity
    {
        return Repositories.OfType<T>().Single();
    }   
}

So we can access our repositories like:

GetRepository<Product>().GetById(1);
GetRepository<Account>().GetById(123434);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:21:17+00:00Added an answer on May 28, 2026 at 6:21 am

    item is an instance of a type, but you need a type parameter to create an instance of your generic Repository<T> class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably not possible, but I have this class: public class Metadata<DataType> where
When I have a method like this: public static void foo(String param) throws IOException
I have Model class like this class ModelName { public static void createTable(SQLiteDatabase db){
Many times i am using code like this: public static void ExecuteMethod1InThread(string msg) {
In Java I had a method like this. public static void digPackageLines( List<PkgLine> pkgLineList,
This is weird and probably not possible but I'll ask anyway. I'm making this
Okay, I know that 1) this is probably not possible with CSS alone and
I'm not sure if this is possible or not. Basically, I'm writing a script
Is this possible? I had troubles with SVN clients not being able to access
I don't know if this is possible or not, or if my limited knowledge

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.