简体   繁体   中英

Exchanging data between classes - DI, IoC

My setup is:

class ModelA { ... }
interface IModelARetriever {  IEnumerable<ModelA> GetObjects(); }

class ModelB { ... }
class DomainObject { List<ModelB> ModelBList; }

DomainObject.ModelBList could be appended using manipulated data from lists of ModelA objects, but not neccessarily. Where should I put the logic for this?

Should I create a method in DomainObject that takes a IEnumerable<ModelA> ? That would mean changing the DomainObject for every possible source of data that can create objects of ModelB.

Should I create a separate interface ModelBFactory and extend that? This sounds best, but just want an expert opinion.

If I've read the question correctly, ModelA objects can be converted into ModelB objects. I'd say that calls for a ToModelB() method on ModelA . If the conversion is straightforward (say, just copying a subset of ModelA 's property values), I'd implement it in ToModelB() directly; if it's more complex (say, conditional copying of some property values, combination of values, arithmetic, etc) I'd have a ModelAConverter class which did the job.

I'd then add the AddModelAObjects() method you mentioned to DomainObject . You then have two choices of how you convert the ModelA s into ModelB s - you either have the ModelAConverter injected into DomainObject 's constructor and do the conversion in AddModelAObjects() , or into ModelA 's constructor and do the conversion in ToModelB() . I'd probably go with the latter, but it depends on what else the objects have to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM