Conception: automate common Entities tasks and List

Posted on 2006-05-31
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
    LVL 5

    Accepted Solution

    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:



    Author Comment

    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,



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now