شرکت فناوری اطلاعات رسمین

ارائه راهکارهای نرم افزاری برای واحد های کاری کوچک و بزرگ، بست و توسعه نرم افزار های کاربردی، طراحی وب سایت برای کلیه واحد های صنفی

شرکت فناوری اطلاعات رسمین

ارائه راهکارهای نرم افزاری برای واحد های کاری کوچک و بزرگ، بست و توسعه نرم افزار های کاربردی، طراحی وب سایت برای کلیه واحد های صنفی

شرکت فناوری اطلاعات رسمین

رسمین مخفف کلمات رستمی و مینو می باشد و با توجه به گسترش روز افزون استفاده از نرم افزار های کاربردی در سال 1392 تصمیم بر این شد که شرکت رسمین را برای ارائه خدمات کامپیوتری نظیر راهنمایی و مشاوره، ایجاد نرم افزار های سفارشی و وب سایت های مورد نیاز کارفرمایان را تأسیس نماییم.

طبقه بندی موضوعی
بایگانی

Simple Introducing About Dependency Injection

Basically, instead of having your objects creating a dependency or asking a factory object to make one for them, you pass the needed dependencies in to the constructor or via property setters, and you make it somebody else's problem (an object further up the dependency graph, or a dependency injector that builds the dependency graph). A dependency as I'm using it here is any other object the current object needs to hold a reference to.

One of the major advantages of dependency injection is that it can make testing lots easier. Suppose you have an object which in its constructor does something like:

public SomeClass() {
    myObject = Factory.getObject();
}

This can be troublesome when all you want to do is run some unit tests on SomeClass, especially if myObject is something that does complex disk or network access. So now you're looking at mocking myObject but also somehow intercepting the factory call. Hard. Instead, pass the object in as an argument to the constructor. Now you've moved the problem elsewhere, but testing can become lots easier. Just make a dummy myObject and pass that in. The constructor would now look a bit like:

public SomeClass (MyClass myObject) {
    this.myObject = myObject;
}

Most people can probably work out the other problems that might arise when not using dependency injection while testing (like classes that do too much work in their constructors etc.) Most of this is stuff I picked up on the Google Testing Blog, to be perfectly honest...

 

#Reference

  • احمد مینو

نظرات  (۰)

هیچ نظری هنوز ثبت نشده است
ارسال نظر آزاد است، اما اگر قبلا در بیان ثبت نام کرده اید می توانید ابتدا وارد شوید.
شما میتوانید از این تگهای html استفاده کنید:
<b> یا <strong>، <em> یا <i>، <u>، <strike> یا <s>، <sup>، <sub>، <blockquote>، <code>، <pre>، <hr>، <br>، <p>، <a href="" title="">، <span style="">، <div align="">
تجدید کد امنیتی