"andrea" <
[email protected]> a écrit dans le message de (e-mail address removed)...
| Now the things has a lot of sense. But I 've a bit of confunsion again.
| I cannot imagine a working scenario.
|
| I start from this assumption (that is the 3-tier model).
|
| DAL -> BAL -> GUI -> BAL -> DAL
| And nothing more.
|
|
| This mean that I get a Dataset of whatever I need to DAL and pass it back
| to BAL
| BAL has a definition of the object Book (or whatever it should be), read
| the dataset
| do all it works (in my case preapare a List<Book>) and return it to GUI
that
| just read it.
Not quite. You should not be getting the Dataset from the DAL, instead, you
should be getting the DAL to retrieve a Dataset and then populate the list
from that Dataset before returning it to the business layer (in the GetList
method)
| So in few words:
|
| GUI call a List<Book> Bal.GetList()
| the method call a DAL, DAL return the dataset, BAL read and assign to the
| returning list.
| Right?
Not necessarily. If you use a proper GUI framework, then the business layer
can ask the GUI layer for a "Presenter" for the list or object that it has
already retrieved from the DAL. As I said, don't get the Dataset from the
DAL, fill the list from a Dataset inside the DAL's GetList method.
| If yes, I cannot understand why in your sample from BAL you create an
object
| of type book
| letting know to DAL about the book itself.
| DAL cannot work with the type book, at least, I've tried and got only
error.
I didn't let the DAL know about the type Book, I used either an Object or
the <T> parameter to return the newly created Book, but the DAL only knew
that it was creating an Object. It was only in the business layer that I
assigned the result of the CreateObject method to a Book.
| Trying to use one of the two Activator, let me get an object of type X
that
| comes
| from external caller. It's a strictly-typed object and compiler know of
what
| type is
| but if I try to do something like this in IDE, I got compile error since
| b is a generic
| object.
|
| // DAL
|
| public List<T> GetList<T>()
| {
| Object b = Activator.CreateInstance(typeof(T));
| List<T> lst = new List<T>();
|
| b.Author = "pippo"; // b for the IDE is a generic object
I'm sorry but you can't treat these objects as strictly typed inside the
DAL.
In these kind of situations, it is usual to create a "TypeMap" so that you
can map types to tables and properties to fields. This allows you to use
different names for fields and properties, etc.
For example, your Book class has the following properties :
Book
Author
Title
ISBN
....but your database has the following columns :
T_BOOK
C_AUTHOR
C_TITLE
C_ISBN
Now you can use a Dictionary<Type, TableMap> to hold the details of the
tables
that match certain types.
public class TableMap
{
private string name;
private Dictionary<string, string> columns = new Dictionary<string,
string>();
public TableMap(string name)
{
this.name = name;
}
public string Name
{
get { return name; }
}
public void AddColumn(string propertyName, columnName)
{
if (! columns.ContainsKey(propertyName))
columns.Add(propertyName, columnName);
}
public string GetColumn(string propertyName)
{
return columns[propertyName];
}
}
public static class DAL
{
private static Dictionary<Type, TableMap> tables = new Dictionary<Type,
TableMap>();
public static void RegisterType(Type type, string tableName)
{
TableMap newItem = null;
if (! tables.ContainsKey(type))
{
newItem = new TableMap(tableName);
tables.Add(type, newItem);
/// alternative 1
// here you need to access something like an XML file that contains a list
of
// propertyName=columnName pairs and iterate through them, adding columns
// to the newly created Table object
string columnName;
foreach(string propertyName in /*XMLListing*/)
{
columnName = // get value from XMLListing
newItem.AddColumn(propertyName, columnName);
}
/// alternative 2
// or you can iterate through the properties of the type
PropertyInfo[] propInfos = type.GetProperties();
string columnName;
foreach(PropertyInfo property in propInfos)
{
columnName = "C_" + propertyName;
newItem.AddColumn(property.Name, columnName);
}
}
public static string GetTableName(Type type)
{
return tables[type].Name;
}
public GetColumnName(Type type, string propertyName)
{
return tables[type].GetColumnName(propertyName);
}
public T GetObject<T>()
{
T result = Activator.CreateInstance<T>();
Dataset ds = // create dataset from
PropertyInfo[] propInfos = typeof(T).GetProperties();
foreach(PropertyInfo pi in propInfos)
{
}
}
}
There is more to do but if I did it all, where would the fun be for you
Does this help ?
Joanna