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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:18:43+00:00 2026-05-31T16:18:43+00:00

I’m working on a project that uses dependency injection via Ninject. So far, it

  • 0

I’m working on a project that uses dependency injection via Ninject. So far, it is working out very well, and I’m liking DI a lot, but now I have decided I need to serialize some objects, and I’m finding it difficult to do that following DI patterns.

Say I have a class called Foo, which has a list of Bars, and makes them through a factory, like this:

public class Foo
{
    private List<Bar> _bars;
    private BarFactory _barFactory;

    ...

    public void MakeBar()
    {
         _bars.Add(_barFactory.MakeBar());
    }
}

Here’s Bar, which gets made when _barFactory.MakeBar() gets called. I want Bar to be serializable:

public class Bar : ISerializable
{
    private List<IPickle> _pickles;
    private PickleFactory _pickleFactory;

    public Bar(PickleFactory factory)
    {
         _pickleFactory = factory;
    }

    public void MakePickle(int x)
    {
         _pickles.Add(_pickleFactory.MakePickle(x));
    }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        //serialize member variables here
    }

    //Constructor called during deserialization
    private Bar(SerializationInfo info, StreamingContext context)
    {
        //fill in member variables with data from SerializationInfo
    }
}

Notice that Bar has its own factory and a collection of pickles. Here’s the problem: when the deserialization constructor of Bar gets called, I don’t have any way to get it another PickleFactory. The original PickleFactory was given to Bar by the BarFactory, but the deserialization constructor wasn’t called by BarFactory.

My current plan to solve this problem would be to extract all the serializable members of Bar into its own class called BarDataObject. Then I would make the BarDataObject serializable, but not Bar itself. I would add a function to BarFactory which accepts a BarDataObject as a parameter and builds a Bar for you filled in with all the information from the BarDataObject.

However, suppose Pickle also has service classes which it got from the factory that made it, which can’t be serialized either. So I would have to extract a DataObject from Pickle as well, and my BarDataObject would have to hold onto a PickleDataObject. And suppose Pickle had a member variable with a mix of data and services too? I would have to make and maintain a DataObject for that as well. This seems like a real bummer, especially considering my project has many other things which I need to serialize, and they’ll probably face the same problem.

So is there a better solution? Am I doing something wrong, DI-wise? I’ve just started working with DI and Ninject, but I can’t seem to find anyone who’s come up with a good way to serialize objects which got injected with service classes.

  • 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-31T16:18:44+00:00Added an answer on May 31, 2026 at 4:18 pm

    In my experience, only service classes should have dependencies, and service classes should never be subject to serialization.

    The fact that you have a class that you want to serialize, but which relies on an injected service, seems like a code smell to me.

    It’s difficult to tell exactly what you’re trying to accomplish with Bar, but from what I can see, I’d suggest making Pickle be a POCO, and using a List<Pickle> instead of a custom Bar class.

    Or, if Bar is intended to have other serializable information besides the Pickles, make Bar a POCO with a Pickles property:

    public class Bar
    {
        public string Name {get;set;}
        public List<Pickle> Pickles {get;set;}
    }
    

    Because POCOs won’t have dependencies, they shouldn’t require factories, so this class should be entirely serializable. If there are complex functions that you’d like to perform on Bars and Pickles, they should be abstracted out into separate utility services that take Bars and Pickles as their method parameters.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT

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.