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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:12:34+00:00 2026-05-25T16:12:34+00:00

I come up against this over and over again: writing code to create instances

  • 0

I come up against this over and over again: writing code to create instances of entities (i.e. populate rows into the database) where there are child associations (i.e. FK references) that need to be filled in.

Example:

namespace EomApp1.Formss.AB2.Model
{
    public class UnitConversionSource : EomApp1.Formss.AB2.Model.IUnitConversionSource
    {
        IEnumerable<UnitConversion> IUnitConversionSource.UnitConversions(DirectAgentsEntities model)
        {
            yield return new UnitConversion
            {
                Coefficient = 1,
                FromUnit = model.Units.First(c => c.Name == "USD"),
                ToUnit = model.Units.First(c => c.Name == "USD"),
                DateSpan = new DateSpan
                {
                    FromDate = DateTime.Now,
                    ToDate = DateTime.Now.Add(TimeSpan.FromHours(1))
                }
            };
        }
    }
}

Desired: the first execution in a given expiration period (say 10 seconds) model.Units.First(c => c.Name == "USD") executes, the entity is fetched from the database, but subsequently (within the expiration period) comes from a memory cache.

Goal: Prevent a loop that inserts a million rows from making the same select query a million times.

I’m interested, specifically, in a non-intrusive solution that doesn’t affect the way I write the code in the example. This is (hopefully) in the spirit of best practices from a separation of concerns architectural standpoint.

(ps: i’m not sure if my use of the term non-intrusive was the standard semantic – let me know if there’s a better way to prase this)

  • 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-25T16:12:35+00:00Added an answer on May 25, 2026 at 4:12 pm

    I’m not sure what is the point of your code but it has obvious and correct solution:

        IEnumerable<UnitConversion> IUnitConversionSource.UnitConversions(DirectAgentsEntities model)
        {
            string unit = model.Units.First(c => c.Name == "USD");
    
            yield return new UnitConversion
            {
                Coefficient = 1,
                FromUnit = unit,
                ToUnit = unit,
                DateSpan = new DateSpan
                {
                    FromDate = DateTime.Now,
                    ToDate = DateTime.Now.Add(TimeSpan.FromHours(1))
                }
            };
        }
    

    If you know that you have to use some values very often it is correct way to fetch them to some dictionary and handle its lifetime yourselves or within some cache API.

    In case of more complex scenarios you can use EF Caching Provider but in such solution your query must be still parsed, hash must be computed, etc. so for your scenario preloading all units to dictionary looks like better way.

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

Sidebar

Related Questions

I come across this problem when i am writing an event handler in SharePoint.
I have come up against this problem a few times at inopportune moments: Trying
I come up against this challenge frequently -- and when trying to solve I
Hoping someone can help me with a slight hurdle I've come up against in
Sam Ruby, author of RESTful Web Services seems to come out against the use
How come this doesn't work (operating on an empty select list <select id=requestTypes></select> $(function()
How come the following doesn't work? CREATE FUNCTION Test (@top integer) RETURNS TABLE AS
I've come up against a bit of a brick wall with Microsoft's .net XmlSerializer.
Inspired by this question. Why is there no list.clear() method in python? I've found
(Feel free to skip the first two paragraphs :) I know this goes against

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.