Conception: automate common Entities tasks and List

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,


Who is Participating?
Sunds like you want to do something like Active Record pattern.  Here is some info about it:
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:


rpelissiAuthor Commented:
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,


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.