Dynamically calling field

Discussion in 'ASP .Net' started by Cowboy \(Gregory A. Beamer\), Aug 16, 2008.

  1. I am going on a search tonight for this, but figured I would throw it out
    here as my brain is mush right now.

    Have a nearly full generic implementation of a LINQ to SQL Repository
    (inspired by quite a few examples I have seen out there). Now I am down ot
    the Delete function, which takes a LINQ to SQL object and fires off a
    logical delete if a particular field is there.

    I know there are ways to accomplish this by altering the classes or adding a
    partial class for each class. The issue here is I don't really want to incur
    the time to add partial classes to the LINQ to SQL class and I want to be
    able to regenerate the LINQ to SQL classes if I alter the schema, so neither
    dinking with the generated source (always a mess) or adding partial classes
    are particularly appealing.

    I might also be able to do so through an extension method on the LINQ
    classes. I am not against this method of solving the problem either.

    Most of the fields in the database have an IsDeleted bit field. If this
    field is present in the LINQ to SQL object, I want to set it to 1 (true) and
    save the object rather than actually delete from the database.

    A coworker suggested adding a static method that casts to the proper type:

    switch(entity.GetType())
    {
    }

    Yes, this can be done, but it is a maintenance nightmare.

    Any clever ideas?

    --
    Gregory A. Beamer
    MVP, MCP: +I, SE, SD, DBA

    Subscribe to my blog
    http://feeds.feedburner.com/GregoryBeamer#

    or just read it:
    http://feeds.feedburner.com/GregoryBeamer

    ********************************************
    | Think outside the box! |
    ********************************************
     
    Cowboy \(Gregory A. Beamer\), Aug 16, 2008
    #1
    1. Advertising

  2. Wrong group. Should be in C#. Found a solution regardless:

    public void Delete(T entity)
    {
    Type type = entity.GetType();
    string property = "IsDeleted";

    PropertyInfo propertyInfo = type.GetProperty(property);

    if (propertyInfo == null)
    {
    Destroy(entity);
    }
    else
    {
    using (System.Data.Linq.DataContext context =
    _dataContextFactory.Context)
    {
    Table<T> table = context.GetTable<T>();
    //TODO: Figure out why .Attach() is not working and stop
    grabbing object
    entity = table.First(s => s == entity);

    MethodInfo methodInfo = propertyInfo.GetSetMethod();
    object[] o = { true };
    methodInfo.Invoke(entity, o);

    context.SubmitChanges();
    }
    }
    }

    --
    Gregory A. Beamer
    MVP, MCP: +I, SE, SD, DBA

    Subscribe to my blog
    http://feeds.feedburner.com/GregoryBeamer#

    or just read it:
    http://feeds.feedburner.com/GregoryBeamer

    ********************************************
    | Think outside the box! |
    ********************************************
    "Cowboy (Gregory A. Beamer)" <> wrote in
    message news:e1y0jb1$...
    >I am going on a search tonight for this, but figured I would throw it out
    >here as my brain is mush right now.
    >
    > Have a nearly full generic implementation of a LINQ to SQL Repository
    > (inspired by quite a few examples I have seen out there). Now I am down ot
    > the Delete function, which takes a LINQ to SQL object and fires off a
    > logical delete if a particular field is there.
    >
    > I know there are ways to accomplish this by altering the classes or adding
    > a partial class for each class. The issue here is I don't really want to
    > incur the time to add partial classes to the LINQ to SQL class and I want
    > to be able to regenerate the LINQ to SQL classes if I alter the schema, so
    > neither dinking with the generated source (always a mess) or adding
    > partial classes are particularly appealing.
    >
    > I might also be able to do so through an extension method on the LINQ
    > classes. I am not against this method of solving the problem either.
    >
    > Most of the fields in the database have an IsDeleted bit field. If this
    > field is present in the LINQ to SQL object, I want to set it to 1 (true)
    > and save the object rather than actually delete from the database.
    >
    > A coworker suggested adding a static method that casts to the proper type:
    >
    > switch(entity.GetType())
    > {
    > }
    >
    > Yes, this can be done, but it is a maintenance nightmare.
    >
    > Any clever ideas?
    >
    > --
    > Gregory A. Beamer
    > MVP, MCP: +I, SE, SD, DBA
    >
    > Subscribe to my blog
    > http://feeds.feedburner.com/GregoryBeamer#
    >
    > or just read it:
    > http://feeds.feedburner.com/GregoryBeamer
    >
    > ********************************************
    > | Think outside the box! |
    > ********************************************
     
    Cowboy \(Gregory A. Beamer\), Aug 16, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J Sahoo
    Replies:
    3
    Views:
    5,331
    naikpalak
    Apr 30, 2009
  2. Emmanuel
    Replies:
    1
    Views:
    688
    Karl Seguin
    Nov 22, 2005
  3. Jerry Manner
    Replies:
    5
    Views:
    575
    Jonathan N. Little
    Jun 9, 2005
  4. Sound
    Replies:
    2
    Views:
    477
    Randy Webb
    Sep 28, 2006
  5. jr
    Replies:
    3
    Views:
    472
Loading...

Share This Page