Good OOP Design Priciples for Handling Database Data [closed] - c#

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I feel like I am asking a pretty basic question, and I think I should be able to find a good answer somewhere on the internet. But I am exhausted from searching and have only turned up a couple of dry bones. To top it all off, I am now frightened that this question is probably too subjective:)
Anyhow, here goes the question. What is considered good practice for accessing, processing, and manipulating data from a relational database in an object oriented program? So far in my programming I have been processing database data in a procedural sort of way. I am currently actively trying to improve my OOP habits and I wasn't sure how to handle this issue.
Here is one of the scenarios I am working with. I have a table with many manufacturing job entries in it. The application I am writing (improving) performs a lot of processing on each job. For example I iterate through each row in the table and do the following:
Post the due date to Google calendar
create a folder unique to the job in a given directory
create a work order for the job
create a contract brief for the job
manages traveler documents
email info to a couple people
etc, the list goes on
You get the point. A lot of processing gets done on each job. Currently I have what most good programmers would call a fantastic crap pile of spaghetti code. Maybe it isn't quite that bad, but almost. I iterate through each row in the table with a for each loop and sequentially perform each action on each row. I don't like the way things are currently designed, but I don't know what to do better.
I would like if I could have a neat object called "Jobs" which would implement all the properties of a job and provide a method for performing each of the actions. Then I could even make a custom collection to handle my jobs and all the world would be brighter. The whole solution would be more readable, easier to maintain, easier to add actions to perform on each job (which happens frequently), etc.
But my problem is I can't figure out how to make the connection between the fancy objects and the database. Should I use some sort of Object-relational mapping (I read all kinds of mixed opinions about this)? Do I just loop through all the rows and convert them to objects which accumulate in a collection? Or is the best option to keep going about such projects in a procedural sort of way? I would love your answers and input.
I am interested in information on this subject in an abstract way. In other words I wonder how to handle a situation like this in general. Not just specific to the example I gave. But of course specifics are great as well. I do most of my programming in Visual Basic and C# with VS 2010.

Look at the Repository pattern. It's a great way to separate data access from business processing in meaningful ways. Applying this pattern I have a few projects:
Entities - the objects that store stuff
DataAccess - the ORM DbContext and ADO.NET wrappers
Repository - wraps queries to present strongly-typed functions to the rest of the app
TheRest - the other projects / tiers: Business, GUI, etc
See How to use Entity Framework context with dependency injection? for a good description of each of the project types

But my problem is I can't figure out how to make the connection between the fancy objects and the database. Should I use some sort of Object-relational mapping (I read all kinds of mixed opinions about this)?
Yes.
As for the processing. Use the visitor pattern and let all different code which want to process the data register themselves as visitor. You could also use an inversion of control container to manage that (IHandlerOf<Job> or IHandlerOf<TravelerDocument>).
It gives you are more loosely coupled way of processing the data.

Related

What is best approach / methodology to manage hierarchical object state in MVVM based app? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I work on GUI and deal with complex objects that is objects have other objects and those object might have other objects.
These objects are manipulated (often almost at the same time) by various views, view models, services, nhibernate, and you name it - doing in simple things like, save/update/delete/validate and etc.
In most cases things like NotifyPropertyChanged is enough, but not always. Sometimes i resolve to calling EventAggregator BUT when object graph is big and especially collection of such objects - it gets so messy i keep loosing ends and not sure what is exact state of a particular object and often not sure if that is still same object or some loose copy of it;
So my question is what is best approach / methodology to manage object states and avoid "state explosion"?
Simplify by introducing additional structure. The best methodology is giving the application more structure. Divide the application into smaller parts internal living of which you can oversee and control well. Let these parts be hidden behind facade/adapter types which expose only a simplified view on that part of the system. Repeat recursively as long as necessary. Use formal models to define structure and inter-communication rules, e.g. UML 2.0 hierarchical state machines usually fit well... I know, this is just a generic engineering bla-bla, but it could help...
I advise you to take note of a book Agile Principles, Patterns, and Practices in C# by Robert C. Martin and Micah Martin, there are many good examples where it is shown how to design a system by UML, and other similar methods. Specifically, it is shown why you should refactor your code, that can be stored in abstract classes and what is not. You can immediately see an example with a coffee maker Mark IV, where he developed a truly independent interface.
According to my feelings, the main principle of MVVM - is the independence of the data from its representations. I like trying to make separate modules, which implement separate logic, not knowing about the data. For example, SQL module, SMTP module, etc, which simply contain some methods like ConnectToDb() or SendEmail(), the main logic is in ViewModel, she combines these Work-modules with the data model.
Useful to draw a diagram before designing the system, but do not get involved too. The main thing is to see the main parts in the paper, and that the others would understand it as well as you know (or architect).

