Simulate Database access on .NET - asp.net

I have an ASP.NET solution that consist of two different projects:
Project One is the ASP.NET pages, javascript and CSS. Project Two is an encapsulated DLL that is reference by project One, and have all the logic for database Accessing.
Basically, project One collect data and creates instances of classes defined on project Two, and call method on those classes that fisically access de database (inside Project Two code) and return object of type List of(ObjectType)
Now I need to send a copy of the project One to a third party programmer, but i don't want to send Databases, so my idea is to create a copy of project Two (a new DLL), that simulates database access but insted of get data from the database returns fixed data (hardcoded) in the exact same format.
So my question is: How can I Hardcode that data on the DLL without having to create object manualy one by one?.
My first attemp is to serialize an object already returned by a database to XML with this code:
Dim sw As New System.IO.StringWriter
Dim ser As New System.Xml.Serialization.XmlSerializer(GetType(List(Of User)))
ser.Serialize(sw, Users)
Debug.WriteLine(sw.ToString)
That creates an string with all the data. But it is possible to re-create the collection from this result? Is there a better way to do this? thanks!!!

Related

How to put ASP.NET Json WebHelper object into a database

I want to take the data from my json file, parse and send to my database. Each field in my json objects have to go to it's corresponding column in my table. I don't have SQL Server 2016 so I need to do this in my ASP.NET Webpages site using razor. I was able to get the file data and I used Json.Decode to make it a object I guess?? Here is how I got the JSON file data
#{
Layout = "~/_SiteLayout.cshtml";
var json =File.ReadAllText(Server.MapPath("~/App_Data/enforcement_actions.json"));
var data = Json.Decode(json);
}
I am completely lost from this point as far as now taking the 'data' variable, parse, and send to my database file. Any help would be great.
Look for Entity Framework and mdf file here: Entity Framework on .mdf file
After that, you have to look to how to map classes on Entity Framework. You see that here: How to map an Entity framework model to a table name dynamically
And finally you'll save your data on the database. You can see it here: Insert data using Entity Framework model

Multiple Entity framework context in asp.net

I have a web site similar to wizard from 4 steps . The site is implemented by asp.net web forms.
The wizard try to build and object and add it to the DB.
The object graph is following :
class A { B bObject ; C cObject ; D dObject}
class B {} ; class C{}; class D{};
the objects b,c,d are fetched from the database during the wizard step and filled into A.
As you know , b,c,d fetched from the DB by different contexts.
When I come to the final step to save A to the DB , the context recognize b,c,d as new objects and not just unmodified so only link them because they come from different contexts.
Any suggestion to make a clean way solving this issue ?
The different contexts caused because of the post packs.
This is one of those cases where the best answer is "don't do that!"
One way to handle this is to just have the wizard steps gather all the data necessary to create the database tables from the user, and save the data in Session state. Only when the final wizard step is executed would you then take the data from Session state and use it to create EF entities and then save the changes, all on a single context.
Another way would be to save the object graph as EF objects (still in Session state), but then, in the final wizard step, open a context and use the Attach method to attach the objects to the current context. Again, this uses a single context.

How to use ADO.net (SqlConnection) in ASP.net MVC3

I am new to MVC3 and could not find a sample that uses SqlConnection with MVC3 to connect to an existing database. All the samples I found like MusicStore and others are using EF.
Read through this tutorial; it shows you exactly how all of the ORM stuff works so you can get going quickly.
From your contour call off to your repository pattern object ex ICustomerRepository.
Your repository calls your data access layer which returns some data transfer object (or some choose a domain object ie Customer)
Then your view model is populated from that object.
Some choose to chop out the transfer objects and send domain objects to the view rather than view models but the recommended approach is view models.
Your sqlconnection code goes in your data access layer. Do not stuff this code into your controller.

The purpose of Include() in ASP.NET MVC + Entity Framework

In the controllers generated by Visual Studio, as well as the sample application (ContosoUniversity), the Index action always has something like
var departments = db.Departments.Include(d => d.Administrator);
What's the difference between that and
var departments = db.Departments;
First I suspected that the first one (with Include) enables the view to retrieve department.Administrator. But the second one (without Include) seems to be able to do that as well.
The Include tells Entity Framework work to eagerly load the Administrator for each Department in the results. In this case, Entity Framework can use a SQL join to grab the data from both tables in a single request.
The code will still work without the Include, but the first time you access a Department's Administrator, EF will need to hit the database to load it (since it wasn't pre-loaded). Loading data on demand (lazily) is a nice feature but it can be a serious performance problem (known as an N+1 problem). Especially if you are accessing the Administrator for each Department (for example, in a loop) - instead of one database call, you will end up with many!
In first case (with Include) when you write department.Administrator servers the object from memory that has been eagerly loaded due to Include method. In the second case, an sql statement will be executed to fetch the Administrator record from the db for each department object.
See the "Lazy, Eager, and Explicit Loading of Related Data" section in this tutorial:
http://www.asp.net/entity-framework/tutorials/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application
var departments = db.Departments;
This will retrieves the aggregate domains only if LazyLoadingEnabled is enabled & MultipleActiveResultSets is set to true in connection string.

