Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi ,

Needed a best way to load the dependent instances which is related with the parent instance creation .

For ex : Class A , have to navigation properties or depended on B and C classes.

So for when we create an instance of Class A we should be loaded with the dependent classes.

Usually we can do achieve this by instantiating the dependent classes(B,C) inside the Class A constructor which obviously is not a good practice because of the tight coupling.

The other way may be using a dependency injection .

But wanted to know whether this same can be achieved with any other design methodology or pattern by which the dependent classes are instantiated in a lazy loaded way and without the coupling.

Appreciate your help in advance.
Posted
Comments
JoCodes 14-Aug-13 15:39pm    
Ok SA, I ll bit elaborate on the concern.

Technology Used : asp.net MVC3,EF4.1, DBFirst Approach( Not yet using DI apporach)
Layered Architecture -View, Controller, Model, ModelView , Service, Repository
The UI representation contains a multilevel treeview structure ( not using any patterns like composite ) since dont know much how to design the tree structure in this scenario. So right now using class 1 ( parent ) ,class 2 ( child ) ... Class 5 . which means the lower level dependencies gets increased while we are instantiating the classes here. That was the reason didnt want a construction dependent classes instantiate or a property instantiate . Just wanted to know any other recommended way to do it.

Thanks

To reduce coupling, extract interfaces from the classes (some version of Visual Studio can do that), and use the interfaces in the parent class.
Nowadays, Dependency Injection is a common way to get the child objects into the parent class, typically via the constructor (in its signature, you'll use the interfaces instead of the concrete classes). The child objects can also be injected into public properties of the parent class (most DI containers support this feature).
Some years ago, "Service Location" was proposed see http://www.martinfowler.com/articles/injection.html[^]): the parent class knows some "ServiceLocator" (often a DI container), and creates its childs via that ServiceLocator. Some people think that that is bad practise and even call it an anti-pattern. On the other hand, this approach is easy to understand and looks stright forward.
 
Share this answer
 
Comments
JoCodes 17-Aug-13 5:09am    
Thanks Bernard for your valuable tips which I ll be surely keep in mind
First of all, if Class A depends on Class B or has navigation to Class B, Class A is tightly coupled to Class B. For example, you have a User object which has a property that is an Address object. A good model means that you can't change the Address object for another Address object as it breaks your domain model.

Moving on, your only other option is to do what is called Lazy Loading. This is achieved using an accessor.

C#
public class User
{
    private Address address = null;

    public Address Address
    {
        get
        {
            if (address == null)
                address = new Address(); //Load the address object

            return address;
        }

        set { address = value; }
    }
}


Hope this helps!
 
Share this answer
 
Comments
JoCodes 14-Aug-13 15:25pm    
Thanks Virus .. Can you please mention the other option too as this situation is used frequently..Why I was mentioning that was because my implementation was for a multilevel tree implementation and has got lots of dependencies ( almost 8 dependencies for a class ). In these case is it advisable to use the properties as you mentioned or a dependency injector(I was hesitating here bcos mostly we use the dependency injection to decouple in the layer level ).. Thanks again
virusstorm 15-Aug-13 13:36pm    
Your choice in how you want to load the data, dependency injection or lazy loading, heavily depends on the framework and design pattern (if you are following any) that you want to make use of. Obviously maintainability and performance are key factors you need to consider.

This Stackoverflow article can point you to performance related things:
http://stackoverflow.com/questions/5658142/comparison-pros-cons-of-various-net-dependency-injection-frameworks

I also recommend taking a look at this book:
http://www.informit.com/store/applying-uml-and-patterns-an-introduction-to-object-9780131489066?aid=3B419801-6640-4A1F-A653-6CD00295FCDD

I keep this book by me (in electronic and hard copy) near me when I designing systems. It helps to spell out pros and cons with some of your design decisions and provides conceptually how to implement common design patters.
JoCodes 17-Aug-13 5:08am    
Thanks alot Virus i m referring to it ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900