473,739 Members | 4,930 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<stringFor ename
{
get
{
if(m_Forename == null)
m_Forename = new Field<string>(. ...);

return m_Forename;
}
}

public Field<stringLas tname
{
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 LastChangeUserN ame
{
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.Curre ntValue", 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.Cu rrentValue;
}
}
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 2088
"Dirk" <di********@gma il.coma écrit dans le message de news:
11************* ********@s34g20 00...legro ups.com...

| 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<stri ng, Fieldfields = new Dictionary<stri ng,
Field>();

protected bool ContainsField(s tring fieldName)
{
return field.ContainsK ey(fieldName);
}

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

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>(fie ldName);

return GetFieldValue<s tring>(fieldNam e);
}
}

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.p hx.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<stringFor ename
{
get
{
if(m_Forename == null)
m_Forename = new Field<string>(. ...);

return m_Forename;
}
}

public Field<stringLas tname
{
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 LastChangeUserN ame
{
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.Curre ntValue", 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.Cu rrentValue;
}
}
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<stringFor ename
{
get
{
if(m_Forename == null)
m_Forename = new Field<string>(. ...);

return m_Forename;
}
}

public Field<stringLas tname
{
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 LastChangeUserN ame
{
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.Curre ntValue", 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.Cu rrentValue;
}
}
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_Curren tValue
in the # syntax.
Jan 4 '07 #5
"intrader" <in******@aol.c oma écrit dans le message de news:
45************* *********@roadr unner.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<Employeeem ployees = GetEmployeeList (...);

myGrid.DataSour ce = employees;

...
}

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

{
Employee e = new Employee();

textBox1.DataBi ndings.Add("Tex t", 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_Curren tValue", but it did not work. Then I
retried "Forname.Curren tValue" 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**@discussio ns.microsoft.co ma écrit dans le message de news:
E4************* *************** **...icrosof t.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<stri ng, 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<stri ng, Fieldfields = new Dictionary<stri ng,
Field>();

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

foreach (PropertyInfo property in properties)
{
// bind Field<to the type of the property
Type fieldType =
typeof(Field<>) .MakeGenericTyp e(property.Prop ertyType);

// 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.Creat eInstance(field Type,
BindingFlags.In stance | BindingFlags.No nPublic, null, args, null, null);

// add the field to the private list
fields.Add(prop erty.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<s tring>("Forenam e"); }
set { SetFieldValue<s tring>("Forenam e", value); }
}
}

Does that help ?

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 4 '07 #8
Joanna Carter [TeamB] wrote:
"intrader" <in******@aol.c oma écrit dans le message de news:
45************* *********@roadr unner.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<Employeeem ployees = GetEmployeeList (...);

myGrid.DataSour ce = employees;

...
}

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

{
Employee e = new Employee();

textBox1.DataBi ndings.Add("Tex t", e, "Forename") ;

...
}

Joanna
Sorry for the characterizatio n.
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
3033
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 example dsParts.xsd and including that in the data tier. I then will create a class that looks like this Public Class CPart Inherits dsParts
2
1913
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 its value after the textbox lost focus or whenever someone manually saved the record. I came to the conclusion after a long time and after many apps have been developed that there had to be a better way. What I thought would have been great is...
1
1591
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 viewstate and loaded. Then I want to bind my object to a webcontrol: myDatagrid.Datasource = myBusinessObj; myDatagrid.Databind(); This is where my error occurs. Some error regarding IEnumerable and
1
1883
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. I have a parent class, that when inserted, inserts a few child classes into the database, based on other classes. In the old system, this is wrote as one big SQL script, with many inserts and updates because the system was not wrote as OOP.
7
2594
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 I'm having is when I change some other column, control value doesn't update immediately until I call endcurrentedit on bindingmanager which doesnt happen until user click update button.
25
2777
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 this application is heavily data-centric around MSDE database. Would it be better to use custom business objects or extend
2
2641
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 the data tier expects business object from the business tier, I get an error saying: Can not covert businesslayer.emp to businesslayer.emp
8
4133
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. However i have recently started a project which will require synchronization to a remote database. Also the underlying database provider may change at a later date. From what i have read it seems that a layered approach is necessary, or at...
9
2742
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 in. My business classes are, of course, decorated with the: <System.ComponentModel.DataObject() attribute. So, I drop a GridView on a webform and set its datasource to an ObjectDatasource which in turn is using one of my business logic...
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9483
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9341
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9269
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9211
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8216
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6756
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6056
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2195
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.