如何组织我的代码(我为网站上的各种用途创建的方法)
我编写了一些代码来将应用程序连接到其数据库,然后创建了一些代码来使用连接代码并检索、更新或添加一些值到数据库中,此外,我可能还有一些代码来处理其他内容,而不是处理数据库
代码有点复杂,也许很简单,但并不短,例如编写一段好的代码来检索单个值,这样我就可以用这个值设置控制器我使用了这个:
SqlConnection sqlconnection= new SqlConnection(ConfigurationManager.ConnectionStrings["DefConnectionString"].ConnectionString);
SqlCommand sqlcommand = new SqlCommand("SELECT name FROM message WHERE id = 3", sqlconnection);
try
{
sqlconnection.Open();
lbl_name.Text = (string)sqlcommand.ExecuteScalar();
Status.Text = "Done";
}
catch (Exception ex)
{
Status.Text = ex.Message;
}
finally
{
sqlconnection.Close();
}
我什至可以添加一些代码为了在数据库中存储有关抛出任何异常的一些信息,我认为代码非常基本,但它并不小,因为它是我需要的最小的部分,编写了一些巨大的代码来添加或编辑新项目,并且还要考虑到这不是我在页面中编写的唯一代码,该页面还有更多需求:D!
我希望我提供了一个详细的案例!...那么您认为使我的页面快速、易于阅读并具有有组织的代码的最佳方法是什么 我已经开始将每个代码放在适当的方法中,但有些方法是常见的,因此我在每个需要它们的页面中再次创建它们,例如 ConnectionToDataBase 方法,同时我认为我只是组织了页面,因此我可以主要查看 Page_Load看看被调用的方法是什么,然后向下滚动以阅读定义,但它仍然是一个大页面并记下最佳实践,我认为
对不起所有大问题,我只是提供详细信息,以便我可以为我的问题获得合理的答案,我希望每个人都能从中受益,因为大多数问题都非常基础,像我这样的新手需要一些详细的案例和答案才能获得更好的开始......感谢您的宝贵时间!
编辑:我知道我是新来的,所以非常欢迎对我的代码提出任何评论!
I wrote some code to connect the application to it's database, then I created some code to use the connection code and retrieve, update or add some values to the database, Also I might have some code to deal with other stuff than to deal with the database
The code is a little complicated, maybe it's simple but it's not short, for example to write a good piece of code to just retrieve a single value so I could set the controller with this value I used this :
SqlConnection sqlconnection= new SqlConnection(ConfigurationManager.ConnectionStrings["DefConnectionString"].ConnectionString);
SqlCommand sqlcommand = new SqlCommand("SELECT name FROM message WHERE id = 3", sqlconnection);
try
{
sqlconnection.Open();
lbl_name.Text = (string)sqlcommand.ExecuteScalar();
Status.Text = "Done";
}
catch (Exception ex)
{
Status.Text = ex.Message;
}
finally
{
sqlconnection.Close();
}
I might even add some code to store some info in the database about any exception is thrown, I think the code is pretty basic, yet it's not small, given that it's the smallest piece I'll need, some huge code is written for adding or editing new items, and also consider that it's not the only code I'll write in the page, the page has more needs :D!
I've provided a detailed case I hope!...So what do you think will be the best way to make my page fast and easy to read and have an organized code
I've started placing every code in an appropriate method, but some methods are common so I create them again in every page that needs them like a ConnectionToDataBase Method, and mean while I think I just organized the page so I could mainly look at Page_Load and see what are the called methods and then scroll down to read the definition but It's still a big page and note the best practice I think
Sorry for all the big question, I just provide details so I could get a reasonable answer for my question, I hope everyone benefits from it as most questions are pretty basic, newbies like me needs some detailed cases and answers to get a better start...Thanks for your time!
EDIT: I know I'm new, So any comments on my code are more than welcome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一些快速观察:
另外(根据下面的评论,这是非常相关的)如果您必须使用内联 SQL,如果您最终将值连接到命令文本中,您可能会遭受 SQL 注入攻击(一般来说,在任何情况下都应该避免内联 SQL)。
不要全局捕获和吞下异常 - 仅捕获实际处理异常的地方 - 否则,将它们冒泡备份。
您需要将数据访问代码抽象为独立于业务对象的数据访问层(我是存储库模式的粉丝),或者(最好)考虑 ORM(我强烈推荐 NHibernate,可能是 Entity) Framework 4 具有更好的 POCO 支持)。
一般来说,我构造此类数据的方式是让业务对象引用存储库对象 - 这被定义为接口,因此我可以通过依赖项注入交换我的测试和实际实现。
然后,数据存储库会执行相关的繁重工作(通常通过 ORM,有时在 ADO.Net 中处理我们的一些遗留代码)——因此,如果我需要客户列表,我只需在存储库上调用“GetCustomers”方法即可。业务层不了解数据访问,它只知道当它调用一个方法时,它会返回一个很好的强类型列表。
[回答请求 RE 组织]
当我在 MVC 网站中使用此模式时,我通常让我的控制器调用业务逻辑类。它的目的是封装从我的数据访问层检索的非数据特定计算等。
业务逻辑类将依次引用一个或多个存储库,这些存储库封装了我的数据和业务类之间的数据传输(例如保存/检索数据等)。
存储库负责提供数据(同样,ADO.Net、NHibernate、EF,任何适合您的东西)。它应该没有业务逻辑,只需足以从我的数据库中获取数据,对其进行适当的调整,然后将基本的非数据特定对象返回到业务层。
我的博客上有一些关于这个组织的示例(可在我的个人资料中找到),虽然它们是 NHibernate 特定的,但展示了存储库模式的基本用法。
希望能有所帮助:)
Some quick observations:
Also (Per the comment below, which is very relevant) if you MUST use inline SQL, in cases where you end up concatonating your values into a command text, you can be leaving yourself open to SQL injection attacks (and in general you should avoid inline SQL in any case).
Do not globally capture and swallow exceptions - only capture exceptions where you are actually handling them - otherwise, bubble them back up.
You will want to either abstract your data access code into a data access layer independent of your business object (I'm a fan of a repository pattern), or (preferrably) consider an ORM (I strongly recommend NHibernate, possibly Entity Framework 4 which has much nicer POCO support).
Generally how I structure this kind of data is have a business object have a reference to a repository object - this is defined as an interface, so I can swap out my test and real implementations via dependency injection.
The data repository then does the relevant heavy lifting (generally via an ORM, sometimes in ADO.Net for some of our legacy code) - so if I needed a list of customers, I would just call a 'GetCustomers' method on the repository. The business layer has no knowledge of data access, it just knows that when it calls a method it gets back a nice strongly typed list.
[Answering the request RE organization]
When I use this pattern for an MVC website, I generally have my controller invoke a business logic class. It's purpose in life is to encapsulate the non-data specific calculations, etc. that it retrieves from my data access layer.
The business logic class will, in turn, reference one or more repositories that encapsulate data transport between my data, and my business classes (things like saving/retrieving data, etc.).
The repository is responsible for serving up the data (again, ADO.Net, NHibernate, EF, whatever works for you). It should have no business logic, just enough to take the data from my database, shape it appropriately, and return basic non-data specific objects back to the business layer.
I have some samples on this organization on my blog (available in my profile) that while they are NHibernate specific, show a pretty basic useage of a repository pattern.
Hope that helps out :)