I am used to making separate Business Object and List<> classes for my DB tables. Sometimes I just have a basic task in hand as displaying a list of year for a search box or having some values in a drop down.
Right now I am ending up with a lot of separate clas开发者_Go百科ses and List classes<> for these basic operations.
How can I make a single class for some basic tasks as these by using generics? I want to have single class for some methods like get current year, employee names, Department Name/Code, Title etc. Please give me an example. I am using .NET 2.0.
Edit After some searching, I found that I can achieve similar tasks by creating a DTO namespace.
What I don't get right now is how to create multiple classes inside my DTO class. Say 1 class for just returning 'year', One for 'Employee Name/Code' and so on inside the single DTO class.
You can map your classes with DB Table manually or you can use NHibernate which is an advanced ORM which works quiet well with .Net 2.0.
Basically you can map your plain c# classes to DB table using a simple xml mapping file.
Example a class called Product can
public class Product
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public bool Discontinued { get; set; }
}
can be mapped to a DB table using following mapping file.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="FirstSolution"
namespace="FirstSolution.Domain">
<class name="Product">
<id name="Id">
<generator class="guid" />
</id>
<property name="Name" />
<property name="Category" />
<property name="Discontinued" />
</class>
</hibernate-mapping>
This also has good support for .Net Collections. There is sure some learning curve but the productivity benefit is worth.
More example can be found here.
http://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application.aspx
精彩评论