ASP.NET C#:我应该使用哪种设计模式?为什么?

时间:2022-09-10 23:14:44

I am developing an app in ASP.NET C# and came across the following scenario:

我正在使用ASP.NET C#开发一个应用程序,并遇到以下情况:

  1. I will have to create some maintenance screens for different entities (tables)
  2. 我将不得不为不同的实体(表格)创建一些维护屏幕

  3. Those entities will basically have the same behaviour within the UI: Search, GetById, Save, Create and GetAll
  4. 这些实体在UI中基本上具有相同的行为:Search,GetById,Save,Create和GetAll

  5. The entities may have different structure i.e. different properties (fields)
  6. 实体可能具有不同的结构,即不同的属性(字段)

As I am talking about 20 plus admin screens, which design pattern I could take advantage of in order to minimize the amount of code I will have to write?

当我在谈论20多个管理屏幕时,我可以利用哪种设计模式来最小化我必须编写的代码量?

I though of the bridge pattern but I am a little confused on how to implement it ...

我虽然有桥模式,但我对如何实现它有点困惑...

A little bit of the technology background I am using:

我正在使用的一点技术背景:

  1. ASP.NET classic (n-tier)
  2. ASP.NET经典(n层)

  3. LINQ to SQL and DAO objects
  4. LINQ to SQL和DAO对象

  5. SQL Server 2005
  6. SQL Server 2005

4 个解决方案

#1


For a set of admin screens that are just doing CRUD (Create, Read, Update, Delete) operations and with little in the way of business logic, I'd be quite tempted to more or less eschew design patterns and take a look at asp.net dynamic data. This is especially true if you want to minimise the amount of code you want to write.

对于一组只进行CRUD(创建,读取,更新,删除)操作并且几乎没有业务逻辑的管理屏幕,我很想要或多或少地避开设计模式并看看asp .net动态数据。如果要最小化要编写的代码量,尤其如此。

#2


This is not a design pattern... but I would strongly suggest using Dynamic Data. Jonathan Carter has some great articles about it: http://lostintangent.com/index.php?s=dynamic+data

这不是一种设计模式......但我强烈建议使用动态数据。 Jonathan Carter有一些很棒的文章:http://lostintangent.com/index.php?s = dynamic + data

#3


If you're really just doing some basic stuff like this: Search, GetById, Save, Create and GetAll, I would recommend you use repositories. If done wrong repositories can get really bad and nasty, but if you're really primarily limited to this set of operations you've basically described a repository with that set of operations.

如果你真的只是做一些基本的东西:搜索,GetById,保存,创建和GetAll,我建议你使用存储库。如果做错了存储库可能会变得非常糟糕和令人讨厌,但如果你真的主要限于这组操作,你基本上已经描述了具有这组操作的存储库。

You'll want to look at ways in which you can extract the extra logic for example of searching so that you're not creating duplicate logic.

您将需要查看可以提取额外逻辑的方法,例如搜索,这样您就不会创建重复的逻辑。

Repositories are nice and testable as long as you make sure not to let them get out of control. I give you this warning only because I've seen far too many people create monster classes out of repositories.

只要您确保不让它们失控,存储库就很好并且可以测试。我给你这个警告只是因为我看到太多人从存储库创建怪物类。

The repositories work with your objects. They are basically the intermediary which handles the persistence of your data. This abstraction allows you to hide from the rest of your code how you're persisting your data. In this case the implementations of your repositories will be using LinqToSql as I believe that is what you said you were using.

存储库适用于您的对象。它们基本上是处理数据持久性的中介。通过此抽象,您可以隐藏代码的其余部分,以便持久保存数据。在这种情况下,您的存储库的实现将使用LinqToSql,因为我相信这就是您所说的。

There are plenty of resources explaining the repository pattern.

有很多资源可以解释存储库模式。

#4


What you want is not a design pattern. You are looking for an ORM with scaffolding. I have used and highly recommend SubSonic - http://subsonicproject.com. You can read about its scaffolding features here: http://subsonicproject.com/web-forms-controls/the-scaffold/

你想要的不是设计模式。您正在寻找带脚手架的ORM。我已经使用并强烈推荐SubSonic - http://subsonicproject.com。你可以在这里阅读它的脚手架功能:http://subsonicproject.com/web-forms-controls/the-scaffold/

#1


For a set of admin screens that are just doing CRUD (Create, Read, Update, Delete) operations and with little in the way of business logic, I'd be quite tempted to more or less eschew design patterns and take a look at asp.net dynamic data. This is especially true if you want to minimise the amount of code you want to write.

对于一组只进行CRUD(创建,读取,更新,删除)操作并且几乎没有业务逻辑的管理屏幕,我很想要或多或少地避开设计模式并看看asp .net动态数据。如果要最小化要编写的代码量,尤其如此。

#2


This is not a design pattern... but I would strongly suggest using Dynamic Data. Jonathan Carter has some great articles about it: http://lostintangent.com/index.php?s=dynamic+data

这不是一种设计模式......但我强烈建议使用动态数据。 Jonathan Carter有一些很棒的文章:http://lostintangent.com/index.php?s = dynamic + data

#3


If you're really just doing some basic stuff like this: Search, GetById, Save, Create and GetAll, I would recommend you use repositories. If done wrong repositories can get really bad and nasty, but if you're really primarily limited to this set of operations you've basically described a repository with that set of operations.

如果你真的只是做一些基本的东西:搜索,GetById,保存,创建和GetAll,我建议你使用存储库。如果做错了存储库可能会变得非常糟糕和令人讨厌,但如果你真的主要限于这组操作,你基本上已经描述了具有这组操作的存储库。

You'll want to look at ways in which you can extract the extra logic for example of searching so that you're not creating duplicate logic.

您将需要查看可以提取额外逻辑的方法,例如搜索,这样您就不会创建重复的逻辑。

Repositories are nice and testable as long as you make sure not to let them get out of control. I give you this warning only because I've seen far too many people create monster classes out of repositories.

只要您确保不让它们失控,存储库就很好并且可以测试。我给你这个警告只是因为我看到太多人从存储库创建怪物类。

The repositories work with your objects. They are basically the intermediary which handles the persistence of your data. This abstraction allows you to hide from the rest of your code how you're persisting your data. In this case the implementations of your repositories will be using LinqToSql as I believe that is what you said you were using.

存储库适用于您的对象。它们基本上是处理数据持久性的中介。通过此抽象,您可以隐藏代码的其余部分,以便持久保存数据。在这种情况下,您的存储库的实现将使用LinqToSql,因为我相信这就是您所说的。

There are plenty of resources explaining the repository pattern.

有很多资源可以解释存储库模式。

#4


What you want is not a design pattern. You are looking for an ORM with scaffolding. I have used and highly recommend SubSonic - http://subsonicproject.com. You can read about its scaffolding features here: http://subsonicproject.com/web-forms-controls/the-scaffold/

你想要的不是设计模式。您正在寻找带脚手架的ORM。我已经使用并强烈推荐SubSonic - http://subsonicproject.com。你可以在这里阅读它的脚手架功能:http://subsonicproject.com/web-forms-controls/the-scaffold/