I have the following scenario:
Server
SQL Server 2008
Core (Entity Framework and business logic)
WCF Service
MVC Web application (for backend management)
Client
Local Database - a simplified model of the main database
WPF Client
Requirements
The client has to work fully offline, and persist data
Changed data should be pulled from the server over WCF service
Client should not change the data, but call a a WCF method (if not available queue the call)
Possible Solutions
Microsoft Sync Framework - I think its an overkill, because I mainly need one way synching, and also the data structure is not the same.
Datasets serialization over WCF, yes, because Datasets support merging and offline scenarios, but isn't It out of date?
Entity Framework? I tried to build a prototype, but EF doesn't seem to support my needs very well (I need to search for an entity, and change it if modified, or add it if not existant)
Question
What, do you think, is the most appropriate approach?
Is SQL Server Compact a good local db?
I am very interested in your thoughts. Thank you!
The Microsoft Sync Framework is in my opinion not appropriate because you have differing schemas. I can also imagine that you have some business rules about what data is allowed to change and how it should be synced.
The choice between DataSets and Entity Framework depends on your needs. An Object-Relation-Mapper comes in to view when you are really using an object model.
If your domain is complex enough and you have the knowledge using a full fledged Domain Model is definitely a nice solution that can scale really well and handle complex projects.
If your project is somewhat simpler and you don't want to build a Domain Model you can choose for DataSets.
Personally, I think that learning the ins and outs of Domain Modeling and the Entity Framework as an ORM is a nice choice for projects. When you have enough experience using these technologies, you will even favor them on small projects.
About the problems you where having with your EF prototype.
Because the data schema of the client and the server is different I would use custom Data Transfer Objects for moving data between the two. This way, you decouple the object models and they can change independent of each other.
The client knows everything there is to know about the data changes. Because it has a local representation of the server data it knows if data is added, changed or deleted. Why don't you add this knowledge to your server call? If you use a field in your DTO that states if the object is Added, Modified or Deleted, the server won't have to detect this.
Related
I have following layers in my applicaiton
Date Layer (reference to Model)
Business Layer(reference to Model ,Data)
Model
Service(WCF)-(reference to Model,Business Layer)
UI (WPF/Silver Light) - Connected via WCF service
How do i detect the changed poco entities in an ObservableCollection in UI layer?
for sending it back to server from client side for saving ? instead of sending all data back to sever side(via WCF)?
or
how to perform add/delete/update operation on entities in the collection in UI layer?
I am using
VS2010/2012
C#
EF 5
ADO.NET POCOEntityGenerator With WCF Support(for generating .tt templates from Model.edmx)
SQL Server 2012
Even though searched a lot of places I didn't find a proper solution..
please help if any ideas...
Thanks...
The method i followed to create My application is given below link
http://www.toplinestrategies.com/dotneters/net/wcf-entity-framework-and-n-tier-solutions-part-2/?lang=en/comment-page-1/#comment-1954
Only proper solution is to do the change tracking manually. Each POCO object will have IsDirty property and each property of this object will have IsDirty = true in it's setter.
One way to make it less manual would be to create a framework, that will create wrapper classes, that will do this for you, but this requires large dose of reflection and code-generation. Also, it will still require all properties to be defined as virtual.
But generally, you want to refrain from making UI that would require this kind of tracking. When you want to change an entity, load only that entity in Edit window.
POCOs are well-suited for transmitting data between client and server. However, if you’re looking for objects to actually work with on client and/or server side you may want to consider using self-tracking entities (STE) as these entities contain logic to track their actual changes and status.
Yet a better solution is to use the N-Tier Entity Framework which provides functionality to work with EF in n-tier applications. See http://ntieref.codeplex.com/ for more details.
If you are using EF, then your entities have a 'HasChanges' flag you can test against before submitting changes to your context.
e.g.
if (this.CurrentEntity.HasChanges || CurrentEntity.EntityState == EntityState.New)
{
this.SubjectContext.SubmitChanges(Submit_Completed, saveDetails);
}
Please excuse the noob question as I am new to integrating data with my applications. I've tried to find answers on the net, but not there yet.
I have an application I'm developing in C# on VS2010 which requires data in/out from a database. I am trying to figure out if its a DataSet or Entity Data Model I need to use when setting up a data source. My understanding was that it was the EDM which allowed me to treat tables/fields in a database as objects, but somehow it looks like I can do that with a DataSet too.
Some sources explain that a DataSet makes a cached copy of the Database which can then be manipulated.
Essentially my question is which should I use and what are the (dis)advantages of one over the other.
You have several options open to you when it comes to storing and retrieving data to/from a database:
At the very simplest level, use ADO.NET to open a connection to the DB, create a command and execute it. If you expect results back (i.e. SELECT ...) then you could call the command's ExecuteReader(...). Working in this manner results in very quick execution and the minimum of overhead, but you have to do more of the heavy lifting. If your app is simple, this is probably a good way to go. If your app is, or is likely to be more complex, you may want to consider other options...
ADO.NET DataSets are a reasonable DB IO mechanism, particularly for reading data from a DB. However, they can be a little cumbersome when trying to update the DB.
You could use an Object-Relational Mapper (ORM) like nHibernate or Entity Framework, but, frankly, that often results in your learning curve increasing dramatically while you figure out how to plug together the moving parts and make them work well together.
You might also consider a new variant of Entity Framework called Code First (CF): This allows you to pretty much design your code and CF will generate your EDM and handle the majority of the DB operations required for you to build your system. Scott Hanselman wrote up a nice intro into EF CF.
Having used practically every DB API and ORM on Windows over the last 20+ years, I am delighted with how CF is shaping up! EF 4.3 that shipped just a couple of weeks ago includes some key new improvements to CF including migrations which allow you to handle changes to your DB schema as it evolves. I've build 3-4 systems using EF CF over the last couple of months and am very happy - it's my favorite relational database IO mechanism at present.
If you want to really get into EF CF, I strongly recommend Julia Lerman's book EF CF - it's a short, nicely written, very useful guide that should take you no more than a day or two to work through the main sections of.
Hope this helps.
If you add a LocalDB data source to your project (because you want a small local database file) then when the Data Source Configuration Wizard pops up, it explicitly asks you whether you want to use a Dataset or Entity Data Model database model. Is this the situation you were facing? That was the problem I had that brought me to this entry.
There is no question that for an enterprise class application, or a website, you would want to investigate ADO.NET or an ORM, but it doesn't help answer this question, which has to do with what are the differences between choosing Dataset vs Entity Data Model in the wizard.
Essentially, Entity Data Model is the more recent technology. If you are unfamiliar with Dataset, then this is probably not the time to start using it.
If you're asking what are the pros and cons for ADO.NET (DataSet) vs EntityFramework (Entity Data Model) then there is a discussion that may help at ADO.NET Entity Framework or ADO.NET
EF will get you up and running pretty quickly but in my (very limited) experience its been a pain to maintain.
What is it that has determined that these are your only two options? There are far more available to you including many ORMs.
If your application is supporting a business application than queries get complex pretty soon. In such scenario, stored-procedures save a lot of time and are much easier to maintain and they work better with ADO.NET. In almost all scenarios, I would suggest using stored-procedures and ADO.NET. Move as much of the business rules and logic to stored procedures as you can...much easier to maintain this way.
Use Datasets (datatables) only to retrieve and read data. Any data that needs to be saved to database should be directly manipulated in the database ... no point doing it in dataset and then saving the same. In a multi-user environment it is almost always better to save the changes to database as soon as the user has clicked "save".
You may (should) use business objects within the application for business-logic processes.
Let us take a simple example of where you are saving a Contact (name, phone, email, address etc) and then retrieving a list of contacts added today...I would suggest you do it as follows:
1) Adding the contact - Client (web or otherwise) collects data --> data is saved in a Contact business object --> validate Contact object --> Call repository layer to save Contact object (adding a repository layer is useful but not-necessary to keep the data layer abstract from the client) --> Repository calls the data layer to save the contact object (here a simple ADO.NET call, using Command object, can be made to call the stored procedure to save the contact in database). No dataset was used in this use case.
2) Retrieving list of contacts -- Client calls the repository layer to get the list of contacts --> repository layer call the data layer to retrieve the data --> here the list of data is retrieved as a dataset(datatable) --> return the datatable back to the client and let the client read the data directly from datatable while rendering the data. Even a single contact can be retrieved as a dataset.
P.S: ORM is almost always an overkill. It is almost always used because certain developers like to keep everything object-oriented...so an extra layer gets added even though it does nothing useful (IMHO).
But, what if you have business logic (stored procedures) which can be used in many different applications.
So depends: if you make your application for different users with different backend storage, or you make many applications for users which doesn't change backend storage so often.
It is very important to have database integrity and rules independent from application (inner or outsource)
In my first c# project, I need to connect to a database server for multiple read only queries. Would anyone share experiences on how to organize the queries into the project? currently I just hardcoded query strings in the c# source files whenever needed. but it is hard to maintain and once something changes on the database server side I am in trouble. Or should I put all query strings in the .config file using appsettings? Are there better ways? I do not have rights to save stored procedures on the server. thanks.
There are different answers with varying levels of sophistication based on your needs. Except in the very smallest of projects, I create two class library projects for database access: one that contains the data model and queries and another test project that exercises the first project's queries. In simple solutions, you use this library in an ASP.NET or other project.
You should strongly consider learning an ORM like NHibernate or VS 2008/.NET 3.5's Linq-To-SQL or Entity Framework. Minimally, you MUST remember to use parameterized queries if you have a web-facing app.
In more sophisticated solutions you will completely encapsulate the database into it's own service, or tier. In my experience I had a data access tier that ran in it's own Windows Communication Foundation service, as a Windows Service, and it was the only service that could talk directly to the database or knew the database's data model. It would do all the interaction with the database, and then transform the data into different data models that are read by the other tiers. I typically create a project called "Contracts" that contains all the interfaces and data models that are communicated from the data tier to the rest of the system. The reason you do this is so that you avoid the pain you have mentioned: you can update the underlying database, ORM layer, and "common data models" and then not change the other tiers at all.
If this is your first project, try to keep thinks simple. If you add too much variables probably you'll end thinking more in technology than in solutions.
That said, if your queries don't expect to change it's parameters, you can use stored procedures. This approach also will help boost your queries as the execution plan will be kept in the database.
I'm writing a .NET application and the thought of implementing a data layer from scratch is icky to me. (By data layer I'm referring to the code that talks to the database, not the layer which abstracts the database access into domain objects [sometimes called the data access layer and used interchangeably with data layer].)
I'd like to find an existing generic data layer implementation which provides standard crud functionality, error handling, connection management - the works. I'll be talking to SQL Server only.
It doesn't matter to me if the library is in C# or VB.NET and I don't care if it's LINQ or ADO.NET. As long as it works.
** I want to emphasize that I'm not looking for data access technologies or mechanisms (e.g. LINQ, ORM tools, etc.) but rather existing libraries.)
If you are talking to only SQL Server the Linq to SQL is your best option. It is pretty easy to get up and running. You will get both the Data Layer and the Abstraction. All you have to do is provide a connection string to Linq to SQL and it will handle the rest.
If you are going to connect to other database than SQL you would want to with NHibernate.
NHibernate takes a little more work than Linq to SQL to get up and running. MS provided in Visual Studio a nice tool that can get you reading from a SQL database pretty quick.
Honestly as much of a fan as I've always been with NHibernate. With the latest release of Enterprise Library 5 Data Access Block that they added in the dynamic mapping support natively. I would have to strongly consider not using NHibernate on a project and instead use a forward database generation tool from my domain objects to create my database (perhaps even use NHibernate solely for the scheme export) or something like CodeSmith and use EntLib.
You can use easyobjects has a very small learning curve, and is very extensible.
From their web:
EasyObjects.NET is a powerful data-access architecture for the .NET Framework. When used in combination with code generation, you can create, from scratch, a complete data layer for your application in minutes.
I'd like to find an existing generic data layer implementation which provides standard crud functionality, error handling, connection management - the works. I'll be talking to SQL Server only.
Might want to check out Subsonic. Though I personally find it quite limited, it's certainly not an ORM, but a "query tool." It will make CRUD operations easy and straightforward, and it generates partial POCO classes for every table in your database, rather than trying to map from a database to a domain layer.
Microsoft's Entity Framework might be what you are looking for to releave you from writing "the code that talks to the database".
The best things are that it already ships with Visual Studio and - depending on your requirements - you can use most functionality out-of-the box or manually adjust it to your custom business logic via T4 templates.
You can use it for forward and reverse engeneering and being a microsoft technology it integrates well with other MS products like SQL server.
I started using it 3 months ago in my current project at work which is composed of several windows and WCF services to convert third party data into our own database scheme. From the experiences we made with it, we'll be using the EF in future project a lot more.
What would you expect this framework to do with your exceptions? If it can't connect to your database, what should it do - crash the application, show an error message (winforms or WPF or ASP)... the questions are endless.
An ORM such as those suggested elsewhere in these answers is likely to be the closest you're going to get. Expecting a third party framework to provide all your exception handling isn't realistic - how would a third party know how your application is supposed to behave?
The direct answer to your question asking for "an existing generic data layer implementation which provides standard crud functionality, error handling, connection management - the works" is simple: use ADO.NET. The answers everyone else have provided actually go beyond that functionality, but your responses suggest that you think that there's something even further beyond - something that implements your data layer for you. My suggestion is that what you're looking for probably doesn't exist.
I'm trying to tackle the problem of disconnected operation for an application with a relatively rich data layer, and it occurs to me that the most natural way to make this work is with a client-side database. I don't want to have to install a separate product, however, and I'm left to wonder if there are any layers out there where you can essentially link a database-like persistence layer into an application. Has anyone had any experience with this? Are there any good frameworks that cover this area?
I would recommend SQLite. It's a full SQL database engine wrapped in a single dll with no installation or maintenance that just ships with your app and runs in-process. There's a great .NET wrapper that integrates nicely and allows you to create custom functions in .NET.
http://sqlite.phxsoftware.com/
If you don't need the power of a relational database and want to simplify translation of your object model for persistence, you should look into DB4O - it's an object database that can run on your client and transparently persist your classes.
You can use NHibernate with sqlite or sqlce database. We use sqlce.
.Net has strongly typed datasets, which work great for this purpose.
http://msdn.microsoft.com/en-us/library/esbykkzb%28VS.71%29.aspx
Even thought you don't want to install another product, you might want to consider SQL Server Compact Edition. Although you do need to install it, it's free, and installs no new Windows services.
The databases themselves are simply a single file per database. LINQ to SQL and LINQ to Entities are still supported, and you can even get a Windows Mobile version.
Are you looking for a database-like persistence layer because you want the query power of a database on the client side, or for persistence between application runs, or both?
If you need both, or just the persistence, then any one of the other answers showcasing integrated DB libraries will do (like this one for SQL Lite).
However, if the only thing you need is the ability to perform complex queries against in-memory data then I would highly recommend using plain-ol LINQ-to-Objects, assuming the option is available to you.