λ°λ©ν
λ₯΄μ λ²μΉ Law of Demeter
λ°λ©ν
λ₯΄λ κ·Έλ¦¬μ€ μ νμ λμ€λ μΆμμ μ μ΄λ€. λ‘λ§μ νμμλ μΈλ μ€ Ceres λΌκ³ λΆλ¦¬λ λ°λ‘ κ·Έ μ . νμ§λ§, κ·Έ λ°λ©ν
λ₯΄νκ³ μ΄ λ²μΉνκ³ λ μκ΄μλ€λ κ²μ΄ ν¨μ . [μν€νΌλμ]1μμλ λ°λ©ν
λ₯΄μ λ²μΉμ μλμ κ°μ΄ μ μνκ³ μλ€.
λ°λ©ν
λ₯΄μ λ²μΉμμλ μ΄λ€ κ°μ²΄ Oμ λ©μλ mλ λ€μκ³Ό κ°μ μ’
λ₯μ κ°μ²΄μ μλ λ©μλλ€λ§ μ€νμν¬ μ μλ€.
m μμμ λ§λ€μ΄μ§ κ°μ²΄
Oκ° μ§μ κ΄λ¦¬νλ μ½€ν¬λνΈ κ°μ²΄
mμ μ€μ½ν μμμ Oκ° μ κ·Ό κ°λ₯ν μ μλ³μ
μ’ λ§μ΄ μ΄λ €μ΄λ°, Richard Carrμ [The Law of Demeter]2 ν¬μ€νΈμ μ’ λ μ¬μ΄ μ€λͺ
μ΄ μλ€.
μ΄λ€ ν΄λΌμ€μ λ©€λ² β λ©μλ λλ μμ± β λ λ°λμ λ€μκ³Ό κ°μ κ°μ²΄λ€μ λ©€λ²λ€λ§μ μ€νμμΌμΌ νλ€:
ν΄λΉ λ©μλ λλ μμ±μ΄ μ μΈλ κ°μ²΄
λ©μλμ νλΌλ―Έν°λ‘ 보λ΄μ§ κ°μ²΄
λ©μλ λλ μμ±μ΄ μ§μ μ΄κΈ°νμν¨ κ°μ²΄
νΈμΆμ μν λ©μλ λλ μμ±μΌλ‘μ κ°μ ν΄λΌμ€ μμμ μ μΈλ κ°μ²΄
μλ μμ μ½λλ₯Ό 보μ. ASP.NET MVC μΉμ¬μ΄νΈλ₯Ό κ°λ°νλ€λ³΄λ©΄ μ½νΈλ‘€λ¬μμ νν λ³Ό μ μλ μν©μ΄λ€.
public class ProductController : Controller { private IProductService _service; public ProductController(IProductService service) { this._service = service; } public ActionResult Index() { var products = this._service.Repository.Get(); return View(products); } } public class ProductService : IProductService { public ProductService(IProductRepository repository) { this.Repository = repository; } public IProductRepository Repository { get; private set; } }
μμ μ½λμμ Index μ‘μ
μ 보면 λλ΅ μμμ΄ κ°λ₯νκ² μ§λ§ ProductServiceλΌλ μλΉμ€ λ μ΄μ΄ μμμ ProductRepositoryλΌλ λ°μ΄ν° 리ν¬μ§ν 리 ν¨ν΄μ ν΅ν΄ CRUDλ₯Ό ꡬννκ³ μλ€. Index μ‘μ
μ μ 체 μ ν 리μ€νΈλ₯Ό 보μ¬μ£Όλ λ·°λ₯Ό κ°κ³ μμ΄μ μ 체 μ ν 리μ€νΈλ μλΉμ€ μμ ꡬνλ 리ν¬μ§ν 리μ Get λ©μλλ₯Ό ν΅ν΄ κ°μ Έμ€κ² λλ€. μ΄λ κ² λ©μλ 체μ΄λμ νλ κ²μ΄ λ°λ‘ λ°λ©ν
λ₯΄μ λ²μΉμ μλ°νλ κ²μ΄ λλ€. ProductController κ°μ²΄λ μμ±μλ₯Ό ν΅ν΄ λ³μλ‘ λ°μ ProductService κ°μ²΄μ λ©μλ λλ μμ±μ νΈμΆν΄μΌ νμ§ κ·Έ λ΄λΆμ μλ ProductRepository κ°μ²΄μ Get λ©μλλ₯Ό μ§μ νΈμΆν΄μλ μλλ€. ProductRepository κ°μ²΄μ νμ¬ μνκ° nullμ΄λΌλ©΄ ν΄λΉ μ½λλ NullReferenceExceptionμ λμ§κΈ° λλ¬Έμ΄λ€. λ°λΌμ ProductService ν΄λΌμ€ μμ μΆκ°μ μΈ λ©μλλ₯Ό μ μΈν΄μ£Όλ λ°©μμΌλ‘ 리ν©ν λ§μ ν΄μΌ νλ€.
public class ProductController : Controller { private IProductService _service; public ProductController(IProductService service) { this._service = service; } public ActionResult Index() { var products = this._service.GetProducts(); return View(products); } } public class ProductService : IProductService { private IProductRepository _repository; public ProductService(IProductRepository repository) { this._repository = repository; } public IList<Product> GetProducts() { return this._repository.Get(); } }
μ¦ ProductRepository κ°μ²΄λ₯Ό public μμ±μ΄λ νλλ‘ λλ κ²μ΄ μλλΌ λ΄λΆμ μΌλ‘ encapsulation μν€κ³ ProductRepository ν΄λΌμ€μ λ©€λ²λ ProductService ν΄λΌμ€μ λ©€λ²λ₯Ό ν΅ν΄ νΈμΆνλ λ°©μμΌλ‘ νκ² λλ©΄, ProductController ν΄λΌμ€λ μ§μ μ μΌλ‘ κ΄λ ¨μ΄ μλ μ½€ν¬λνΈμΈ ProductSerivceμ λν΄μλ§ ν΅μ κΆμ κ°μ§ μ μμ΄μ λ³΄λ€ μμ νκ³ μ μ°ν μ½λλ₯Ό μμ±ν μ μκ² λλ€.
μ΄λ°μμΌλ‘ λ©μλ 체μ΄λμ μ΅λν μ€μ΄λ κ²μ΄ λ°λμ§ν κ°μ²΄μ§ν₯ νλ‘κ·Έλλ°μ΄λΌκ³ ν μ μκ² λ€. νμ§λ§ μ΄λ κ² νλ‘κ·Έλλ°μ νκ² λλ©΄ μΆκ°μ μΈ λ©μλλ₯Ό μμ±ν΄μΌ νλ λΆλ΄μ΄ μκΈ°κ² λλλ°, μ΄κ²μ΄ κΌ λΆμ μ μ΄λΌκ³ λ ν μ μλ κ²μ΄ κ°μ²΄ μ¬μ΄μ μμ‘΄μ±μ μ΅μννλ λ°©μμΌλ‘ μ μ°νκ² κ°λ°μ ν μ μκΈ° λλ¬Έμ΄λ€.
λ¨, LINQλ₯Ό μ°λ μν©μ΄λΌλ©΄ μκΈ°κ° λ¬λΌμ§λ€. LINQμμλ νΉμ±μ λ©μλ 체μ΄λμ΄ νμμΌ μ λ°μ μλμ§λΌ, μ΄ λ°λ©ν
λ₯΄μ λ²μΉμμ λ²μ΄λ μ μλλ°, κ·Έ μ΄μ λ λ©μλ 체μ΄λμ νλ κ²κ³Ό μκ΄μμ΄ νμ 리ν΄νμ
μ΄ λμΌνκΈ° λλ¬Έμ΄λ€. μμ μμ μ½λμ λμ¨ ProductRepository ν΄λΌμ€μ Get λ©μλλ μλ§λ λ΄λΆμ μΌλ‘ μλμ κ°μ΄ ꡬνμ΄ λμ΄ μμ κ²μ΄λ€.
public class ProductRepository : IProductRepository { private CompanyDataContext _context; public ProductRepository(ICompanyDataContext context) { this._context = context as CompanyDataContext; } public IList<Product> Get() { return this._context .Products .Where(p => p.IsActive) .OrderBy(p => p.DateRegistered) .ToList(); } }
μ¬κΈ°μ Products, Where κ·Έλ¦¬κ³ OrderByλ λͺ¨λ λμΌν IEnumerable<Product> κ°μ²΄λ₯Ό λ°ννλ€. μ¦ LINQλ₯Ό μ΄μ©ν λ©μλ 체μ΄λμ κ²½μ° λ©μλλ§λ€ λμΌν λ°μ΄ν° νμ
μ λ°ννκΈ° λλ¬Έμ μ΄ λ°λ©ν
λ₯΄μ λ²μΉμ μλ°νμ§ μκ³ μμ νκ² μ¬μ©ν μ μλ€.
[Law of Demeter]1 from Wikipedia
[The Law of Demeter]2 by Richard Carr
http://en.wikipedia.org/wiki/Law_of_DemeterΒ β©οΈ β©οΈ
http://www.blackwasp.co.uk/LawOfDemeter.aspxΒ β©οΈ β©οΈ