http://www.ilkon.com

I am thinking about using linQ to SQl generated objects as my data objects and putting the business rules on top of them using extension methods.

such as

Simplier Example:

(PERSON > a linQ to designer generated Table<Person> class

PERSON prs = new PERSON();

prs._NAME = "Erdem";

prs.LASTNAME =" ahin";

string fullName = prs.getFullName();

string initials = prs.getInitials();

prs.emailSend();

prs.setDefaultValues();

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

public

static class ExtensionsPerson

{

public static string getFullName( this PERSON value)

{

string fullname = value._NAME + " " + value.LASTNAME;

return fullname;

}

public static string getInitials( this PERSON value)

{

string fullname = value._NAME.First() + "." + value.LASTNAME.First();

return fullname;

}

public static void setDefaultValues( this PERSON value)

{

value._SECURITYCODE = 0;

value._LASTMODIFYDATE =

DateTime.Now;

}

public static void emailSend( this PERSON value)

{

//send email to this person in a special format decided by the business

}

}

********************************************************************
I see no down sides to this kind of approach which really worries me and I wanna be sure that this kind of applicaiton design will not end up like a "BLACKBOX" !!! which will bring me a maintaince and deployment nightmare in the future.



Re: LINQ Project General Integrated use of c#3.5 and business application development

TurkoAmericano

The downside is probably the maintenance issue. What is the upside Why is this preferable to using a separate BO layer




Re: LINQ Project General Integrated use of c#3.5 and business application development

Miha Markic

Why do you want to code entity methods as extension methods - this could be classified as abusive use of extension methods :-)

Use partial classes instead and add "normal" methods to each entity. Or even better in your case, use properties.






Re: LINQ Project General Integrated use of c#3.5 and business application development

jwooley

I would agree with Miha on this one. Partial classes are the way to go for that kind of business logic. Extension methods are too easy to abuse and really aren't meant to be used on objects you have control over. Consider what would happen if you used the extension method in a ExtensionsPerson class. Someone else could come along and add a GenericExtension class. If the extension methods had the same signature and were both in scope, you would run into an ambiguity issue.

Worse yet, if you create your custom business methods in the separate ExtensionsPerson and later added a native method in the Person class with the same signture, the native method would be used in place of the extension method. (Which may not be what you would want).

Furthermore, binding (and serialization) only works against the properties, not the methods, so you would not be able to bind to the results of your extensions.

Here is an implementation using a partial class implementation:

Code Snippet

public partial class Person

{

public string FullName

get {

return NAME + " " + LASTNAME;

}

public string Initials

get {

return NAME.First() + "." + LASTNAME.First();

}

public void SetDefaultValues()

{

_SECURITYCODE = 0;

_LASTMODIFYDATE = DateTime.Now;

}

public void EmailSend()

{

//send email to this person in a special format decided by the business

}

}

Jim Wooley

http://linqinaction.net

http://devauthority.com/blogs/jwooley






Re: LINQ Project General Integrated use of c#3.5 and business application development

Anders Borum

Even better - write corresponding business objects and use a private entity storage as datasource for your properties.

Code Snippet

public class BusinessObject

{

private MyEntity _entity ..

public string MyProperty

{

get { return this._entity.MyProperty; }

}

}

etc.






Re: LINQ Project General Integrated use of c#3.5 and business application development

www.ilkon.com

thanks guys.I think will stick to the partial class idea as I do not wanna create my own blackbox with the custom entities.

Partial classes seem much more understandable than extension methods.

so for the extension methods, I would probably use them less in business entities...