120 likes | 466 Views
Practical Uses of Interfaces in C#. By Kevin S. Goff. Who is Kevin S. Goff?. Founder and Principal Consultant: Common Ground Solutions 18 years experience - NET/SQL Server/Crystal Reports/Visual FoxPro 2005 Microsoft .NET MVP for C#
E N D
Practical Uses of Interfaces in C# By Kevin S. Goff
Who is Kevin S. Goff? • Founder and Principal Consultant: Common Ground Solutions • 18 years experience - NET/SQL Server/Crystal Reports/Visual FoxPro • 2005 Microsoft .NET MVP for C# • Author of “The Baker’s Dozen” series in CoDe Magazine(Productivity Tips for .NET, Crystal Reports, and T-SQL) • Speaker at 2005 DevTeach, MSDN CodeCamp, .NET User Groups • Common Ground Framework for .NET (free framework) • Awards/citations from the Dept. of Agriculture/large corporations • www.commongroundsolutions.net / kgoff@commongroundsolutions.net • My Blog: http://www.TheBakersDozen.net Uses of Interfaces - Kevin S. Goff
Definition of an interface • Interface is a contract that guarantees how a class will behave, and what properties and methods the class will contain. • When you create an instance of a class that implements the interface, you can be assured that the class will contain every PEM that the interface defined. • Think of the interface as the bullet point list of what will be in the class. Uses of Interfaces - Kevin S. Goff
Definition of an interface • OK…SO WHAT??? • What does that gain me??? • A popular INETA speaker has told people that they don't need to know about interfaces… Uses of Interfaces - Kevin S. Goff
Three examples of interfaces • Distributed computing (remoting) • Generic Data Binding • Reusable data maintenance forms • All examples use the Common Ground Framework for .NET (my free framework) Uses of Interfaces - Kevin S. Goff
Distributed computing (remoting) • Example: Login Authentication (must validate user ID/PW, via back-end objects) • IUserObject (one method, parms, and result) • Server side: • Methods that will be called via remoting must inherit from System.MarshalByRefObject • public class UserObject : CGS.Business.cgsBaseBusinessObject, Masonry.Interfaces.IUserObject • public class cgsBaseBusinessObject : System.MarshalByRefObject • Must register the object to be called via remoting through the registered TCP Channel TcpServerChannel Tcps = new TcpServerChannel(nTCPPort); ChannelServices.RegisterChannel(Tcps); RemotingConfiguration.RegisterWellKnownServiceType (typeof(Masonry.Server.BusinessObjects.UserObject), "UserObject" , WellKnownObjectMode.Singleton); • Client side (we don’t have the code, but we have interface and TCP port # • Create instance of interface object IUserObject oUserObject; ClientRemoteAccess oRemoteAccess = new ClientRemoteAccess(); oRemoteAccess.tInterface = typeof(IUserObject); oRemoteAccess.cServiceName = "UserObject"; oUserObject = (IUserObject)oRemoteAccess.GetAccessObject(); string cXMLResultSet = oUserObject.ValidateUser(oGlobals.cUserID, oGlobals.cUserPassword, oGlobals.nDataBaseKey); Uses of Interfaces - Kevin S. Goff
Generic Data Binding • Data bound controls work differently • Textbox is bound to different property than a checkbox, and a combobox has more properties • Define data binding interface • Define interfacenamespace CGS.Interfaces { public interface IBoundControl (BindControl, DtSource, DcSource) • Implement interface (PEMs) in data-bound controls • Textbox, combo box (additional properties), checkbox • Implements props of the interface, plus the BindControl method • Each BindControl behaves a little differently, but it doesn’t matter • On a form or container, we can loop through controls and bind easily ((IBoundControl)oControl).BindControl(); Uses of Interfaces - Kevin S. Goff
Reusable data maintenance forms • Generic data maintenance form: • public interface IDataMaintenanceForm • public interface ICriteriaContainer • public interface IResultContainer • public interface IEntryContainer Uses of Interfaces - Kevin S. Goff