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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:23:30+00:00 2026-05-24T19:23:30+00:00

I am asking in order to know what is the best practice for such

  • 0

I am asking in order to know what is the best practice for such cases. I am using EF4.

I have two entities called “Note” and “Product”. I have another entity called “Stock”.
Note has many Products and each Product can be in one Note only (Product must have a Note).
Whenever I insert, Delete or Update a Product, I have to update the relevant Stock entity according to the Note attached to the Product.

In the InsertProduct() method I insert the Product and then update the relevant Stock. In case the Product will break the Stock rules – I get an exception and the operation will abort as should be.

In the InsertNote() method, I cannot insert the Note because such thing will insert automatically the attached Products and for each Product I need to update the Stock and validate Stock rules (as described above). So before the Note insertion I loop through all the Products and call to InsertProduct(). After that I insert the note.

Until here two questions:

  1. Does the things should be like that? Do I need to manually insert
    each Product before I insert the Note? How does this settle with the
    fact Product must be attached to exists Note (in the time of
    insertion the Products, the Note wasn’t inserted)?

  2. Where should I put the InsertNote() and InsertProduct() methods?
    each one of them deals with Note, Product and Stock entities. Should I create something like NoteService and ProductService classes or should I put them in one big service class?

In the question I asked here, I got an answer that describe how to create events like OnBeforeInsert(). Is this implementation is fine with my needs to update the Stock after insert Product?

  • 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-24T19:23:30+00:00Added an answer on May 24, 2026 at 7:23 pm

    I’m not quite sure I fully grasp what you’re trying to do and what it means to validate Stock rules and update stocks. But it seems like you’re trying to reuse InsertProduct from InsertNote when you shouldn’t be.

    It seems like InsertNote should

    • check all the rules and update Stock accordingly (I’m assuming this means loading the stocks into the context from the database or attaching them if you have them in memory somewhere)
    • add the new Note to the context (which will add the new products you’ve put into its collection before)
    • SaveChanges()

    and InsertProduct should

    • check/update stocks as above
    • add the Product to the Note‘s collection of Product
    • SaveChanges()

    In other words, both these methods would reuse just the rule checking part. InsertProduct might be better renamed to UpdateProductsOfNote because it sounds like Note is an aggregate root of your model (In DDD, you do persistence/repository operations against the aggregate).

    As to where these methods live, if they are not too complex, adding them to a repository/data access class (that owns the EF context object) would be ok. It does seem like they have quite a few rules associated with them. If so, some sort of domain service would probably be a better choice. It seems like much of the rules aren’t simply to support persistence. If you relocate these methods then you may not need to handle ObjectContext events as you asked in the post you referenced.

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

Sidebar

Related Questions

I have two dendrograms which I wish to compare to each other in order
I'd really like to know what knowledge do I have to pursue in order
Just asking if I have the right understanding System.out.println(); System is the package out
I know what I asking might not make a lot of sense for C#
I have an Order object to represent a Prospective Order/Receipt. This is an entity.
I'm sorry to keep asking stupid questions, but I have tried to research this
I have used Java, C++, .Net. (in that order). When asked about by-value vs.
I don't know if I'm asking this the right way but here's what I
The first question your probably asking is how many Group by / Order by
It's my first time asking here (I have visited the site several times, but

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.