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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:07:33+00:00 2026-05-20T18:07:33+00:00

I am doing validation using DataAnnotation attributes on the Model classes, and the Model

  • 0

I am doing validation using DataAnnotation attributes on the Model classes, and the Model class is used for validation on both the Client and Server side of the application.

My problem is, I can’t figure out how to lazy load my Model’s properties without causing circular references

The libraries involved are:

  • WCF Service Library
  • Client-Side DataAccess Library
  • Models Library

Because the Models library is used on both the Client and Server side for data validation, I cannot reference the DataAccess library from within the Models library. Therefore, how can I setup lazy-loading?

For example, I have a ConsumerModel which has a property of PhoneNumbers which should be lazy loaded. How can I load the PhoneNumberModels from within the ConsumerModel without referencing the Client-Side DAL?

Client-side DAL:

using MyModels;

public class ConsumerDataAccess
{
    public ConsumerModel GetConsumerById(int id)
    {
        ConsumerDTO dto = WCFService.GetConsumer(id);
        return new ConsumerModel(dto);
    }
}

ConsumerModel:

public class ConsumerModel
{
    public ObservableCollection<PhoneNumberModel> _phoneNumbers;

    public ObservableCollection<PhoneNumberModel> PhoneNumbers
    {
        get
        {
            if (_phoneNumbers == null)
            {
                // Can't reference DataAccess Library since that would cause a Circular Reference
            }
        }
    }
}

What are some alternative ways I could make this architecture work?

I would prefer to keep Validation with the Models, and to use the models from both the Client and Server side for validation. I would also prefer to keep using DataAnnotation for Validation.

EDIT

Here’s my final solution based on Lawrence Wenham’s answer if anyone is interested. I ended up using a delegate instead of an event.

DAL:

public class ConsumerDataAccess
{
    public ConsumerModel GetConsumerById(int id)
    {
        ConsumerDTO dto = WCFService.GetConsumer(id);
        ConsumerModel rtnValue = new ConsumerModel(dto);
        ConsumerModel.LazyLoadData = LazyLoadConsumerData;
        return rtnValue;
    }
}

private object LazyLoadConsumerData(string key, object args)
{
    switch (key)
    {
        case "Phones":
            return PhoneDataAccess.GetByConsumerId((int)args);
        default:
            return null;
    }
}

Model Library:

public class ConsumerModel
{
    public delegate object LazyLoadDataDelegate(string id, object args);
    public LazyLoadDataDelegate LazyLoadData { get; set; }

    public ObservableCollection<PhoneNumberModel> _phoneNumbers;

    public ObservableCollection<PhoneNumberModel> PhoneNumbers
    {
        get
        {
            if (_phoneNumbers == null && LazyLoadData != null)
            {
                _phoneNumbers = (ObservableCollection<PhoneNumberModel>)
                        LazyLoadData("Phones", ConsumerId);
            }
            return _phoneNumbers;
        }
    }
}
  • 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-20T18:07:34+00:00Added an answer on May 20, 2026 at 6:07 pm

    One way might be to raise an event in the get {} of your Model classes properties, and then implement a lazy-loading manager on the client side that has a reference to your DAL. EG:

    public class LazyLoadEventArgs: EventArgs
    {
        public object Data { get; set; }
    
        public string PropertyName { get; set; }
    
        public int Key { get; set; }
    }
    

    Then in your Model classes:

    public event EventHandler<LazyLoadEventArgs> LazyLoadData;
    
    public ObservableCollection<PhoneNumberModel> PhoneNumbers
    {
        get
        {
            if (_phoneNumbers == null)
            {
                LazyLoadEventArgs args = new LazyLoadEventArgs {
                    PropertyName = "PhoneNumbers",
                    Key = this.Id
                };
                LazyLoadData(this, args);
                if (args.Data != null)
                   this._phoneNumbers = args.Data as ObservableCollection<PhoneNumberModel>;
            }
            return _phoneNumbers;
        }
    }
    

    The handler for the LazyLoadData event would have the job of fetching the data from the client side’s DAL, then storing it in the .Data property of LazyLoadEventArgs. EG:

    private void Model_HandleLazyLoadData(object sender, LazyLoadEventArgs e)
    {
        switch (e.PropertyName)
        {
            case "PhoneNumbers":
                e.Data = DAL.LoadPhoneNumbers(e.Key);
                break;
            ...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the right way of doing both client side and server side validation
I am performing both clientside validation and server side validation using jquery and Asp.net
I am doing form submission and validation using jQuery and from server side I
I am doing some client side validation in ASP.NET MVC and I found myself
I am doing validation server side, If fails I am passing back json data
For example, if I'm doing some form input validation and I'm using the following
I have an ASP.NET MVC 2 form that is working perfectly, doing client side
There seems to be some confusion, at least for myself, on client side validation
I'm doing Javascript validation on my number fields. I'm using RegEx to do this
I'm doing validation on a lengthy form using a custom attribute. One of the

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.