实例化依赖类而不是在构造函数中执行它的最佳方法 [英] The best way to instantiating the dependent classes instead of doing it in Constructor

查看:91
本文介绍了实例化依赖类而不是在构造函数中执行它的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





需要一种加载与父实例创建相关的相关实例的最佳方法。



例如:A类,必须导航属性或依赖于B和C类。



因此,当我们创建A类实例时,我们应该加载依赖类。



通常我们可以通过在类A构造函数中实例化依赖类(B,C)来实现这一点,这显然不是良好的做法,因为紧耦合。



另一种方式可能是使用依赖注入。



但是想知道这是否同样可以使用任何其他设计方法或模式来实现依赖类以延迟加载的方式实例化而没有耦合。



提前感谢您的帮助。

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.

推荐答案

要减少耦合,从类中提取接口(某些版本的Visual Studio可以这样做),并使用父类中的接口。

现在,依赖注入是将子对象引入父类的常用方法,通常通过构造函数(在其签名中,您将使用接口而不是具体类)。子对象也可以注入父类的公共属性(大多数DI容器支持此功能)。

几年前,服务位置被提议参见http://www.martinfowler.com/articles/injection.html [ ^ ]):父类知道一些ServiceLocator(通常是DI容器),并通过该ServiceLocator创建其子节点。有些人认为这是不好的做法,甚至称之为反模式。另一方面,这种方法很容易理解,看起来很直接。
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.


首先,如果A类依赖于B类或导航到B类,A类紧耦合例如,您有一个User对象,其属性是Address对象。一个好的模型意味着你不能更改另一个Address对象的Address对象,因为它会破坏你的域模型。



继续前进,你唯一的另一个选择是做什么叫做延迟加载。这是使用访问器实现的。



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.

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!


这篇关于实例化依赖类而不是在构造函数中执行它的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