开发者

Beginner question - Encapsulation and "Proxy Classes"

While learning c#, I have picked up the implication from reading other people's cod开发者_JAVA技巧e that it's good practice to separate certain classes from the program by use of a sort of "Proxy Class". Namely those programs with private data.

For instance, say I have database class that performs just about all of my database interactions. It has both a "Send data" and "Read Data" method.

What I also have is a Manager class, in which I have placed a private instance of the Database class. In this Manager class, I have also included a "Read Data from Database" and "Send Data to Database" method. All these do is accept the required parameters and use them to run the Database's Read/Write methods.

Other than a few misc methods whose code could easily be run from elsewhere, Manager's only purpose's to act as a messenger between the program and the Database class to prevent Database from needing to be instantiated directly in the form.

Am I on the right track here or am I just creating more work for myself by not simply creating a Database object in the form and running everything directly from there?


I'm with John Saunders on this - you're using the class in some realm of "correctness", but not for a real purpose.

As described in your example, a proxy class is used as an abstraction. Your executing code doesn't need to know anything about the data source - it just submits a request for data and gets data back as a response. The proxy hides this implementation detail from you - it could retrieve the data from a flat file, a database, a web service; it really doesn't matter. This makes your code more flexible because it's not tied to a specific database implementation, and it also makes it testable because you can inject some other class in there that provides test ("mock") data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