Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Dynamically calling field

Reply
Thread Tools

Dynamically calling field

 
 
Cowboy \(Gregory A. Beamer\)
Guest
Posts: n/a
 
      08-16-2008
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! |
********************************************

 
Reply With Quote
 
 
 
 
Cowboy \(Gregory A. Beamer\)
Guest
Posts: n/a
 
      08-16-2008
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)" <(E-Mail Removed)> wrote in
message news:e1y0jb1$(E-Mail Removed)...
>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! |
> ********************************************


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript validation for a not required field, field is onlyrequired if another field has a value jr Javascript 3 07-08-2010 10:33 AM
how to show an Image field from a SQL Server table field to a aspx page J Sahoo ASP .Net 3 04-30-2009 08:30 PM
1.Enter space bar for field names and save the field.The field shoud not get saved and an alert should be there as"Space bars are not allowed" Sound Javascript 2 09-28-2006 02:43 PM
Validating a field based on value of another field Emmanuel ASP .Net 1 11-22-2005 12:35 PM
Placing the selection of a list field in a text field Jerry Manner HTML 5 06-09-2005 01:52 PM



Advertisments