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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:14:08+00:00 2026-05-23T16:14:08+00:00

Okay, I need some help here. This is the same old "can’t use an

  • 0

Okay, I need some help here.

This is the same old "can’t use an interface to enforce constructors/static methods" problem.

WHAT IS THE PROPER OO DESIGN THEN??

I have a set of data entities (Entity Framework stuff) for which I wrote partial class methods to convert to/from XML (XElement objects).

I have an instance method to "save" XML:

// Convert entity to XML
public XElement ToXml() {}

…and I have a constructor to "read" XML:

// Create entity from XML constructor.
public MyEntity(XElement) {}

Alternatively, I could use a static factory method to "read" XML:

public static MyEntity ParseXml(XElement) {}

The dilemma:

  1. I could create an interface that mandates the "save" ToXml() method, but what good is that if it only addresses half the problem? An interface can’t enforce either of the "load" methods.

  2. I could rely on my own good intent to create these methods without any sort of contract.

  3. I could create a static class filled with redundant methods like XmlToEntity1() and XmlToEntity2(), etc… (Now I’ve described a good ‘generics’ problem.) However, the specific conversion code (which is specific to each entity) would create separate methods or switch/case for each and seems to belong inside the entity classes, not in some other class, no?

If an experienced C# coder can show a good design for this common problem I think I would learn a lot from it.

Happy 4th of July!

Possible Solution 1

A single XmlSerializer class with two static generic methods:

public static T Deserialize<T>(XElement xml) {}
public static XElement Serialize<T>(T entity) {}
  • Pro: Only one class (no interface needed)
  • Pro: Separates serialization responsibility from entity classes.
  • Con: Would still require separate methods or switch/case blocks for each entity type supported.
  • Con: ? Not extensible — have to modify this class each time an entity changed or added/deleted.

Possible lessons learned?

The "can’t use interface for constructors and static methods" problem might be a symptom of:

  1. Violating the SRP (Single Responsibility Principal).
  2. Violating the SoC (Separation of Concern) principal.
  • 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-23T16:14:08+00:00Added an answer on May 23, 2026 at 4:14 pm

    It would be a good design to have the storage/retrieval separate from the entity anyway. In OO terms this can be referred to as the Single Responsibility Principle. Your entity exists for some purpose (probably related to your domain). The storage of that entity is a separate responsibility that could change independent of the domain. (e.g. you could retrive it from a database, a web service or the file system).

    Static methods are not the only way to do this of course. You could create an interface at the save/retrive level and implement that interface for each of the entities. Then you could easily use those in a generic way without worrying a lot about the types.

    Adding some example code:

    interface EntityGateway<TEntity> {
        TEntity Load(String id);
        void Save(TEntity entity);
    }
    public class XEntityGateway implements EntityGateway<XEntity> {
        XEntity Load(String id) { ... implementation details } 
        void Save(XEntity entity) { ... implementation details } 
    }
    
    XEntityGateway gw = new XEntityGateway();
    XEntity entity = gw.Load("SOMEID");
    // modify entity
    gw.Save(entity);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, I am stumped on this one and need some quick help. I have
Okay, so I'm obviously a newbie, but I need some help passing a variable
Okay, I need help. I'm usually pretty good at SQL queries but this one
Okay! I really need your help or some tips I need a program that
Okay, so this probably sounds terribly nefarious, but I need such capabilities for my
Okay so im working on this php image upload system but for some reason
Ok been working with WPF for a while but I need some help. I
Okay so i set up this thing so that I can print out page
Okay, so I need to use PHP to obtain the date of the next
I need some help with modeling in Cognos Framework Manager 8.4. Given following schema

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.