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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:44:38+00:00 2026-06-14T01:44:38+00:00

I am using UnitOfWork along with Repository Pattern. The architecture is MVVM with LINQ-to-SQL

  • 0

I am using UnitOfWork along with Repository Pattern. The architecture is MVVM with LINQ-to-SQL Classes directly as Model. Following code snippets show Generic Repository, A Sample(City Repository) and UnitOfWork Class.

Generic Repository:

public abstract class Repository<T> : IRepository<T> where T : class
    {
        protected Table<T> _objectSet;

        #region Constructor

        public Repository(DataContext context)
        {
            _objectSet = context.GetTable<T>();            
        }

        #endregion


        public virtual void Add(T entity)
        {
            _objectSet.InsertOnSubmit(entity);
        }
}

CityRepository:

 public class CityRepository: Repository<City>
 {
        public CityRepository(DataContext context): base(context)
       {
             public override void Add(City entity)
             {
                  //Can't publish event here, because Changes 
                  //aren't still submitted to Database

                  base.Add(entity);
                  //CityAddedEventArgs e = new CityAddedEventArgs(entity);
                  //if (this.CityAdded != null)
                  //this.CityAdded(this, e);

             }
       }
 }

Unit Of Work

public class UnitOfWork : IUnitOfWork
{
        private readonly DataContext _context;

        public UnitOfWork(DataContext context)
        {
            _context = context;
        }

        #region IUnitOfWork Members

        public AddressRepository Addresses
        {
            get
            {
                if (_addresses == null)
                {
                    _addresses = new AddressRepository(_context);
                }
                return _addresses;
            }
        }

        public CityRepository Cities
        {
            get
            {
                if (_cities == null)
                {
                    _cities = new CityRepository(_context);
                }
                return _cities;
            }
        }


        public void Save()
        {
            _context.SubmitChanges();

           //Need to Raise event here if an entity is added, 
           //but don't know which entity is added !
        }

}

Now. I want to publish an event when an entity is added to the database i.e. on UnitOfWork.Save() method. For e.g. If AddressEntity is added to the database, AddressAdded event must fire and if CityEntity is added to the database, CityAdded event must fire. But then how would I know after SubmitChanges which entity is added to the database ?



The sole purpose of publishing event is let ViewModel know that an Entity is added to database, and now it can add the EntityViewModel to its ObservableCollection<EntityViewModel>

  • 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-06-14T01:44:39+00:00Added an answer on June 14, 2026 at 1:44 am

    In your Save method, you can first put the added elements in a list and use that list after the SubmitChanges(). Something like (I don’t have Visual Sutdio right now so it might not compile):

        public void Save()
        {
            var inserts = _context.GetChangeSet().Inserts.ToList();
    
            _context.SubmitChanges();
    
            foreach(var entityAdded in inserts)
            {
                //raise event
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UnitOfWork/Service pattern where I populate my model using NHibernate before sending
I'm using EF along with mvc, for that I've a generic repository, unitOfWork implementation
I have a MVC app using the UnitOfWork, Service Layer, Repository Pattern and EF4
I currently have a Repository/UnitOfWork pattern down. However, there is one hard coupling that
In .NET4 MVC 4 using Code First I have two classes that form a
Let's say I have a Generic Repository class along the lines of the following:
I am using EF with Repository Pattern and unit of Work. I am pulling
Im trying to implement a UnitofWork pattern using this Scott Allen tutorial My current
I have a repository pattern setup using NHibernate. The base class looks like this:
I have the following pseudo code using EF Code First: User user = GetFromCache();

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.