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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:52:07+00:00 2026-06-05T04:52:07+00:00

I have a Data layer and Business layer in my App. In data layer

  • 0

I have a Data layer and Business layer in my App.
In data layer I have imported database tables as objects using entity framework.
One of them is Unit table for example.
Now in business layer I want to add some methods to Unit of data layer so I have this class:

namespace Business.Entity
{
    public class Unit : Data.Unit 
    {
     //Some business affairs here
    } 
}

And for loading units in UI I have created a repository in business layer :

    public static IEnumerable<Data.Unit> LoadUnits()
    {
        return from entity in StaticDataContext.Units select entity;
    }

Everything is good till now.

But I want to load a list of Business.Unit in UI so I wrote this one:

    public static IEnumerable<Business.Entity.Unit> LoadUnits()
    {
        var result = (from entity in StaticDataContext.Units
                      select entity).ToList().Cast<Business.Entity.Unit>();
        return result;
    }

It compiles well but then I get this runtime error when binding it to a Grid:

InvalidCastException: Unable to cast object of type 'Data.Unit' to type 'Business.Entity.Unit'

Can any one say how to arrange the classes to be able to load business classes in UI ?

Thanks

  • 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-05T04:52:09+00:00Added an answer on June 5, 2026 at 4:52 am

    You can not directly cast parent object to child object. Possible solutions for your problem:

    1. Create in Business.Entity.Unit class a constructor accepting Data.Unit as argument and assigning all necessary properties, like:

      namespace Business.Entity
      {
          public class Unit : Data.Unit 
          {
              public Unit(Data.Unit parent)
              {
                  // Assign proprties here
              }
              //Some business affairs here
          } 
      }
      

      After that you can do:

      public static IEnumerable<Business.Entity.Unit> LoadUnits()
      {
          var result = (from entity in StaticDataContext.Units
                        select new Business.Entity.Unit(entity)).ToList().Cast<Business.Entity.Unit>();
          return result;
      }
      
    2. Rewrite your Business.Entity.Unit class so that it does not inherit Data.Unit, but accepts Data.Unit entity as a constructor parameter, aggregates it (stores in a local private member) and presents wrapper properties and functions to operate on it

    3. Remove your Business.Entity.Unit class entirely and implement all additional methods as extension methods.

    I’d vote for the third one because IMHO it leaves code a bit cleaner and does not have overhead of introducing and managing additional entity.

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

Sidebar

Related Questions

I have a layered application with namespaces: App.Core - business layer logic services App.Data
I'm working on a project in which we have a database, data layer (entity
If I have a multi-layer Winform app with a Presentation, Business and Data Layer
I have a project of business objects and a data layer that could potentially
I have a data access layer, a business logic layer and a presentation layer
I have a service, business and data access layer. In which layer should I
I have created a Data Access Layer using .NET. Everywhere that I update a
I have an existing web app that has a data layer and a bll
Here's the architecture of my app: I have a data access layer in a
so, I have this: Web App Business Logic Data Access Logic I created in

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.