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

  • Home
  • SEARCH
  • 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 6343441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:26:13+00:00 2026-05-24T20:26:13+00:00

I have the following business objects: public abstract class Product { public int Id

  • 0

I have the following business objects:

    public abstract class Product
    {
        public int Id { get; set; }
        public bool OnStock { get; set; }
    }

    public class ProductForImport : Product
    {
        public int ImportId { get; set; }
    }

    public class ProductForExport : Product
    {
        public int ExportId { get; set; }
        public bool IsExportable { get; set; }
        public bool IsUsable { get; set; }
        public string OtherParam {get; set;}

        public static implicit operator ProductForExport(ProductForImport pfi)
        {
            ProductForExport p = new ProductForExport();
            p.Id = pfi.Id;
            p.IsExportable = true;
            p.ExportId = 0;
            return p;
        }
    }

so I can convert between the two types:

    static void Main(string[] args)
    {
        ProductForExport pfe = new ProductForExport();
        pfe.Id = 1;
        pfe.OnStock = true;

        ProductForImport pfi = new ProductForImport();
        pfi.ImportId = 200;

        ProductForExport pfe2 = (ProductForExport)pfi;
    }

this works OK.

I have 100.000 ProductsForImport items.
If I understand correctly, if I convert them to ProductsForExport items, I’ll have 100.000 +100.000 items in memory – that’s reasonable.

My problem is: I have to send these “ProductForExport” objects through JSON services, each service just need some subset of the properties of each type:

servicecall1 should return ProductForExport1{ExportId,IsExportable}

servicecall2 should return ProductForExport2{ExportId,IsUsable}

Question: should I write an implicit conversion similar to the above example for these new types – ProductForExport1 and ProductForExport2 (so basically create 100.000+100.000 new objects)

or

somehow can I just “hide” the unwanted properties with some magic from the original type without the need to create new instances?

thanks,

b.

  • 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-24T20:26:15+00:00Added an answer on May 24, 2026 at 8:26 pm

    If you ned such kind of decoupling and separation of entities – you can create DTO object along with each business object and use DTO to communicate with Service.
    But if you have a lot of business entities consider an other approach to avoid maintenance hell.

    public sealed class ExportProductDto
    {
       public(ProductForExport exportProduct)
       {
           // initialize fields
           this.ExportId = exportProduct.ExportId;
       } 
    
       public int ExportId { get; private set; }
    }
    

    BTW,
    An overkill solution with operator overload, use Adapter pattern to convert between product types

    To decouple adapting from entities itself implement following interface your self:

    public interface IProductAdapter<TImport, TExport>
    {
        TImport ToImportProduct(TExport exportProduct);
        TExport ToExportProduct(TImport importProduct);
    }
    

    Or an other adapter approach:

    // Implement this interface for ProductForImport class
    // public class ProductForImport : IExportProductAdapter, Product
    public interface IExportProductAdapter
    {
        ProductForExport ToExportProduct();
    }
    
    // Implement this interface for ProductForExport class
    // public class ProductForExport : IImportProductAdapter, Product
    public interface IImportProductAdapter
    {
        ProductForImport ToImportProduct();
    }
    

    EDIT: Answer to comments

    // An example of IExportProductAdapter adapter implementation
    public sealed class ProductForImport : Product, IExportProductAdapter
    {
         public int ImportId { get; set; }
    
         public ProductForExport ToExportProduct()
         {
            ProductForExport p = new ProductForExport();
            p.Id = this.Id;
            p.IsExportable = true;
            p.ExportId = 0;
            return p;
         }
    }
    

    And then instead of:

     ProductForExport pfe2 = (ProductForExport)pfi;
    

    You can do:

     ProductForExport pfe2 = pfi.ToExportProduct();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following business objects: public class ItemCategoryBO { public string ItemCategory {
I have the following idea: Business object implemented as interface or abstract class with
I have the following in a business logic assembly: public class BusinessEntity { ...
I have the following business object: public class MyObject { // Some public properties
I have the following entity model: public class Project { [Key] public int ProjectID
I have the following classes: public abstract class BusinessRule { public string PropertyName {
I have the following sort descriptors that sort an array of my business objects,
I have the following Ria Service define: namespace SilverlightTest.Web { [EnableClientAccess()] public class ContactService
With the following business object: public class ItemsRow : BusinessObject<ItemsRow> { public static readonly
I have the following pojo: public class Foo { @Size(min=0,max=10) private String bar =

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.