Making a Linq property optional while using in Webservice - asp.net

I am using LINQ object in my webservice and consumed by iphone and android developers.
Now i have added some new fields in a table which in turn will be used linq object.
I want to know some way by which i can mark some linq properties as optional i.e. if iphone and android deve. doesnt pass that properties then no error will show up.
Thanks
Gaurav

First, it is not recommended to embed your LINQ objects directly in a web service. Create data objects specifically for the web service that captures the relevant parameters for each web service call. This will protect your service consumers from changes to the underlying data storage and allow you to more tightly control what is known to the consumers. You can then use the adapter pattern to convert objects in your web service to LINQ objects for submitting to database.
Secondly, regarding your question about optional parameters. With custom data objects, this will be easily done with nullable properties (for primitive types), or just null for object types. If you must use the LINQ objects, making the fields nullable in the database will also cause the properties to be nullable, and thus optional to service consumers.

Related

hiding method from certain layers in project

I was looking through an old project and wanted to see if anyone had a suggestion on how to hide certain methods from being called by various layers. This was a 3 tier project, webapplication -> web service -> database
In the application there is a User object for example. When a User was being updated, the webapplication would create a User object and pass it to the webservice. The webservice would use the DataAccessLayer to save the User object to the database. After looking at this I was wondering if instead I should have made a Save method in the User class. This way the service and simply call the Save on the User object which would trigger the db update.
However doing it this way would expose the Save to be called from the webapplication as well, correct? Since the webapplication also has access to the same User object.
Is there anyway around this, or is it better to avoid this altogether?
There is a separation of concerns by keepeing the User object as object that only holds data with no logic in it. you better keep it separated for the following reasons:
As you stated, it is a bad practice since the Save' functionality will be exposed to other places/classes where it is irrelevant for them (This is an important for programming generally).
Modifying the service layer - I guess you are using WCF web service as you can transfer a .NET object (c#/VB) to the service via SOAP. If you put the saving logic in the 'User' object, you can't replace it another webservice that receives a simple textual data structures like JSON or XML or simply doesn't support .NET objects.
Modifying the data storage layer - If you want, for example, to store the data inside a different place like other database such as MongoDB, RavenDB, Redis or what ever you want, you will have to reimplement each class that responsible for updating the data. This is also relevant for Unit Testing and Mocking, making them more complicated to interrogate.

Are linqtosql objects serializable?

Hi I am developing a webservice via asp.net 3.5 , I wonder if I use lintosql as ORM then different datatypes that I use to return (datatable,object,List,class members, etc) would be serializable by default ? or I need to modify linqtosql classes to make them serializable ?
It is possible by setting the Serialization Mode property to Unidirectional. I would however recommend that you create your own DTO's and expose those in your webservice instead of auto generated linq entities. It'll give greater control over you API.
For more information on the subject: LINQ to SQL serialization capabilities

xml serialization and de-serialization of generic list in C#.net

I am preparing a code structure like the following;
WCF service - all business logic can implement here. All service methods can takes input arguments as string(XML data) and can return string (XML data) or int.
Client Website - ASP>NET web site with entity layer and business logic layer(BAL layer).
Flow is like this:
client Website --->BAL--> (WCF service----> DAL Layer---->Database)
BAL layer consume wcf service
For this purpose, in BAL layer, I want to create a list of entity class and do XML serialization(list to XML format) and pass it to a WCF service methods.other side in WCF service method de-serialization(XML to list).
How do I do this in ASP.NET and WCF?
You don't do that. This seems to be a common misunderstanding for new users of WCF.
You define your DataContracts which contains your data, pass them to your WCF service, and it takes care of serializing/deserializing them.
You do not serialize them to XML before passing them through the service.
WCF will automatically serialize a list for you, but the default collection type it is deserialized to on the client side is an array.
You can change this default collection to a list (or linked list, or anything else available) by right clicking on your service reference in the project for your client application, selecting "Configure Service Reference...", and selecting System.Collections.Generic.List from the collection type drop down menu.
If you are sending a list from a client to the WCF service, it is sufficient to define you Service method's parameter as a list (if it contains simple .NET types), otherwise you need to define a special data contract as per the other answer.

How do I serialize Linq to SQL entities for use in out-of-proc Session State?

I need to put some of the entities created via a.dbml Linq-To-Sql file into Session State. Because I am using out-of-proc State Server, they need to be serializable.
How can I achieve this?
I have tried setting the Serialization mode in the .dbml file to 'Unidirectional'.
Most of the time it is impossible to serialize LINQ to SQL entities, because they reference all sorts of objects that can't be serialized or you would end up serializing an enormous object graph.
It's best to create Data Transfer Objects that you specially created for this. You can make such objects [Serializable], which allows them to be saved in an out-of-proc state (or send to a client using a web service). DTO's give you full control over what gets serialized, because they would contain primitive types or a collection of other DTO's.

How to expose custom object (with sub objects) in web service?

I'm kinda new to web services and want to make sure I am doing things correctly.
I have a custom object which has sub objects as well. (let's say Company object, sub object is collection of Employee objects)
I want the web service to return a collection of Company objects. Do I make the service return a Dataset and custom generate a dataset with datatables representing the different objects?
What is the best way to do this? I tried to just serialize it, but that doesn't seem to work either.
I tried this dll
http://www.codeproject.com/KB/linq/linqsqlserialization.aspx
But the output XML doesn't seem to include the sub object.
Whether you're using the 2.0 framework (with ASMX web services, which are no longer supported) or the 3.0 framework (with WCF), both will handle return of complex objects provided they are serializable. In the 2.0 framework, that means capable of marking your objects with the [Serializable] attribute. In the 3.0 framework, you're implementing serialization using the [DataContract] attribute. http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute.aspx and http://msdn.microsoft.com/en-us/library/system.serializableattribute.aspx.
Both frameworks will enable the client-side WSDL in preparation for clients to consume your complex objects. Since they're non-primitives, you'll be limited to SOAP-based clients because the return payload will require complex representation.

Resources