473,405 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Dynamically calling field

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! |
********************************************

Aug 16 '08 #1
1 898
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<Ttable = 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)" <No************@comcast.netNoSpamMwrote in
message news:e1**************@TK2MSFTNGP06.phx.gbl...
>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! |
********************************************
Aug 16 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ola Fjelddahl | last post by:
hi. I load a script dynamically and it works * everytime with IE6. * sometimes! with Mozilla1.5 <- makes me curious * never with Opera7.11 all tests on WindowsXP. With "sometimes" I mean
2
by: Sean | last post by:
Hello all, I may just not be searching for the right thing, but I've been looking for a way to dynamically create controls (and name them) in my code so that I can create only the controls I...
0
by: John Crowley | last post by:
I'm having an odd problem with viewstate and a dynamically created control inside a repeater template. Basically, I have a repeater setup like this in the aspx:
8
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
7
by: Samuel | last post by:
Hi, I am building a page that makes use of user control as a templating technique. The following is that I have in mind and it is actually working: Root/ -- login.aspx -- login.aspx.vb --...
4
by: Stone Chen | last post by:
Hello, I have form that uses javascript createElement to add additional input fields to it. However, my validating script will not process new input fields because it can only find the named...
7
by: Steve_Black | last post by:
Hello, I'm toying with the idea of loading a MenuStrip (VB.Net 2005) dynamically based on who is logged into my system. Every user has different security settings and I want to customize the...
3
by: rn5a | last post by:
A SqlDataReader is populated with the records from a SQL Server 2005 DB table. The records retrieved depends upon 2 conditions (the conditions depend on what a user selects in an ASPX page). If...
15
by: =?Utf-8?B?VG9tIENvcmNvcmFu?= | last post by:
I've been led to believe by several articles, particularly Eric Gunnerson's C# Calling Code Dynamically, that calling a method dynamically through Reflection was much slower than through a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.