Click here to Skip to main content
15,898,374 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I'm a hobby programmer, trying to learn C#.

I'm writing a class library project to sit between my SQL database and my windows forms project. The class library project uses LINQ to access the database. I want to save a DataGridViewRowCollection in the forms project to the database, but the class library project does not know what a DataGridViewRowCollection is. I could add a reference to System.Windows.Forms to the class library project, but that just seems 'wrong'.

So my questions are:

1) Is it 'wrong' (i.e. poor practice) to just add a System.Windows.Forms reference to a class library project?

2) If it is wrong, what is the best object to cast a DataGridViewRowCollection to so the class library can use it?

3) Is there another way, besides trial and error and experience, to discover what can and cannot be cast to what?

Thanks.
Posted

wrote:
1) Is it 'wrong' (i.e. poor practice) to just add a System.Windows.Forms reference to a class library project?


Well, It is really easy to add a reference to System.Windows.Forms and it will not matter as such as the original windows application loads both System.Windows.Forms as well as your class library. So there is no problem in regards to the performance.

The only thing that concerns is why do we create Class Libraries. Actually we create class library to make layers between the application end and database end. Its main motive is to have independent modules to access the database which you might add in another project and directly use those functions(Code reusability). So Our motive should be to factor code in such a way that all are independent. If you add Windows.Forms in your class library it will make the class library totally dependent on the Application and it is unseparable. Thus if this is going to be your motive, I would rather add the whole class library into my forms application rather than writing out unnecessary dependent dlls.


wrote:
2) If it is wrong, what is the best object to cast a DataGridViewRowCollection to so the class library can use it?


So better approach is to expose a method, that may take a list of DataRow element. You build the Datarow from the application and pass to the function into the layer. The layer will save those into the database. Thus the presentation layer is made independent to the data layer.


wrote:
3) Is there another way, besides trial and error and experience, to discover what can and cannot be cast to what?


Of course. Just inspect each object by going to the Definition into the Object Explorer. You see the class library and interface which the actual object is inherited from. You can case only those which are within the family of those interfaces.

For example you can cast
IDbConnection con = new SqlConnection();
IDbConnection con = new OleDbConnection();


as both of them are inherited from a common interface IDbConnection.

I hope you got the ideas clear.
Cheers.
Happy Learning.
:thumbsup:
 
Share this answer
 
That was very helpful. Thanks for your time. Dell :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900