Implementing application decoupling with dependency inversion and dependency injection

Source: Internet
Author: User

First, we virtual a system environment (E-SHOP), an application of the online store, which has an application: Gets all the product information under the specified classification. We follow the traditional way of thinking to achieve. Figure 1 shows the design diagram for the system.

Create a new Class library project Eshop.service. Then add the appropriate classes to the project.

Namespace Eshop.service
{
public class Product
{
}

public class Productrepository
{
Public ilist<product> getproductsbycategory (int categoryId)
{
ilist<product> products = new list<product> ();
Perform database operations
return products;
}
}

public class Productservice
{
Private Productrepository productrepository;

Public Productservice ()
{
Productrepository = new Productrepository ();
}

Public ilist<product> getproductsbycategory (int categoryId)
{
Ilist<product> Products;
String storagekey = String.Format ("products_in_category_id_{0}", categoryId);
Products = (list<product>) HttpContext.Current.Cache.Get (Storagekey);
if (products = = null)
{
Products = Productrepository.getproductsbycategory (categoryId);
HttpContext.Current.Cache.Insert (Storagekey, products);
}
return products;
}
}
}

Is it possible to find any unreasonable from the above program section? I summed up a bit, roughly the following points:

1. Productservice relies on productrepository, and once the latter's API changes, the former will inevitably change as a result.

2. It is difficult to test the Productservice method, because Productrepository is not really connected to the database, and also relies on HttpContext, which is tightly coupled.

3. HttpContext is currently used for caching, and Productservice will change if a caching mechanism (such as velocity or memcached) is to be replaced.

Based on the unreasonable points above, we will reconstruct and optimize each of them.

For 1: We use the dependency inversion principle (Dependency inversion Principle)-- which relies on abstractions rather than concrete implementations to solve. We joined the interface iproductrepository.

public interface Iproductrepository
{
ilist<product> getproductsbycategory (int categoryId);
}
public class Productrepository:iproductrepository
{
//...
}
public class Productservice
{
Private Iproductrepository productrepository;

Public Productservice ()
{
Productrepository = new Productrepository ();
}
//...
}

For 2: We use the Dependency injection principle (Dependency injection Principle)-- by injecting abstractions into constructors, methods, or properties to resolve them. We modify the Productservice constructor.

public class Productservice
{
Private Iproductrepository productrepository;

Public Productservice (Iproductrepository productrepository)
{
This.productrepository = productrepository;
}
//...
}

For 3: We use the adapter mode (Adapter pattern)-to transform the existing interface to be compatible with the target interface that the customer expects . We have added interface Icachestorage and class Httpcontextcacheadapter.

public interface Icachestorage
{
void Remove (string key);
void Store (string key, object data);
T retrieve<t> (string key);
}

public class Httpcontextcacheadapter:icachestorage
public class Httpcontextcacheadapter:icachestorage
{
public void Remove (string key)
{
HttpContext.Current.Cache.Remove (key);
}

public void Store (string key, Object data)
{
HttpContext.Current.Cache.Insert (key, data);
}

Public T retrieve<t> (string key)
{
T item = (t) HttpContext.Current.Cache.Get (key);
if (item = = NULL)
{
Item = default (T);
}
return item;
}
}

public class Productservice
{
Private Iproductrepository productrepository;
Private Icachestorage cachestorage;

Public Productservice (Iproductrepository productrepository, Icachestorage cachestorage)
{
This.productrepository = productrepository;
This.cachestorage = Cachestorage;
}

Public ilist<product> getproductsbycategory (int categoryId)
{
Ilist<product> Products;
String storagekey = String.Format ("products_in_category_id_{0}", categoryId);
Products = cachestorage.retrieve<list<product>> (Storagekey);
if (products = = null)
{
Products = Productrepository.getproductsbycategory (categoryId);
Cachestorage.store (Storagekey, products);
}
return products;
}
}

To integrate the above solutions, post the refactoring complete code here:

Namespace Eshop.service
{
public class Product
{
}

public interface Iproductrepository
{
ilist<product> getproductsbycategory (int categoryId);
}

public class Productrepository:iproductrepository
{
Public ilist<product> getproductsbycategory (int categoryId)
{
ilist<product> products = new list<product> ();
Perform database operations
return products;
}
}

public interface Icachestorage
{
void Remove (string key);
void Store (string key, object data);
T retrieve<t> (string key);
}

public class Httpcontextcacheadapter:icachestorage
{
public void Remove (string key)
{
HttpContext.Current.Cache.Remove (key);
}

public void Store (string key, Object data)
{
HttpContext.Current.Cache.Insert (key, data);
}

Public T retrieve<t> (string key)
{
T item = (t) HttpContext.Current.Cache.Get (key);
if (item = = NULL)
{
Item = default (T);
}
return item;
}
}

Sometimes for convenience, we define an empty object (empty cache adapter)
public class Nullcacheadapter:icachestorage
{
public void Remove (string key)
{
}

public void Store (string key, Object data)
{
}

Public T retrieve<t> (string key)
{
return default (T);
}
}

public class Productservice
{
Private Iproductrepository productrepository;
Private Icachestorage cachestorage;

Public Productservice (Iproductrepository productrepository, Icachestorage cachestorage)
{
This.productrepository = productrepository;
This.cachestorage = Cachestorage;
}

Public ilist<product> getproductsbycategory (int categoryId)
{
Ilist<product> Products;
String storagekey = String.Format ("products_in_category_id_{0}", categoryId);
Products = cachestorage.retrieve<list<product>> (Storagekey);
if (products = = null)
{
Products = Productrepository.getproductsbycategory (categoryId);
Cachestorage.store (Storagekey, products);
}
return products;
}
}
}

Attach the reconstructed plan:

Implementing application decoupling with dependency inversion and dependency injection

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.