DataTable Wrapper or How to decouple UI from Business logic

I am using web forms, C#, Asp.net.
As we all know, in this model UI and business logic are often mixed in. How do I separate these effectively?
The example I would like to use is:
I have a GridView and a DataTable (GridView binds to the DataTable and DataTable is fed from the stored procedure).
I would like the GridView (UI) and DataTable (business logic) to be decoupled.
Is it worth it to write an wrapper for DataTable? Are there practical patterns that have been proved and tested that you could recommend to be followed?
If someone with experience could shed some light, that would be awesome.
And, as a final note I would like to say that ASP MVC is not an option right now, so don't recommend it.
My database access layer returns a DataTable.
Note that I HAVE to use this database layer as this is a company policy.
I went through this recently while decoupling much the same thing from our UI layer.
You can see my progress here and here.
In my opinion, A DataTable does not represent business logic. Specifically, it's data pulled directly from the database. Business logic turns that data into a truly useful business object.
The first step, then, is to decouple the DataTable from the Business object.
You can do that by creating objects and List<object> that make up DataTables and Collections of DataTables, and then you can make a ListView that displays those Objects. I cover the latter steps in the links I posted above. And the former steps are as easy as the following:
Create a class that will represent your object.
iterate through your DataTable (or DataSet, or however you retrieve the data) and shove those fields into properties of that object (or that List<T>);
return that List to the Gridview or ListView to display.
This way your ListView or Gridview won't be tightly coupled to the method that you are retrieving your data. What happens if you decide to get your data from a JSON query or a XML file later on? Then you'd have to build this into there.
Step 1 - Getting Data From Database
There are multiple methods to get data from a database, there's no way I can go through all of them here. I assume that you already know how to retrieve data from a database, and if you don't, there are quite a few links to follow. Let's pretend you've connected to the database, and are using an SQLDataReader to retrieve data. We'll pick up there.
Class Diagram
Foo
----
id
Name
Description
And here's the method:
private void FillDefault(SqlDataReader reader, Foos foo)
{
try
{
foo.id = Convert.ToInt32(reader[Foo.Properties.ID]);
foo.Name = reader[Foo.Properties.NAME].ToString();
if (!string.IsNullOrEmpty(
reader[Foo.Properties.DESCRIPTION].ToString()))
foo.Description =
reader[Foo.Properties.DESCRIPTION].ToString();
else foo.Description = string.Empty;
}
catch (Exception ex)
{
throw new Exception(
string.Format("Invalid Query.
Column '{0}' does not exist in SqlDataReader.",
ex.Message));
}
}
Once that happens, you can return a list by going through that process in a while loop that targets the SQLDataReader.Read() function.
Once you do that, let's pretend that your Foo being returned is a List. If you do that, and follow the first link I gave above, you can replace Dictionary<TKey, TValue> with List<T> and achieve the same result (with minor differences). The Properties class just contains the column names in the database, so you have one place to change them (in case you were wondering).
DataTable - Update Based on Comment
You can always insert an intermediate object. In this instance, I'd insert a Business Layer between the DataTable and the UI, and I've discussed what I'd do above. But a DataTable is not a business object; it is a visual representation of a database. You can't transport that to the UI layer and call it de-coupled. They say you have to use a DataTable, do they say that you have to transport that DataTable to the UI? I can't imagine they would. If you do, then you'll never be de-coupled. You'll always need an intermediate object in between the DataTable and the UI layer.
I'd start by decoupling the data table right into the trash can. Build a domain layer, and then some type of data access layer which deals with the DB (ORM recommended).
Then build a servicing layer which provides the data to the UI. All business logic should be within the service or the entities themself.
Consider implementing MVP (model view presenter) pattern. It gives you separation of biz logic through presenter interface, which also allow better unit testing capabilities. Your codebehind of aspx page is then just connector of events and getter/setter of properties. You can find it in MS pattern&practices enterprise application blocks (CAB - composite application block - if i'm not mistaking).
You can read more about it here: http://msdn.microsoft.com/en-us/magazine/cc188690.aspx
But also going from DataTable/DataSets to objects (POCO) is preferred.

Resources