|
------------------------------
Author of Primary ROleplaying SysTem
How do I take my coffee? Black as midnight on a moonless night.
War doesn't determine who's right. War determines who's left.
|
|
|
|
|
I am/we are moving from our 6 years old home grown entity framework towards .NET 4.0 Entity Framework and LINQ to Entities - I like it!
At a first glance it looked like I had to manually code LINQ queries into every Business-Object, even for standard operations (CRUD).
Let me show you a tiny generic BusinessObject, that fits perfectly into ObjectDataSource:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Data.Objects.DataClasses;
using System.Data.Objects;
using System.Data;
using System.Reflection;
namespace DataObjectDemo {
[DataObject]
public abstract class BasicWebBO<TEntity, TKey, TContext>
where TEntity : EntityObject,new()
where TContext : ObjectContext, new() {
private TContext _context;
protected TContext Context {
get {if (_context == null) {
_context = new TContext();
}
return _context;
}
}
protected virtual string EntitySetName {
get {
return typeof(TEntity).Name;
}
}
[DataObjectMethod(DataObjectMethodType.Insert)]
public virtual void Insert(TEntity entity) {
Context.AddObject(typeof(TEntity).Name,entity);
Context.SaveChanges();
}
[DataObjectMethod(DataObjectMethodType.Update)]
public virtual void Update(TEntity entity) {
Context.AttachTo(EntitySetName, entity);
Context.ObjectStateManager.
ChangeObjectState(
entity, System.Data.EntityState.Modified);
Context.ApplyCurrentValues(
entity.EntityKey.EntitySetName, entity);
Context.SaveChanges();
}
[DataObjectMethod(DataObjectMethodType.Delete)]
public virtual void Delete(TEntity entity) {
Context.AttachTo(EntitySetName, entity);
Context.DeleteObject(entity);
Context.SaveChanges();
}
[DataObjectMethod(DataObjectMethodType.Select)]
public virtual TEntity Select(TKey key) {
TEntity template = new TEntity();
PropertyInfo pi = typeof(TEntity).GetProperties()[0];
pi.SetValue(template, key, null);
EntityKey entityKey =
Context.CreateEntityKey(EntitySetName, template);
object entity;
if (Context.TryGetObjectByKey(entityKey, out entity)) {
return entity as TEntity;
}
return null;
}
Now you can easily derive a BusinessObject, eg. PersonBO:
public class PersonBO : BasicWebBO<Person, int, DemoEntities> {
}
PersonBO is now ready to use and provides a standard type-save implementation for ObjectDataSource-CRUD-Methods along with designer support
You can also extend PersonBO with any additional business logic and Custom-CRUD-Methods
[DataObjectMethod(DataObjectMethodType.Select)]
public IEnumerable<Person> SelectAll() {
return from person in Context.Person select person;
}
[DataObjectMethod(DataObjectMethodType.Update)]
public void UpdateImage(int PNR, byte[] image) {
}
You may have noticed the hack around the primary key value. It would also make sense, to declare this method abstract.
wish you well
modified 1-Jul-12 20:14pm.
|
|
|
|
|
Hi,
Woppelmann P wrote: Now you can easily derive a BusinessObject
This idea of deriving business object is fantastic.
You should use this idea as a new article. The user's will come to know about this. This is not the right place of this kind of ideas. You can refer Here[^] for articles and tips.
Warm Regards.
--Amit
|
|
|
|
|
Try
Dim objReader As New System.IO.StreamReader(Application.StartupPath & "\" & TextBox3.Text & "_OAuthtk.dat")
My.Settings.m_strToken = objReader.ReadLine()
My.Settings.Save()
objReader.Close()
Dim objReader1 As New System.IO.StreamReader(Application.StartupPath & "\" & TextBox3.Text & "_OAuthtkST.dat")
My.Settings.m_strTokenSecret = objReader1.ReadLine()
My.Settings.Save()
objReader1.Close()
Me.Hide()
Form1.Show()
Catch ex As Exception
Me.Hide()
Form3.Show()
End Try
I used that code in my application called TRocket to check if the files are present or not :P *The lazy way to check/read.
|
|
|
|
|
You call it lazy I call it simple.
You could check for the file existence
if (File.Exist(...)) {...}
But what if the file exist but is not readable?
I think your code is fine.
I also often hear you should not catch all.
In some other languages, the compiler know what exception is raised by a block of code. Catching the required exception in that scenario is easy.
In C#, it is harder. If you catch the exceptions you can guess, you might miss the rare exceptions like disk disconnected, out of memory or god knows.
I am not a programmer that put program elegance or programmer state of mind before the end-user satisfaction.
I think catching all is safer.
In one word, I think your code if FINE.
modified 8-Jun-12 18:19pm.
|
|
|
|
|
+5
Bastard Programmer from Hell
|
|
|
|
|
On the other hand, checking whether or not a file exists and is readable before you need to access it may not be worth the trouble because it may not exist or be readable later when you do need to access it. Generally, I prefer to just access it when I need to and deal problems then.
|
|
|
|
|
Perhaps I was not clear.
You summarized my exact thoughts
|
|
|
|
|
I have this project which is so nasty I don't even want to touch it but as a jock on my professional life I inherited it from a master developers. Application is so pathatically written it makes my mind go crazy whenever there is a fix required. So here is one example of hundreds of stord proc it uses or not uses ( some stord proc are just there for fun ).
CREATE PROCEDURE [dbo].[uspInsertUpdateDeleteFltcode]
@fltCode varchar(8),@fltDesc Varchar(20),@fltGrp varchar(8),@casterRelated bit,
@fltType varchar(2),@flag char(1),@shortDesc Varchar(8),@rptOrd Numeric(9,0)
AS
declare @Operation char(1)
BEGIN
SET NOCOUNT ON;
SET @operation=@flag
IF @operation='I'
BEGIN
INSERT INTO FltCode (Flt_Code,Flt_Desc,Flt_Grp,Caster_Related,Flt_Type,Sh_Desc,Rpt_Ord)
VALUES(@fltCode,@fltDesc,@fltGrp,@casterRelated,@fltType,@shortDesc,@rptOrd)
END
IF @operation='U'
BEGIN
....
...
..
Some of them call some other stord procs internally. Some are at least 500 lines long and loop within loop within loop and so on but this is insane why in the world you have one strod proc that do every CURD operation ? Why ..
Zen and the art of software maintenance : rm -rf *
Math is like love : a simple idea but it can get complicated.
|
|
|
|
|
virang_21 wrote: jock on my professional
So you think you're some jock[^]. I believe it's more like a joke.
virang_21 wrote: pathatically
Your spelling is quite pathetic.
virang_21 wrote: other stord procs internally
So how does a town in Norway[^] figure into all this?
virang_21 wrote: have one strod
An what's this bit with that Russian civil war hero[^]?
virang_21 wrote: every CURD[^] operation
Hmmm,makes me think of cheese. I wonder why?
I'm trying to figure out real hard what really belongs into the hall of shame here.
Cheers!
"With sufficient thrust, pigs fly just fine."
Ross Callon, The Twelve Networking Truths, RFC1925
|
|
|
|
|
yea mate ... after long debug spelling doesn't matter much.. and English ( Engrish ) is not my first language.. I still swear in my mother tongue when I am frustrated...
Zen and the art of software maintenance : rm -rf *
Math is like love : a simple idea but it can get complicated.
|
|
|
|
|
This post made me laugh.
It's not his native language neither mine for that matter.
No shame there, the guy is frustrated.
|
|
|
|
|
virang_21 wrote: Some are at least 500 lines long and loop within loop within loop and so on but this is insane why in the world you have one strod proc that do every CURD operation ? Why .
For fun, I do not see any other 'advantages'.
Nice prefixes - must have been written by a VB6-programmer
Bastard Programmer from Hell
|
|
|
|
|
Eddy Vluggen wrote: must have been written by a VB6-programmer
This joke is less and less funny when you know, work with, and refactor code by vb6 programmers
If it moves, compile it
|
|
|
|
|
loctrice wrote: This joke is less and less funny when you know, work with, and refactor code by vb6 programmers
It's not a joke, it's sarcasm. Those prefixes are heavily in use by both Access (VBA) and VB6 developers. You won't see these kind of examples in a book on Sql. Most "experienced" programmers that I worked with are multi-lingual and rarely label themselves using a certain language.
..and yes, I too, worked a lot with VB6. The Professional edition!
Bastard Programmer from Hell
|
|
|
|
|
|
Pascal Ganaye wrote: Type prefixing is far more common in C++ than VB.
Granted; and Apps Hungarian is still in use, with a good reason. Systems Hungarian has been abandoned, also with a good reason. It's not a problem to have used it, but it IS a problem if you still see a modern-day use. Especially since the argument is over ten years old.
..and ffs, why in Sql? Is it really that complex code? No, it simply someone religiously following his "coding standard".
Bastard Programmer from Hell
|
|
|
|
|
Sometimes because "Our Dear Leader" dictated that all stored procs shall be prefixed with
sp_ at least I could convince him that we should use
usp_ , with difficulty... Sys Hungarian is also common throughout the system, such that
Dim iVariable As String also happens to be a common sight
|
|
|
|
|
Whehe, aight
The last example shows how it does more damage than good. I've seen these quite often, and it costs more time than that it actually helps.
Was I right in my guess? And if I also guess he's Dutch?
Bastard Programmer from Hell
|
|
|
|
|
Again I am playing devil's advocate here.
I am not suggesting that you should write this code.
However
Sometime the DBA through SQL user access rights settings won't let program access the database through SELECT/INSERT/UPDATE statements but allow execution of stored procedures.
In those scenario you must use STORED procedures to do anything.
|
|
|
|
|
I don't mind use of stored proc.. It is terrible when INSERT/UPDATE/DELETE done in the same stored proc and code is calling the same stored proc with only way to find what operation it is doing is via flag is a big fat FAIL !!!
Zen and the art of software maintenance : rm -rf *
Math is like love : a simple idea but it can get complicated.
|
|
|
|
|
Just came across this code from an ex-coworker (names changed to protect the innocent):
Select Case DoStuff(SomeVariable)
Case 0
Case 1, 2
Case Else
result = "omgnub"
End Select
When I saw that, I . Well played, n00b, well played.
|
|
|
|
|
Nice, that's epic!
""Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
That reminded me of a custom exception one of my friends made: NoobDetectedException . He said he would take it out when he turned in the project, but he didn't. The professor hit the exception, and was not very pleased.
The professor showed the class the exception, which had the message: "You are a noob. You will be assimilated. There will be no escape. (This also applies to <professor name omitted>")
public class SysAdmin : Employee
{
public override void DoWork(IWorkItem workItem)
{
if (workItem.User.Type == UserType.NoLearn){
throw new NoIWillNotFixYourComputerException(new Luser(workItem.User));
}else{
base.DoWork(workItem);
}
}
}
|
|
|
|
|
good practices to use a memcached client to make your site faster!
public IEnumerable<Product> GetProductList()
{
List<Product> data = null;
var key = string.Format("{0}", "Some_Beautiful_Key");
if (_cache != null)
if (_cache.Contains(key))
data = _cache.Get< List <Product> >(key);
data = TheDao.GetProductTable();
if (_cache != null)
_cache.SaveOrUpdate(key, data);
return data;
}
Rudy Alvarez // rdebug.com
|
|
|
|