I usually use IoC frameworks to inject dependencies that are services. Is it ok to mark classes that are data objects as IoC components?

To be more clear I will give an example.

I have an abstract data class A in one assembly.

  public abstract class A
     //possibly some method signatures that should be overriden in derived classes

I expect some assemblies that reference my assembly to define classes derived from A. The CustomIoCConcrete attribute is used to mark all the class types that I want to inject later, its parameters are a name and a version of the implementation (there can be more that one with the same name, but different namespace).

  [CustomIoCConcrete("name", 2)]
  public class B : A
     //possibly some overriden methods and fields defined by client.

Then I want to gather all the types of these data classes in my Holder class using IoC. I want my Holder class to create an instance of some of the class that it is holding types for. Collection injected is of IMetadata type - a type binding the IoCBase type with an object that is Marked with CustomIoCConcrete. This type has a method CreateInstance() that will create an instance of desired type.

class Holder : IHolder
    readonly IEnumerable<IMetadata<A, CustomIoCConcrete>> versionedClasses;

    public DocumentDataSegmentsHolder(
        IEnumerable<IMetadata<A, CustomIoCConcrete>> versionedClasses)
        this.versionedClasses = versionedClasses;

    public A GetObject(string name, int version)
        return versionedClasses.SingleOrDefault(o => o.Metadata.Name == name
            && o.Metadata.Version == version)?.CreateInstance();

So to sum up, I am using IoC to gather types that inherit from a certain base class and are marked with a certain attribute. Then I use the class holding these types to create a concrete instance of one of the types that it is holding. The definitions will be created by user of the library. Other way would be to use factories, but then the user would have to create factories and data classes.

  • I'm not certain I understand what you mean by "data class". Is "Holder" the data class or "B"? What makes it a "data class" as opposed to some other kind of class? – Kamil Drakari Jan 25 '19 at 18:40
  • @KamilDrakari The data class would be a class which main purpose is to hold data. Like a model in MVC, or a DTO. As opposed to a service of which main purpose would be to contain logic and operate on data classes. "B" class is the data class here, "Holder" class is a service. – Mikołaj Mularczyk Jan 25 '19 at 19:12
  • where does the data in your data classes come from? – Ewan Jan 27 '19 at 20:03
  • @Ewan Users override "Fill" methods in their implementations of the data class. "A" has a virtual "Fill" method. The method accepts an object as an argument and this object is then used to fill the properties defined in "B" class. I didn't write it in the question because I thought it is not so relevant here. – Mikołaj Mularczyk Jan 27 '19 at 20:25
  • How does the Holder do anything with B when GetObject only returns A? Its not clear what problem you are trying to solve – Ewan Jan 27 '19 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.