A typical factory Model

Source: Internet
Author: User
Namespace BLL {public class empbll {idal. empdal ITU = dalfactory. emp_factory.createusers (); // = new idal. empdal? What are the benefits of this? Public datatable getall () {return ITU. getall () ;}} namespace idal {public interface empdal {datatable getall () ;}} namespace dalfactory {public class emp_factory {static readonly string Path = system. configuration. configurationmanager. deleetask[ "Dal"]; public static idal. empdal createusers () {string classname = path + ". SQL _emp "; Return (idal. empdal) assembly. load (PATH ). createinstance (classname ); // How do I convert it to the interface type? }}} Namespace sqlserverdal {class SQL _emp: idal. empdal {dbunit. sqlaccess SQL = new dbunit. sqlaccess (); public system. data. datatable getall () {return SQL. executedataset ("select * From tab_emps join tab_roles on tab_emps.roleid = tab_roles.roleid "). tables [0];} is a typical factory model.

The purpose is to meet the design principles of "disabling modifications and developing extensions.
1. idal. empdal ITU = dalfactory. emp_factory.createusers (); // The interface cannot be instantiated. Only one instance can be attached to the interface. If a class inherits the empdal interface, you can use new to instance and assign values, in this way, the implementation is coupled with the interface, which is not easy to expand in the future. If you use a factory to create an instance, you only need to modify the system. configuration. configurationmanager. appsettings ["Dal"]; the value of the corresponding xml configuration document can replace the SQL statement with L, which can be expanded without re-compiling.

2. Return (idal. empdal) Assembly. Load (PATH). createinstance (classname); // how do I convert it to the interface type?
This is a polymorphism. The createinstance method is equivalent to the new method. A class named classname is instantiated here. This class must inherit the idal. empdal interface.

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.