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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:48:37+00:00 2026-05-28T14:48:37+00:00

I seperate my domain logic from my web service logic This is from my

  • 0

I seperate my domain logic from my web service logic

This is from my domain and actually gets the data from nHibernate

public static IList<Location> LoadReturnLocationsFromDatabase(DateTime lastUpdateTime)
{
    using (var session = NHibernateHelper.OpenSession())
    {
        // retreive all stores and display them
        using (session.BeginTransaction())
        {
            var locations = session.CreateCriteria(typeof(Location)).Add(Expression.Gt("LastUpdatedTime", lastUpdateTime)).SetMaxResults(10).List<Location>();
            return locations;
        }
    }
}

This data is then returned to the web service and I use Automapper to duplicate it, so as to not expose the database access object to the web service and keep all things seperate.

public IList<GetLocationDetailsResponse> GetLocationUpdate(DateTime lastUpdateTimeDT)
{

    Mapper.CreateMap<Location, GetLocationDetailsResponse>();

    IList<Location> locations = WhygoDomain.GetLocations.LoadReturnLocationsFromDatabase(lastUpdateTimeDT);

    IList<GetLocationDetailsResponse> getLocationDetails = Mapper.Map<IList<Location>, IList<GetLocationDetailsResponse>>(locations);
    return getLocationDetails;
}

My problem is that I can’t do the mapping unless I specify that the relationship between location and state isn’t lazy loaded because the web service is outside:

using (var session = NHibernateHelper.OpenSession())

in the data domain.

Lazy loading does seem to be the prefered method of doing something like this, so I’m wondering if this approach ok? This is a data export service which will export so memory usage etc could end up being problematic.

If I need to change this, is the cause of the problem the structure of my code? If so how can I keep my domain logic seperate, and get around this problem?

  • 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-28T14:48:38+00:00Added an answer on May 28, 2026 at 2:48 pm

    Eager Fetch

    You can avoid this problem and achieve better performance by eagerly fetching the states along with the locations – otherwise you have what’s called a “Select N+1” problem. See Ayende’s blog for a good explanation of this:
    http://ayende.com/blog/1328/combating-the-select-n-1-problem-in-nhibernate.

    Essentially, a separate SQL query is executed every time a different location.State is accessed, which could mean as many as 11 round-trips to the database in your case. If the location query included the states in a LEFT OUTER JOIN, then all of the needed data could be fetched in a single round-trip to the database.

    In your case, the following query will probably work better:

    var locations = session.CreateCriteria(typeof(Location))
        .Add(Expression.Gt("LastUpdatedTime", lastUpdateTime))
        .SetMaxResults(10)
        .SetFetchMode("State", FetchMode.Eager)
        .List<Location>();
    

    Dependency Inversion

    The problem you have encountered illuminates the fact that GetLocations does not know enough about the situation to be responsible for creating and destroying NHibernate Sessions. The creation of the NHibernate Session needs to be moved up at least one layer. There are certainly more elegant ways of doing all of this, like using an IoC container, but here’s some quick and dirty code to illustrate what I mean:

    public IList<GetLocationDetailsResponse> GetLocationUpdate(DateTime lastUpdateTimeDT)
    {
        using (var session = NHibernateHelper.OpenSession())
        {
            var locations = GetLocations.LoadReturnLocationsFromDatabase(session, lastUpdateTimeDT);
            return Mapper.Map<IList<Location>, IList<GetLocationDetailsResponse>>(locations);
        }
    }
    

    One final note: AutoMapper’s Mapper.CreateMap is static setup code that only needs to be executed once on application start. Global.asax is the best place for that type of code.

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

Sidebar

Related Questions

This is an ASP.NET MVC website. Following domain driven design, we have a service
Say I have a base class in Assembly A: public class MyBaseClass{ public static
I am designing a WCF service using DDD. I have a domain service layer
I'm a web developer who's working in a new shop with developers from a
According to this definition , Fowler's concept of Anemic Domain Model is: a software
I'm coming from a WebForms world where all logic is located in the codebehind
I want to create a spring-ws web service that eventually marshals a POJO into
This is a question on domain model design. Let's say for a domain design
I'm working on creating a domain layer in Zend Framework that is separate from
I have code seperated in two domain. Both domains and code is trusted. I

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.