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.