Conception: automate common Entities tasks and List

Posted on 2006-05-31
Medium Priority
Last Modified: 2010-04-16
I am writing C# apps for my (small) company.
We are basically doing always the same kind of applications that need to read and write into a MySQL database.
So what we usually need is
1) to edit lists of elements with filtering possibilities.
2) edit, create or duplicate, delete one specific element

So what I want is to build some abstract class or interface that could help me saving time writing always the same stuff each time a concept is added to our model and database.

I was thinking about creating an abstract class "Entitiy" that would implement methods such as "Update(), Delete(), Create(), Duplicate()..." and a seconde one "EntitiesList" that would implement methods such as "GetListWithFilterAndOrder()".
So each time a new concept would appear I would only configure, once for all, the SQL tables, fields... And it would be immediately easily used in some datagrid or editing forms...

See my point?

So what I am asking is a good way to do that. Is it a good idea? I am missing something not using some DataApdater that more or less implements these kinds of methods...?
Well I need to learn from your experience!!

Thank you for answering,


Question by:rpelissi

Accepted Solution

aaronfeng earned 750 total points
ID: 16797921
Sunds like you want to do something like Active Record pattern.  Here is some info about it: http://www.martinfowler.com/eaaCatalog/activeRecord.html
It is a good idea for you to have a base Entity that takes care all the common operations, so you can reduce duplication.  I'm working on an project doing similar thing you are talking about.  Duplication is one of the worst thing you can do in software.

Active Record is great if you are building a small app, or app doesn't have much domain logic.  In an AR pattern domain object represents one row in the table, and each domain object will be able to save, delete etc itself back to the database (inherit from the Entity).  For example:

Customer c = new Customer();
Customer.DeleteAll();  // notice this is static in the Entity because it operates on the whole table.
c.Save();                    // instance method in the Entity because it operates on the object itself.

The downside of AR is the static method can't be mocked out easierly for testing.  Since you are isolating your duplication, you can move into another storage pattern pretty easy.  I think you are going in the right direction.

I have written some stuff on my blog about this: http://aaronfeng.blogspot.com/



Author Comment

ID: 16999680
Sorry, I was kind of busy.

I finally built to class : Entity wich represents one row of the DataBase and EntityCollection that represents a selection from a table.
When I create a new entity, I don't write any SQL, I just have to give columns names, table name and schema name.

As I didn't want to use Refection, I had to find a way to build property dynamically-like. So each class that inherit from Entity implements a "Fields" enum and a "ColumnsName" array where items are in the same order.
So when I need to call a property I use a method "object GetItem(int)".

Class Product : Entity
Product prod
int MyCode = (int) prod.GetItem((int) Product.Fields.Code);

But right now, I am thinking about using a VB.Net macro to write accessors in the Product class for the property to encapsulate the use of GetItem method...

Then I had to solve the problem of SQL JOIN as my Entity can only get datas from one table.
So I introduced a concept called CaptionLink. A CaptionLink contains a column name and a Collection that is ICaptionLinkable (implements the method "string GetCaption(int Id)").
So I made a SuperDataGrid class that contains a CaptionLink array.
Then I overrided the DataSource_Changed() event.
For each CaptionLink I add a new column in the DataTable and fill it with the caption obtained from the column specified in the CaptionLink, usin the ICaptionLinkable method of the CaptionLink collection.


The only thing I am not happy about is that I had to duplicate the Fields enum and ColumnsName array in both Entity and EntityCollection.
in my EntityCollection abstract class, I need to use the Entity "Fields" enum. Unfortunately, it doesn't exist in the Entity but in the son concept Product (that inherit from Entity).
What I need is a sort of "abstract static property" or "overridable static property"... What am I missing there?

Thank you,



Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question