data-access-layer

Reuseable ObjectContext or new ObjectContext for each set of operations?

假如想象 提交于 2019-11-30 05:31:33
I'm new to the Entities Framework, and am just starting to play around with it in my free time. One of the major questions I have is regarding how to handle ObjectContexts. Which is generally preferred/recommended of these: This public class DataAccess{ MyDbContext m_Context; public DataAccess(){ m_Context = new MyDbContext(); } public IEnumerable<SomeItem> GetSomeItems(){ return m_Context.SomeItems; } public void DeleteSomeItem(SomeItem item){ m_Context.DeleteObject(item); m_Context.SaveChanges(); } } Or this? public class DataAccess{ public DataAccess(){ } public IEnumerable<SomeItem>

How do I determine if a column is in the primary key of its table? (SQL Server)

痴心易碎 提交于 2019-11-30 04:53:08
I am currently using... select Table_Name, Column_name, data_type, is_Nullable from information_Schema.Columns ...to determine information about columns in a given database for the purposes of generating a DataAccess Layer. From where can I retrieve information about if these columns are participants in the primary key of their table? Galwegian Here is one way (replace 'keycol' with the column name you are searching for): SELECT K.TABLE_NAME , K.COLUMN_NAME , K.CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS C JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS K ON C.TABLE_NAME = K.TABLE

Ways of unit testing data access layer

南笙酒味 提交于 2019-11-30 03:24:52
I have be trying to look for an effective way in unit testing my data access layer in C#. I'm primary a Java developer and have only used C# for about 6 months, in the past i've used a library called DBUnit to test against a known state database. I haven't been able to find a similar active library that can be used, closest seems to be nDBUnit but it hasn't been active for awhile now. There seems to be a lot of conflicting methods on how and why in C#. Ideally I want to test the data access layer using mocking without the need to connect to a database and then unit test the store procedure in

Difference between Data Access Layer and Model in MVC

倖福魔咒の 提交于 2019-11-29 20:01:39
I have implemented what I thought was a pretty decent representation of MVC in several web applications but since having joined crackoverflow, I'm finding that perhaps my initial definitions were a bit simplistic and thus I'd really like some clarification on the differences between the Data Access Layer and the Model or Domain Layer of a web application. For context, I currently use data access objects that implement the CRUD functions for a single record in the table that the object represents as well as a get() function that returns an object that allows me to iterate through all of the

Should I return IEnumerable<T> or IQueryable<T> from my DAL?

梦想的初衷 提交于 2019-11-29 19:32:59
I know this could be opinion, but I'm looking for best practices. As I understand, IQueryable<T> implements IEnumerable<T> , so in my DAL, I currently have method signatures like the following: IEnumerable<Product> GetProducts(); IEnumerable<Product> GetProductsByCategory(int cateogoryId); Product GetProduct(int productId); Should I be using IQueryable<T> here? What are the pros and cons of either approach? Note that I am planning on using the Repository pattern so I will have a class like so: public class ProductRepository { DBDataContext db = new DBDataContext(<!-- connection string -->);

Use BLL functions without reference the DAL in my API

别等时光非礼了梦想. 提交于 2019-11-29 18:37:58
I have 3 project (C#) API, BLL and DAL. The DAL reference the DAL and the API reference the BLL. In my API I need to use all the CRUD functions but I can't use the function from my BLL because VS said that "The type "blabla" is defined in a assembly that is not referenced. You need to add the reference (DAL)" but I don't want to referenced the DAL in API project. Is there a way to do it without use my DAL project ? Amit Joshi In my view, what you are trying to achieve is good way to architect the project. I am also doing same; just small difference that I will explain below. Not referencing

Execute stored procedure w/parameters in Dapper

落爺英雄遲暮 提交于 2019-11-29 17:03:58
问题 I'm using Dapper (thanks Sam, great project.) a micro ORM with a DAL and by some reason I'm not able to execute stored procedures with input parameters. In a example service I've the following code: public void GetSomething(int somethingId) { IRepository<Something, SomethingEnum> repository = UnitOfWork.GetRepository<Something, SomethingEnum>(); var param = new DynamicParameters(); param.Add("@somethingId", dbType: DbType.Int32, value:somethingId, direction: ParameterDirection.Input); var

Mocking vs. Test DB?

为君一笑 提交于 2019-11-29 10:47:37
问题 Earlier I asked this question How to correctly unit test my DAL?, one thing left unanswered for me is if to really test my DAL is to have a Test DB, then what is the role of mocking vs. a testing DB? To add on this, another person suggested to "use transactions and rollback at the end of the unit test, so the db is clean", test db that is. What do you guys think of this testing + test DB + transaction rollback (so db is not really written) approach to test DAL? To be complete, my DAL is built

How to correctly unit test my DAL?

一笑奈何 提交于 2019-11-29 07:33:49
问题 I'm new to unit testing. But how do I unit test my DAL which is written with Entity Framework, so I can make sure my DAL code is working correctly but no database is actually touched? Could someone give as much detail as possible please. 回答1: If you want to test that your data access layer works correct you really need to test it against a database at some point as otherwise you aren't actually testing it works. 回答2: When I unit test my DAL I use transactions and rollback at the end of the

What is the best practice for multiple “Include”-s in Entity Framework?

依然范特西╮ 提交于 2019-11-29 06:58:48
问题 Let's say we have four entities in data model: Categories, Books, Authors and BookPages. Also assume Categories-Books, Books-Authors and Books-BookPages relationships are one-to-many. If a category entity instance is retrieved from database - including "Books", "Books.BookPages" and "Books.Authors" - this will become a serious performance issue. Moreover, not including them will result in "Object reference is not set to an instance of an object" exception. What is the best practice for using