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 4266088
In Process

The Archive Base Latest Questions

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

I am implementing Unit of Work. This is part of my interface: public interface

  • 0

I am implementing Unit of Work. This is part of my interface:

public interface IUnitOfWork
{
    void Add(object entity);
    void Update(object entity);
    void Delete(object entity);        
    void Commit();
}

It looks like from examples I’ve found online or in books, the Unit of Work is forced to work with type “object”, as we can be adding any “type” of entity to the uow.

Not a big deal until it comes time to Commit(). When I commit, given an entity type, I need to look-up the repository based on the entity type, and then call the appropriate action.

For example, I have an entity type named Expert

public class Expert
{
    public object ID { get; set; }
    public string Name { get; set; }
}

Expert has a repository named ExpertRepository. All Repositories in my application implement IRepository:

public interface IRepository<T>
{
    void Create(T item);
    void Update(T item);
    void Delete(T item);
}

public class ExpertRepository : IRepository<Expert>
{        
    public void Create(Expert item)
    {
        //TSQL to insert
    }

    public void Update(Expert item)
    {
        //TSQL to update
    }

    public void Delete(Expert item)
    {
        //TSQL to delete
    }
}

I want to clarify that it is not the responsibility of my Repositories to add, update, or delete an entity from my uow, rather, I’ll have the consuming code (maybe in a Service Layer) new up the uow, and then add directly to it… when my app is ready, it will call Commit().

This does two things. It allows my consuming code to decide how it wants the entity to be persisted. Either the consuming code can call .Create() directly on the Repository to persist the entity with no uow, or the consuming code can start a uow, call .Add() on the uow, and then when .Commit() is called, the uow will go through all entities, and by action type (add, update delete), new up the appropriate repository, and the TSQL code will execute.

Now, the problem I’m running into is when I’m looping through the Add, Update, and Delete collections in my uow. Here is a code snippet:

public void Commit()
{
using (TransactionScope tx = new TransactionScope())
    {
        try
        {
            foreach (object item in addedItems)
            {
                if (item.GetType() == typeof(Expert))
                {
                    IRepository<Expert> repository = new ExpertRepository();
                    repository.Create((Expert)item);
                }
            }
        }
    }
}

I have to resolve entity type of what’s in the object collection via the item variable. Then I need to new up that entity’s Repository (also providing the correct type to the IRepository interface), and then call .Create().

That’s all well and good, but I don’t want to have these conditional checks for every possible entity, for the entity’s interface, and the entity’s repository sitting in my uow. That’s very very bad.

I know that an IoC container could help here (such as Unity), but is there any IoC container that can resolve generic types at runtime? I ask, b/c IRepository will need to be typed correctly to be a var that holds the instance of a given entity’s repository.

I’ve made some design decisions here such as putting the strong typing with the Repositories, not the UOW. For instance, I don’t want a .Create() method sitting on a Repository that takes type object, esp. since my Repositories have a one-to-one relationship with my entities (Expert, ExpertRepository).

I’ve also decided that since most of the repository calls are the same, I don’t want an interface for each repository (aka, ExpertRepository implements IExpertRepository). That doesn’t make sense, I think using generics here is the answer.

But it all comes down to the problem in my uow. How do I get what I need to implement it correctly.

Any suggestions or pointers in the right direction would be appreciated.

Thanks!

  • 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-21T06:42:46+00:00Added an answer on May 21, 2026 at 6:42 am

    IoC is the way to go. Some of them supports open generics. Let’s take StructureMap as an example. You’ll need to have registration code similiar to this:

    // default implementation
    x.For(typeof(IRepository<>)).Use(typeof(DefaultRepository<>)); 
    // specific implementations
    x.For<IRepository<Expert>>().Use<ExpertRepository>(); 
    

    And then, your Commit method can just ask the container for the implementation for given type:

    _container.ForGenericType(typeof(IRepository<>))
                    .WithParameters(item.GetType())
                    .GetInstanceAs<IRepository>();
    

    (You’ll need to have non-generic IRepository interface that will cover the needs of your Commit method, but I believe that’s no problem when implementations can vary).

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

Sidebar

Related Questions

I am trying to work out why this works Public Interface IFullService Inherits ISerializableObjectLayerService,
I have an interface, written in C#, defined as this : public interface IWidget
We've seen some very useful methods of implementing EF4 Repository and Unit of Work
I have been using http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application as guidance to help me create a repository.I have
I'm currently researching how to add the Unit of Work pattern to my existing
few days ago i read tutorial about GenericRepository and Unit Of Work patterns http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application
I'm implementing unit testing with PHPUnit on an existing codebase. I'm fairly new to
I have a Visual Studio 2008 C++ project where I am implementing unit tests
implementing publishActivity in PHP using the REST API using this code: $activity = array(
The second assertion never executes in the unit test below: namespace Foo { public

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.