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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:54:35+00:00 2026-05-25T19:54:35+00:00

* Preface: I’m pretty new to the unit of work pattern * My goal

  • 0

* Preface: I’m pretty new to the unit of work pattern *

My goal is to implement a unit of work class that will be able to keep track of all objects that have been changed throughout a given transaction. Everything I read about the unit of work pattern has it side by side with the repository pattern. So this is the approach I’d like to use.

Say for example I create a new User. On my unit of work object, I have a list of newly created objects, so I add my new User to this list. My user repository has a method titled Create, which takes in a User and calls a stored procedure to add the data to the database. When I call commit on my unit of work, how will it know which repository and method to call based on the list of new objects? Say it contains a User object and a Comment object. Both are newly created and need to be added on commit. I’m uncertain how to accomplish this.

Could somebody explain this a bit better and maybe even a small example if possible?

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-25T19:54:36+00:00Added an answer on May 25, 2026 at 7:54 pm

    One of most common ways of solving this is using inversion of control.

    For example, you’ve your classes User and Comment, and you’ve implemented a generic repository IRepository<TDomainObject>. That’s getting a repository of User or Comment is just giving the TDomainObject parameter:

    • IRepository<User>
    • IRepository<Comment>

    Later you’ve configured who’s implementing IRepository<User> and IRepository<Comment>, so if you use something like Microsoft Pattern & Practices’ Common Service Locator, and we’re in the body of commit method in your unit of work:

    foreach(DomainObject some in NewObjects)
    {
            ((IRepository<DomainObject>)ServiceLocator.Current.GetInstance(Type.GetType(string.Format("NamespacesToGenericRepository.IRepository`1[[{0}]]", some.GetType().FullName)))).Add(some); 
    }
    

    Note IRepository<TDomainObject> has a contravariant TDomainObject generic parameter which type must inherit a base type of Domain Object called DomainObject, which permits an upcast of something like IRepository<User> to IRepository<DomainObject>.

    In other words, your IRepository<TDomainObject> interface signature will look like this:

    public interface IRepository<out TDomainObject>
        where TDomainObject : DomainObject
    

    This is just a summary and/or hint about how to implement locating the concrete repository so an unit of work of domain objects can manage ones of any specialized domain object.

    If you want to learn more about inversion of control check this Wikipedia article:

    • http://en.wikipedia.org/wiki/Inversion_of_control

    And, because of my own experience, I’d like to suggest you Castle Windsor as inversion of control framework of choice:

    • http://www.castleproject.org/container/
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Preface Let me start off by saying that I'm a relatively new programmer and
**Preface I'm very new to generics. I have a class I created called Geno.
Let me preface this question with saying that I am very new to Orchard
Let me preface this question by saying that I am relatively new to writing
I'll preface this by saying that I usually work in C#/.Net. Normally, I use
Preface: I am 100% new to the Flash and ActionScript world, doing research for
Preface : I am new to the iPhone SDK, Obj-C, Interface Builder and Cocoa.
Preface: I'm know that in most cases using a volatile field won't yield any
To preface: I am very new to C, so I am probably missing something
Preface: I know that there are high quality graph APIs available. I'm interested in

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.