当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Routes with 16-bit Guids seem Crazy?

问题描述:

I just looked at my Database Schema from my DBA and its using 16bit unique Identifiers as the Primary Key. The question I have how do I used this in the routing for MVC.

Something like http://www.app.com/project/21212/product/212121

This is a midsize enterprise application, why would you need a GUID for our tables anyway?

I know we can create a friendly ID field, but I know MVC Routing doesn’t recommend using database IDs in Routes..

So I guess my questions are:

Why would we need 16 Bit Guids for our Primary Key?

How could I use that in the Route. The route isn’t supposed to contain and Database IDs.

网友答案:

Lets look at this very page as an example. I think we can all agree that StackOverflow is a successful MVC application...

http://stackoverflow.com/questions/4079861/routes-with-16-bit-guids-seem-crazy

What is that "4079861" in there? A database ID?

Note that the database ID is really the only important part as these links also arrive at the same location:

http://stackoverflow.com/questions/4079861/

http://stackoverflow.com/questions/4079861/Foo

So, the short answer is: yes, your routes will probably have a big ugly Guid in them. Go talk to your DBA if you have a problem with that.

网友答案:

On the DB part of the question

The decision of what kind of DB keys you are going to use should be completely independent of your MVC routes. DBAs might chose to use whatever value they think is appropriate to your application without having to worry about how you are going to craft your routes. I couldn't tell whether they make sense for your domain or not.

On the route/URL part of the question

Depending on what you are trying to do adding a GUID to a route might not be the best idea for a route/URL. The authors of "ASP.NET MVC in Action" (page 95) give some good guidelines on how URLs should be:

  • Simple and clean
  • Hackable
  • Allow URLs parameters to clash
  • Short
  • Avoid exposing database IDs where possible
  • Consider adding unnecessary information

If you have GUIDs as database IDs see if you can use another value to craft the route to each resource/record. For example the name of the product plus the last 4 digits of the db ID, or another unique and user friendly (see guidelines) value that you can come up with based on the information that you are trying to access.

网友答案:

You should ask yourself: Is there another way to uniquely identify my [insert name] ?

@Mark gives the StackOverflow example. The nice part is that it's a number, even if it's a long one. Numbers are nicer than GUIDs.

Your options are probably:

  • create a simple number to GUID mapping in the database, creating a redundant unique identifier, for routing purposes
  • actually display the GUID as part of your routing
  • find some other way you could uniquely identify your record (i.e. date + name combination, which blogs use) - although here you have to make sure you don't allow duplicate entries of your routing identifier.

What you end up using will depend entirely on your situation and requirements.

分享给朋友:
您可能感兴趣的文章:
随机阅读: