Asked by:
Business logic advise needed

Question
-
User-1104215994 posted
Hello,
I am trying to implement a web <g class="gr_ gr_22 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="22" data-gr-id="22">api</g> which basically calls a third party rest service and returns back serials/pins to the client. This part works well but now I was asked to implement a business logic which requires to query our database for serials/pins first. If required serials/pins <g class="gr_ gr_23 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="23" data-gr-id="23">is</g> not in our database then I will call this third party rest service.
I query our database and if there are serials/pins I decide to update those serials/pins (sort of a flag) with a unique id. The client then sends another request with the same unique id and token to complete the process. I will check my database to search for this <g class="gr_ gr_20 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" id="20" data-gr-id="20">ids</g>, if there are these ids, I am planning to mark <g class="gr_ gr_29 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="29" data-gr-id="29">them as</g> "used" and return serials/pins to the client.
Does this process seem OK? Any other ideas that can help me to build better logic? Are there any flaws in my logic? (bottlenecks, table locks <g class="gr_ gr_67 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="67" data-gr-id="67">etc</g> etc)
Best Regards.
Sunday, April 14, 2019 8:32 AM
All replies
-
User1120430333 posted
I don't see why it would not work, but I wouldn't be using Entity Framework for this. I would go with ADO.NET, MS SQL Server Command objects and parmterized T-SQL or Store Procedures to take advantage of MS SQL Server Query Plan Caching, if the WebAPI is going to be used frequently or to avoid EF cold starts if the WebAPI goes out of scope setting idle.
Sunday, April 14, 2019 10:25 AM -
User-1104215994 posted
Hi DA924,
WebAPI is <g class="gr_ gr_7 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="7" data-gr-id="7">planining</g> to be used frequently so what you mean is EF performs <g class="gr_ gr_8 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="8" data-gr-id="8">poor</g>? If so I have to change all of the EF to ADO.NET. Reading and updating the same table makes me uncomfortable in general. Is it likely to have table lock issues?
Sunday, April 14, 2019 10:35 AM -
User475983607 posted
Hello,
I am trying to implement a web api which basically calls a third party rest service and returns back serials/pins to the client. This part works well but now I was asked to implement a business logic which requires to query our database for serials/pins first. If required serials/pins is not in our database then I will call this third party rest service.
I query our database and if there are serials/pins I decide to update those serials/pins (sort of a flag) with a unique id. The client then sends another request with the same unique id and token to complete the process. I will check my database to search for this ids, if there are these ids, I am planning to mark them as "used" and return serials/pins to the client.
Does this process seem OK? Any other ideas that can help me to build better logic? Are there any flaws in my logic? (bottlenecks, table locks etc etc)
Best Regards.
We need the source code and schema if you want a code review from the community.
Sunday, April 14, 2019 11:20 AM -
User1120430333 posted
Hi DA924,
WebAPI is planining to be used frequently so what you mean is EF performs poor? If so I have to change all of the EF to ADO.NET. Reading and updating the same table makes me uncomfortable in general. Is it likely to have table lock issues?
Yes EF is slow manly due to object generation in your case. There are some things to consider concerning EF performance if you plan on using EF. But mainly this new logic you're talking about can be done without using EF. And just use straight up ADO.NET and MS SQL Command objects
https://docs.microsoft.com/en-us/ef/ef6/fundamentals/performance/perf-whitepaper
https://exceptionnotfound.net/dapper-vs-entity-framework-vs-ado-net-performance-benchmarking/
I am not saying use Dapper, becuase you don't need an unknown ORM introduced into the solution. ADO.NET you could use with no problems, just don't use datasets and datatables, which are very slow, but rather use custom object and collection.
Sunday, April 14, 2019 11:28 AM -
User-1104215994 posted
mgebhard
We need the source code and schema if you want a code review from the community.There is no code yet <g class="gr_ gr_5 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="5" data-gr-id="5">mgebhard</g>, just business logic and possible issues to consider upfront. What are your <g class="gr_ gr_55 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="55" data-gr-id="55">thoughs</g> about EF vs <g class="gr_ gr_76 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation multiReplace" id="76" data-gr-id="76">ADO.</g>Net?
Sunday, April 14, 2019 12:09 PM -
User475983607 posted
cenk1536
There is no code yet mgebhard, just business logic and possible issues to consider upfront.The design is not specific enough to provide meaningful feedback. We need to know at least the table schema.
What are your thoughs about EF vs ADO.Net?I thought you are already using EF? You can go with ADO.NET and you'll need to write logic to populate types.
Sunday, April 14, 2019 12:21 PM -
User-1104215994 posted
The design is not specific enough to provide meaningful feedback. We need to know at least the table schema.Here are the draft models:
public class GameBank { public int GameBankID { get; set; } public string referenceId { get; set; } public string productCode { get; set; } public int quantity { get; set; } public string version { get; set; } public DateTime? requestDateTime { get; set; } = DateTime.Now; public int? customerID { get; set; } public string password { get; set; } public DateTime? responseDateTime { get; set; } = DateTime.Now; public string initiationResultCode { get; set; } public string companyToken { get; set; } public int used { get; set; } public string productDescription { get; set; } public string currency { get; set; } public double unitPrice { get; set; } public double totalPrice { get; set; } public virtual List<GameCouponBank> coupons { get; set; } } public class GameCouponBank { public int Id { get; set; } public int GameBankID { get; set; } public DateTime? expiryDate { get; set; } public string Serial { get; set; } public string Pin { get; set; } }
I thought you are already using EF?<g class="gr_ gr_230 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="230" data-gr-id="230">Yes</g> I am using EF but are there any known performance issues?
Sunday, April 14, 2019 12:29 PM -
User475983607 posted
Yes I am using EF but are there any known performance issues?You're asking questions that are too vague to answer. Yes, EF is slower than ADO.NET but EF populates types and this is usually a convenience that out weighs writing your own code to populate a type. There are other ORMs that are faster than EF as well like Dapper.
But the question is, what are your performance benchmarks? Once we have specifics then we can guide you. If you do not have performance benchmarks then shame on you for dragging us down the rabbit hole.
The one-to-many model shown above is very basic. Why do you have concerns?
Sunday, April 14, 2019 12:47 PM -
User-1104215994 posted
Why do you have concerns?I am trying to eliminate flaws upfront and since you are very experienced than me, I think it is a good idea to get feedbacks or warnings from you.
Sunday, April 14, 2019 12:52 PM -
User475983607 posted
I am trying to eliminate flaws upfront and since you are very experienced than me, I think it is a good idea to get feedbacks or warnings from you.You might as well ask, "How long is the string?".
I would do the basics. Use async/await, EF, and standard SQL normalization. If you're queries filter by serial and pin fields then index the serial and pin fields.
Other than that, if you have specific performance benchmarks then we need to know what they are. If you want a code review then we need your code.
Sunday, April 14, 2019 1:14 PM -
User765422875 posted
A full blown ORM is rather convenient, but there are performance considerations you want to take into account for Entity Framework. They are pretty well documented.
https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/ef/performance-considerations
I rarely use an ORM if performance is paramount as I want more control. I typically opt for a micro ORM like Dapper calling stored procedures.
Monday, April 15, 2019 3:30 PM -
User-1104215994 posted
Since there won't complex queries and I also use async/await, I think performance will not be a huge issue.
Monday, April 15, 2019 6:43 PM -
User765422875 posted
Then you should be fine. Using async EF methods will allow your app to scale better. When you need to perform I/O intensive and other non-CPU bound requests, you will get the benefit by utilizing the asynchronous operations.
Hopefully, this helped.
Monday, April 15, 2019 7:03 PM