Dilemma between keeping my code with pure js + jQuery AJAX calls or getting rid of every client script and rewriting the code in .NET only [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I've developed a website (jQuery + .NET/C#) and it's 90% ready. Before a publish it I decided to face the truth and I admitted to myself the code isn't totally hack-safe. To be honest it's barely hack-safe. It's because I've been practicing some bad web programming habits (like relying too much on client scripting instead shelling it into server-side).
To be more precise, I've being using too much jQuery I guess and now I see all my webmethods are exposed in .js files. I've minified and obfuscated but it doesn't prevent a determined cheater to get all my stuff. That's one point.
Other issue is the fact I have some webmethods that return a lot of data from my database. Not confidential data, but some nice data my system generates and I don't wanna any external website to call it and display it like it is their material.
One of solutions I thought about was to generate all html in server-side and return it to .js and then append it to his respective element (in this way I'd hide all the intelligence beyond it) but it doesn't solve all the problems I related above.
So my question is... do you guys think it's safer to rewrite everything using ASP.NET controls or is there any way to make it REAL SAFE using the code I have (like avoiding external callings to my webmethods)?
I know without looking to the code it's hard to make an opinion but it's huge! And it's all about what I said (too much client-scripting, little server-side).
Thanks in advance.
First of all, you can check on serverside, which domain is calling your backend -> deny for unallowed domains. (Check this ticket)
To reduce javascript magic, you can move the logic into backend as you said.
Maybe some tricks to save your javascript code ( Check this out )
If you want a totally hack-safe Site, then you will be disappointed. you can just make it the hardest possible.
This is easiest with sites processed server-sided, and then served to the client. but i would also recommend not moving all to the server, because after all the server will have to process each and every request you make to it.
I have experienced, that it is the "easiest" to make all data-serving and evaluation logic serversided and rendering, as well as easy processing logic client-sided. Thus combining the strong sides of both technologies at minimal security risk. e.g. after an AJAX-Call to serve some data (if you even want to do that) you can call render, update and processing logic client sided with the help of success: or failure:, as well as after: or similar keywords.
Try some new things out. if you move it all to one technology/process you will have to take the disadvantages of this process. split things up!

Designing a file center in asp.NET [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
My university project is to design a file center ( it should be similar to 4shared.com) with asp.net(c#) and SQL server. Before I start to design I have some question, please help me.
Is it better to use Dream weaver or visual studio ? I am not familiar with Dream weaver but our teacher said it helps us. Whats your idea ? Learn Dream weaver or visual studio is enough ?
My site, according to users behaviour. should suggest to them , for example someone is trying to download xx.mp3, my site should suggest that other that downloaded xx.mp3 also downloaded yy.mp3 ff.mp3 and so on. Now I don't know how should I save user behaviour. Should I keep this information in my database tables or elsewhere? Please tell me how to implement this.
Another question is about my tables. what are your ideas about my site tables? What important column should they have? And of course how should I save files? Is it better to save them in database or only save their path on tables and save them in site folder?
Last question - I am not professional in asp.net and c#. Will it be possible for me to design this website or should I tell my teacher that I cant do this ?
Please, if you have any tutorial about designing a file center or about my question put it here.
I'll follow Ruben's formula: Your question is subjective to personal opinions. Here is mine:
Go on with Visual Studio, it can edit web pages very well, so you don't really need Dream Weaver, also it will help you in writing code and testing your application. Dream weaver may help do somethings like creating menus and stuff... I say you should learn to those things without dream weaver, still it is up to you.
User behaviour is not vital. Of course you can always choose to track user's activity for the convenience of suggestions, yet this feature will take a while to implement, and if we count that a good chunk of user will prefer to turn that off I say you should not put priority on this part of the project (unless, your project is exactly about that).
If in doubt, always have an Id. The columns Rubens suggests are good for auditory so consider that too. I want to tell you that security is paramount. All the cycle from login to logout, change password and such seems simple, but requires special attention. I will self-promote here a bit, start by My answer to Recover / Reset lost password options via email.
You can do it, really. The question is how long will it take (will it be done in time?), I mean... there is some learning involved as in anything. There two things you must know:
It is absurd to pretend you know the future. In software development, if you have done something in the past, chances are you can just reuse the code... if you definitively can't reuse your old code, it is because: a) you haven't done it or b) you haven't done it well enough. So, every time you are in front to a project there will be things that you haven't done, those will be the parts that take more time (because for the other, you just reuse) and it is ridiculously hard to tell how long will they take. So, No. I can't tell you if you are gonna do it in time, and also it is that way all the time and we get things done regardless.
You can always be mediocre... Bad advice? yes, a bit. But in truth, if you have to present something, anything, for a deadline... and you don't know what to do, you can always go with what you already know. This way you will pass easier, and also, you will get used to not push yourself (ergo, be mediocre). So, decide, what matters to you: Getting good grades*, or learning new things? [Yes, you can do both].
*: That is only a dichotomy if we assume bad teachers, good teachers will not give good grade to a project well done that tries nothing new, instead will incentive a project that is innovative but barely works (or that's the theory).
Remember to take a look at the antecedents, yes 4Shared, but also Google Drive, Microsoft SkyDrive, Dropbox, Mega...
Is your site, one where people do backup of personal media, or is it one where people share files with others? Will everybody be able to access uploaded files? Does it make sense to avoid duplicate files? Does it make sense to cipher the files (With a personal key?)? Do not answer me, just consider them.
Lasly, Tutorials? No, sorry. Still, you can researchs the parts, such as: access control, file upload, redirections, session management and so forth. Do don't really need a tutorial, take that idea out of your system, it is stopping you from being the one who does things for which there is no tutorial (and maybe write one?).
Anyway, I want to suggest to you to have a read of Post/Redirect/Get at Wikipedia And also in this very website. And I also want to suggest you to not hesitate to ask questions (ah, yes, search first, stand in the shoulders of giants).
Your question is subjective to personal opinions. Here is mine:
I would recommend Visual Studio for working with C# and ASP.NET.
Yes, you should keep the user behavior in database. You can track the user behavior with the user session, and saving each download related to the user session.
Any database tables should have an Id, CreationDate, CreatedBy, LastModificationDate and LastModifiedBy.
You should try and retry before telling yourself you can do something. If you are in university, you should learn to overcome obstacle and challenges.

In a strongly-typed world, why isn't ASP.NET MVC's fragile dependency on naming conventions frowned upon? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
Since forever, strongly-typed objects have been a foundation of object-oriented programming. Fast-forward to 5 minutes ago, when working with the Entity Framework and MVC3, I was forced to add this to my Web.config:
<connectionStrings>
<add name="_MY_EXACT_CLASS_NAME_DbContext" connectionString="Data Source=blahblah.../>
</connectionStrings>
Great, My entire application depends on an arbitrarily-chosen name in an XML attribute. Is this really what modern programming looks like? Misspelling a class name is a serious offense, one the compiler leads us directly into fixing, but in this case, I'll just get a runtime exception message. If Mr. aforementioned exception message in a good mood, he'll point me toward Mordor, and I'll trudge off toward another Mount Doom of wasted debugging hours to destroy the invisible One-Typo-To-Rule-Them-All.
The same goes for Controllers:
routes.MapRoute("BE_CAREFUL","{controller}/{action}/{id}",
new { controller = "ONE_FALSE_MOVE_AND",
action = "BUT_I_SWEAR_IT_SAID_BUILD_SUCCEEDED" }
);
It seems like things come and go in waves. Strongly-typed objects had their day in the sun, and now we're all girl-next-door over the anonymous "var". I'll admit, being coy about your type stirs up a lot of sexy scenarios - especially knowing you don't have to do any setup work - but here's The Actual Question:
How do the forefathers of object-oriented programming feel about our "advancement" of their art by adding a bunch of wishy-washy, do-sorta-whatever anonymous constructs while at the same time creating fragile dependencies on naming conventions?
For all we know, MVC4 might suddenly require that all names be preceded by exactly 4.7 spaces followed by lolcat ASCII art. Why? Because yes, that's why. Take a moment and marvel at the fact that you just witnessed the birth of a naming convention. Obviously, this is seriously solid foundational material for a flagship framework.
So, if there's one thing I want my entire codebase to both functionally and philosophically depend on, there's nothing more mission-critical to the mathematic logic of programming than..... Microsoft's® English-Language Naming Conventions!
</sarcasm>
</griping>
<!-- resume enjoying all of MVC's amazing features, after eating any humble pie served up in the comments -->
My entire application depends on an arbitrarily-chosen name in an XML attribute.
This is called "coding by convention" or "convention over configuration" ... you pick a few things that need configuration, and then everything else just "falls into place". Like using razor and having _layout.cshtml in /views/shared. Or like using razor and having mySpecialController with ActionResult Index and /views/mySpecial/Index.cshtml ... those are just a way of letting the convention work for you.
Since forever, strongly-typed objects have been a foundation of object-oriented programming.
Strongly-typed objects had their day in the sun, and now we're all girl-next-door over the anonymous "var".
var variables are just a shorthand to make things slightly more readable, the compiler still strongly and statically types things at compile time. Consider the difference here:
foreach (var c in Customers) { /* do stuff */ }
foreach (CustomerDataItem customerDataItem in Customers) { /* do stuff */ }
As you can see, the first one says "get a item c from Customers" and the second one says the same thing but good lord man I've already written two more lines of code while you're still typing the long one. Granted, with ReSharper that advantage disappears, however ...
For all we know, MVC4 might suddenly require that all names be preceded by exactly 4.7 spaces followed by lolcat ASCII art. Why? Because yes, that's why.
har.
How do the forefathers of object-oriented programming feel about our "advancement" of their art by adding a bunch of wishy-washy, do-sorta-whatever anonymous constructs while at the same time creating fragile dependencies on naming conventions?
Ok, so this was mostly just frustration, but I'll bite. The guys that started wanted simpler code (BASIC, COBOL look up what those mean) and so they want things to be easier and more math-y. That's where things are moving (LINQ is set math, and higher order calculus; also see F# and Python)
So they would LOVE what we're doing now. Getting away from procedural code (algebra) and moving into set-oriented logic (adv calculus). Also see Event Handlers ;-)
So .. having said all that: I've been in your shoes. I've asked those questions. I've studied at the feet of masters. I love where the languages are going.
In your next life, I want you to learn node.js. I want you to learn async evented processing, and I want you to understand how things don't rely on ANSI-C anymore. We've made a lot of progress in this industry, and things are looking up. Things are looking up every day. I love where we are, and I think it's the right thing for our industry.
Cheers, and HTH.
My answer: "because of the advancements in automated testing".

Code Samples for interview [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
I recently applied for a developer position and the director there asked me to send some samples of code.
How should I approach this? Once, I sent a sample of code that I wrote for myself to a company and they didn't get back. This time, I want to be prepared and send appropriate samples. I want to know what I should send them, create a sample website/code it and send a link along with code files, or create a sample project, or some other approach.
I cannot send code that I have written for my previous and current employer, so I'm not sure what to send.
They asked for C#, HTML, CSS, JS, SProcs, triggers samples, so I thought: would it be nice if I create a project that includes all the above and send it to them, or should I send individual samples?
You've pretty much answered your own question. Why not create a project including all of the above? Small games are fun to write and you can demonstrate a lot of knowledge by creating one. Websites are good too, if you want to demonstrate things like good UI design and dynamic HTML and scripting knowledge.
It's essentially your portfolio to demonstrate what you're capable of, so apply all your knowledge. This includes not just what the program does but also how it does it -- good choice of algorithms, code architecture, proper naming of variables and appropriate comments, etc.
You can write code that's of just as high quality as what you'd create for an employer -- higher, in fact, since you have complete control over the methodology and standards used. Install one of the free revision control systems (git, mercurial, subversion etc), use bugzilla to track your bugs and todo list, and write a full suite of unit tests. Produce professional documentation and demo screenshots.
Yeah, that's a lot of work. But I can guarantee you that if such a project crossed my hiring desk, I'd have you in for an interview in a hot minute.
I've been in this situation before, and I usually just refuse to send them the code samples.
The reason is this: when I work for an employer, I'm working professionally, and the employer has exclusive rights to all the code. If I tried to copy the code and send it to another potential employer, I'm at risk of major intellectual property theft. On the other hand, if I create some code for my own use, it is by definition amateur code (even if produced by a professional), and doesn't have the same level of project management stricture, test stricture, etc., and is therefore not representative at all of my professional coding skills.
Companies that ask for this are effectively asking for the impossible. I find it best to point this out to them.
I'd take something I already worked on - preferably three-tier including sprocs, etc., and factor out anything of a sensitive nature. If it's something you wrote yourself then you will be comfortable discussing it and able to show the breadth of your knowledge - that's what will land you the job. A complete project shows that you know how everything fits together. One more thing: if they ask you where you go to find good code samples, do not immediately say Google: tell them you would look in their code base ;-)
Besides what you send them (a project, a website), I suggest you also give some thought as to what you want to convey with it, and give a written introduction to the sample: why you think it's an interesting piece of code? What does it showcase? What pieces are you proud of? What were the challenges?
They should be able to figure that out themselves, but there is no harm in helping people see what you want them to see...
WHen I intervoiew people I often ask them to give me some code. Usually I asked them to code infront of me though but I guess my point would be the same if the send it to me. I don't look at what they've done (the functionality) but how they've done it. What design principles guide their work. How tidy is the code. What's their test strategy. Have they used any patterns and if so are they used in a well thought way. Depending on the language I'd look for specific aspects of the language. In c++ I'd love to see them be capable of using template specialization elegantly (E.g. for traits/policies) in c#3.0 I'd look for propper use of lambda expressions, generics, LINQ I'd be looking for both correct use as well as over use.
Hope it gave you an idea of what kind of scrutiny you might be put through

Categories

Resources