473,320 Members | 1,580 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Databinding of complex business layer classes

Hello,

I have a problem to use databinding with my business layer classes. My
data class does not have simple properties (string, int or datetime),
instead, all my properties are objects of the generic type Field<T>
(see sample code).

public class Employee
{
public Field<stringForename
{
get
{
if(m_Forename == null)
m_Forename = new Field<string>(....);

return m_Forename;
}
}

public Field<stringLastname
{
get
{
if(m_Lastname == null)
m_Lastname= new Field<string>(....);

return m_Lastname;
}
}

// The Employee class has many more properties with the Field<T>
datatype.
}
public class Field<T>
{
public T CurrentValue
{
get
{
// Get Field from database...
return ...;
}
set
{
// Save Value in Database

// Save last change date.

// Save NT-Username of the User who made the last changes
}
}

public DateTime LastChangeDate
{
get
{
return ...;
}
}

public string LastChangeUserName
{
get
{
return ...;
}
}

// I have some more informations for each Field...
}

When I now try to use databinding of a collection of those objects to
da grid (I use the Janus GridEx, but the problem is a general
databinding problem), I have the problem that the databinding
expression only accepts one-level-property names "Forename". What I
need is something like "Forename.CurrentValue", but that is not
possible.

I know that it would be possible to add every Property in the Employee
class twice like

public string FornameValue
{
get
{
return this.Forname.CurrentValue;
}
}
But this is a ugly solution.

Does anyone have an idea how I can change the Field<Tclass so that I
can use it with Databinding?

Regards
Dirk

Jan 3 '07 #1
8 2061
"Dirk" <di********@gmail.coma écrit dans le message de news:
11*********************@s34g2000cwa.googlegroups.c om...

| I have a problem to use databinding with my business layer classes. My
| data class does not have simple properties (string, int or datetime),
| instead, all my properties are objects of the generic type Field<T>

Do not expose the Field<Tobjects as public properties. You really need to
create a base business class that is capable of holding a list of these
field objects and then access them the derived classes.

internal abstract class Field
{
private string name;

internal Field(string name)
{
this.name = name;
}

public string Name
{
get { return name; }
}
}

internal class Field<T: Field
{
private T value;

...

internal T Value
{
get { return ...; }
set { ... = value; }
}
}

public abstract class Base
{
private Dictionary<string, Fieldfields = new Dictionary<string,
Field>();

protected bool ContainsField(string fieldName)
{
return field.ContainsKey(fieldName);
}

protected void Add<T>(string fieldname)
{
fields[fieldname] = new Field<T>(fieldName);
}

protected T GetFieldValue<T>(string fieldName)
{
return fields[fieldName].Value;
}

protected void SetFieldValue<T>(string fieldName, T value)
{
fields[fieldName].Value = value;
}
}

public class Employee : Base
{
public string Forename
{
get
{
string fieldName = "Forename";

if (ContainsField(fieldName))
Add<string>(fieldName);

return GetFieldValue<string>(fieldName);
}
}

I have taken this one step further and used the idea of a Data Packet which
contains the list of fields and manages all sorts of other stuff under the
hood, but this should give you the general idea.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 3 '07 #2
"Joanna Carter [TeamB]" <jo****@not.for.spama écrit dans le message de
news: eL*************@TK2MSFTNGP04.phx.gbl...

Sorry, I forgot the cast on the fields in the accessors.

| protected T GetFieldValue<T>(string fieldName)
| {
*** return ((Field<T>) fields[fieldName]).Value;
| }
|
| protected void SetFieldValue<T>(string fieldName, T value)
| {
*** ((Field<T>) fields[fieldName]).Value = value;
| }

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 3 '07 #3
Dirk wrote:
Hello,

I have a problem to use databinding with my business layer classes. My
data class does not have simple properties (string, int or datetime),
instead, all my properties are objects of the generic type Field<T>
(see sample code).

public class Employee
{
public Field<stringForename
{
get
{
if(m_Forename == null)
m_Forename = new Field<string>(....);

return m_Forename;
}
}

public Field<stringLastname
{
get
{
if(m_Lastname == null)
m_Lastname= new Field<string>(....);

return m_Lastname;
}
}

// The Employee class has many more properties with the Field<T>
datatype.
}
public class Field<T>
{
public T CurrentValue
{
get
{
// Get Field from database...
return ...;
}
set
{
// Save Value in Database

// Save last change date.

// Save NT-Username of the User who made the last changes
}
}

public DateTime LastChangeDate
{
get
{
return ...;
}
}

public string LastChangeUserName
{
get
{
return ...;
}
}

// I have some more informations for each Field...
}

When I now try to use databinding of a collection of those objects to
da grid (I use the Janus GridEx, but the problem is a general
databinding problem), I have the problem that the databinding
expression only accepts one-level-property names "Forename". What I
need is something like "Forename.CurrentValue", but that is not
possible.

I know that it would be possible to add every Property in the Employee
class twice like

public string FornameValue
{
get
{
return this.Forname.CurrentValue;
}
}
But this is a ugly solution.

Does anyone have an idea how I can change the Field<Tclass so that I
can use it with Databinding?

Regards
Dirk
I like your general idea and I am struggling with similar issue. I ran
into a very good article by Alexander Gornik "Making ASP.NET databinding
via # work, without reflection"
(http://www.codeproject.com/aspnet/TypeDescriptors.asp)
Hope this helps.
The solution by Joanna is very feasible but undesirable due to the
strange and complex databinding expression you may need to use.
I would like to see her post a working small example.
Regards,
intrader
Jan 4 '07 #4
Dirk wrote:
Hello,

I have a problem to use databinding with my business layer classes. My
data class does not have simple properties (string, int or datetime),
instead, all my properties are objects of the generic type Field<T>
(see sample code).

public class Employee
{
public Field<stringForename
{
get
{
if(m_Forename == null)
m_Forename = new Field<string>(....);

return m_Forename;
}
}

public Field<stringLastname
{
get
{
if(m_Lastname == null)
m_Lastname= new Field<string>(....);

return m_Lastname;
}
}

// The Employee class has many more properties with the Field<T>
datatype.
}
public class Field<T>
{
public T CurrentValue
{
get
{
// Get Field from database...
return ...;
}
set
{
// Save Value in Database

// Save last change date.

// Save NT-Username of the User who made the last changes
}
}

public DateTime LastChangeDate
{
get
{
return ...;
}
}

public string LastChangeUserName
{
get
{
return ...;
}
}

// I have some more informations for each Field...
}

When I now try to use databinding of a collection of those objects to
da grid (I use the Janus GridEx, but the problem is a general
databinding problem), I have the problem that the databinding
expression only accepts one-level-property names "Forename". What I
need is something like "Forename.CurrentValue", but that is not
possible.

I know that it would be possible to add every Property in the Employee
class twice like

public string FornameValue
{
get
{
return this.Forname.CurrentValue;
}
}
But this is a ugly solution.

Does anyone have an idea how I can change the Field<Tclass so that I
can use it with Databinding?

Regards
Dirk
I may be mistaken here, but I have seen the syntax Forename_CurrentValue
in the # syntax.
Jan 4 '07 #5
"intrader" <in******@aol.coma écrit dans le message de news:
45**********************@roadrunner.com...

| The solution by Joanna is very feasible but undesirable due to the
| strange and complex databinding expression you may need to use.
| I would like to see her post a working small example.

Far from being a strange and complex databinding, it couldn't be simpler.

{
List<Employeeemployees = GetEmployeeList(...);

myGrid.DataSource = employees;

...
}

If you need to bind individual controls to an Employee, then all you do is

{
Employee e = new Employee();

textBox1.DataBindings.Add("Text", e, "Forename");

...
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 4 '07 #6
Hello Joanna, Intrader

thank you for your help.

First I tried "Forname_CurrentValue", but it did not work. Then I
retried "Forname.CurrentValue" and I recognized that it work readonly,
editing a column with such an Binding expression doesnt seem to work?

I will now analyze which of the two solutions I will realize.

regards
Dirk

Jan 4 '07 #7
"Josh" <Jo**@discussions.microsoft.coma écrit dans le message de news:
E4**********************************@microsoft.com...

| I am new to generics and would like to undersatnd the code you posted. I
| cannot get it to compile howver (even with the code change in your
subsequent
| post). Can you post a complete code that will compile?

There really isn't that much that doesn't work; after all I wrote the code
in the newsreader :-)

This is checked to compile and run :

// base non-generic class because we can't have use Field<T>
// for different types in a Dictionary that will only hold one type
internal abstract class Field
{
private string name;

internal Field(string name)
{
this.name = name;
}

public string Name
{
get { return name; }
}
}

// this is the real generic Field<Tclass that will be instantiated and
held
// in the Dictionary<string, Fieldin our base business class
internal class Field<T: Field
{
private T value;

internal Field(string name) : base(name) { }

internal T Value
{
get { return value; }
set { this.value = value; }
}
}

public abstract class Base
{
private Dictionary<string, Fieldfields = new Dictionary<string,
Field>();

protected Base()
{
// get list of publicly declared properties in class derived from this
class
PropertyInfo[] properties = GetType().GetProperties();

foreach (PropertyInfo property in properties)
{
// bind Field<to the type of the property
Type fieldType =
typeof(Field<>).MakeGenericType(property.PropertyT ype);

// create an array to hold the constructor args
object[] args = new object[] { property.Name };

// use Activator to create the Field<Tinstance
Field newField = (Field) Activator.CreateInstance(fieldType,
BindingFlags.Instance | BindingFlags.NonPublic, null, args, null, null);

// add the field to the private list
fields.Add(property.Name, newField);
}
}

protected T GetFieldValue<T>(string fieldName)
{
return ((Field<T>) fields[fieldName]).Value;
}

protected void SetFieldValue<T>(string fieldName, T value)
{
((Field<T>) fields[fieldName]).Value = value;
}
}

public class Employee : Base
{
public Employee() : base() { }

public string Forename
{
get { return GetFieldValue<string>("Forename"); }
set { SetFieldValue<string>("Forename", value); }
}
}

Does that help ?

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 4 '07 #8
Joanna Carter [TeamB] wrote:
"intrader" <in******@aol.coma écrit dans le message de news:
45**********************@roadrunner.com...

| The solution by Joanna is very feasible but undesirable due to the
| strange and complex databinding expression you may need to use.
| I would like to see her post a working small example.

Far from being a strange and complex databinding, it couldn't be simpler.

{
List<Employeeemployees = GetEmployeeList(...);

myGrid.DataSource = employees;

...
}

If you need to bind individual controls to an Employee, then all you do is

{
Employee e = new Employee();

textBox1.DataBindings.Add("Text", e, "Forename");

...
}

Joanna
Sorry for the characterization.
What you have described is quite simple and elegant.
Jan 27 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: D Witherspoon | last post by:
I am developing a Windows Forms application in VB.NET that will use .NET remoting to access the data tier classes. A very simple way I have come up with is by creating typed (.xsd) datasets. For...
2
by: Johann Blake | last post by:
I've been playing around trying to bind textbox controls and datagrids to typed datasets. Up until recently, I never used DataBindings. I always manually wrote the value into a textbox and stored...
1
by: mgonzales3 | last post by:
I create a business layer object w/public properties. the db access is also done from here. Its here I set the property values as well. So when my UI request is made all values are saved to the...
1
by: Nemisis | last post by:
hi guys, Currently converting an old classic asp system to a OOP asp.net application. We are building the new application using a 3 tier arcitecture and i was wondering about the following. ...
7
by: Vlado Jasovic | last post by:
Hello, I'm using typed dataset for databinding to windows controls and I'm having some problems. I'm trying to move all business logic to datatable column_changing events and the problem that...
25
by: Penelope Dramas | last post by:
Hello, I'm in a front of very serious .net redesign/rewrite of an old VB6 application. I had been asked to make it .NET 2.0 and would like to ask couple of questions regarding data access as...
2
by: grawsha2000 | last post by:
Greetings, I am developing this N-tier business app. The problem I'm facing is when I try to pass business objects (employees, dept..etc) from business tier to data tier,i.e., the add method in...
8
by: morleyc | last post by:
Hi, until recently i was quite happy to add data sources from mssql database in visual studio and drag the datasets directly onto the form this creating a directly editable form which worked well....
9
by: SAL | last post by:
Hello, I have a Dataset that I have table adapters in I designed using the designer (DataLayer). I have a business logic layer that immulates the DataLayer which may/may not have additional logic...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.