Extending LINQ classes to my own partial classes in different namespaces? - linq-to-sql

I have a .dbml file which of course contains the auto-generated classes based on my tables.
I would however, like to extend them to my own classes. Typically I design such that each of my tables get their own namespace in their own folder containing all of their associated dao and service classes. So if I am dealing with a page that only has to do with 'customers' for instance, I can only include the customerNS.
But when using LINQ I seem to be unable to do this. I have tried removing a default namespace from the project, I have tried putting the .dbml file into it's own folder with a custom namespace and then adding a 'using' statement, but no nothing works.
I also saw the Entity Namespace, Context Namespace, and Custom Tool Namespace properties associated with the .dbml file and tried setting all these to names x and trying 'using x' in my other class to allow me to extend partial classes, but it just doesn't work.
Is this possible or do I have to keep all extended partial classes in the same namespace as the .dbml file?

If the types don't have relations the answer is simple: use multiple dmbl files. If you need relations and you also want multiple namespaces read on.
You might be able to get this done with a T4 template file. In VS2010 there's a template to create one (it's called a generator template or something). For VS2008 you can find one on codeplex
The changes you'll need to make to the standard template is you need to make sure that all properties use fully qualified names (because the related types are now in different namespaces). And for most control you can probably skip the namespace info from the generated classes (so you can define it in your partial classes).

You have to keep all linq classes in one namespace. Why you trying to put DO classes in different namespaces?

Related

Concept of Namespace in Dotnet? [duplicate]

This question already has answers here:
C# namespace alias - what's the point?
(11 answers)
Use of Namespaces in C#
(6 answers)
Closed 8 years ago.
I searched many articles to understand the concept of namespace? But I could not understand.
Can anyone explain the concept of namespace with simple example? Why do we import namespace?
The following is pseudo-code, I hope it's clear
namespace1.SomeClass
namespace2.SomeClass
var x = new SomeClass(); //which class are we trying to instantiate?
var y = new namespace1.SomeClass(); //now compiler and everyone else knows
A namespace is used to organize objects in categories and control the scope of objects.
More details about .NET namespaces on : http://msdn.microsoft.com/en-us/library/0d941h9d
Notice that the namespace concept is not limited to .NET but to many programming languages.
Why Namespace?
Namespaces are used to organize code. It lets you organize code and gives you a way to create globally unique types and avoid name collisions.
e.g.
Suppose, you have created a class Foo in your code. In same project, you are using some third party library, in which also a class with same name exists. In this case, when you are referring class Foo, compiler won't be able to resolve it.
But, this problem can be solved by namespaces. The Foo class in the library you are using, belongs to some namespace specified by developer of it. (Usually, it contains company name or something unique identifier). And your Foo class belongs to namespace you have specified. So, at the time of using it, you can specify fully qualified name of the class like <Namespace>.Foo. Which will make it easy for compiler to resolve reference.
Also, you yourself can categorize your classes using namespace to bifurcate it according to their purpose. Which will be easier to maintain. (e.g. CoreFramework.Foo, UIHelper.Bar, etc...)
Why do we import namespace?
Now, at the time using class you have categorized by namespace. You will have to tell compiler in which namespace the referring class contains. By default, compiler looks for the classes in same namespace. If the class you are referring belongs to another namespace, either you will have to specify fully qualified name of the class (i.e. Namespace.Foo) or you can inform compiler at the beginning of the class by using import statement that, code withing the class contains references to the classes belonging to this namespace.

ScriptIgnore in LINQ to SQL

I have a LINQ to SQL entity that I will be serializing and returning as JSON over a webservice call.
There is one property on that entity that I would like not to be serialized. For this, there is generally the [ScriptIgnore] attribute, which works exactly the way I want if I manually add that to the designer.cs file.
Now, since the designer file is automatically generated, I would prefer not having to manually edit it, as any changes could easily be overwritten. My question is thus: is there any way to annotate the property so that it is excluded upon serialization, directly in the DBML editor?
If the answer is no; are there any solutions to this that are neater than manually setting the property to null before serializing it, or returning an anonymous type identical except for that one property? In MVC.NET, is there any way to pass parameters to the JSON() method to modify its behavior, perchance?
My apologies if this has been asked before - I'd expect it to be a common question, but I couldn't find anyone like it.
All the DBML generated classes are partial classes so that you can extend them in another file. The DBML designer will only alter classes in the Designer.cs file. Remove the property from the DBML designer and put it in a partial class in another file. You can then add whatever extra attributes you wish, and the DBML designer will leave it alone. You will have to manually manage this property and update it to match any database changes, but I think that is probably a price worth paying if it solves your problem.
If you will have no success with partial classes (which is probably the best way), then you can just serialize the date yourself. It is known that ASP.NET MVC use JavaScriptSerializer to serialize the data. The JavaScriptSerializer have simple and nice customization features like JavaScriptConverter and you can very easy convert the object in something less standard (see use Attr tags for json? for example or all other topics).
To be the most conform to the standards you can define a class derived from JsonResult (for example like here ASP.net MVC returning JSONP or http://dev.qsh.eu/Blogs/Dmitry-P/January-2010/ASP-NET-MVC-Tip--3.aspx) and save serialized data in context.HttpContext.Response directly with a Write method. Then you are absolutely free how you serialize the data to JSON.
Instead of using the partial class answer above you could try going into the DBML designer and setting the access modifier of the property from public to internal. I did this on table to table relationship properties and doing so worked for me to eliminate circular references when serializing my objects to JSON.

Adding more attributes to LINQ to SQL entity

I want to add browsable attribute to some properties for entities generated by LINQ to SQL.
Is it a good idea? Since these entities are auto-generated, and when I regenerate they (the attributes I have added) might be overwritten.
I would probably use Damien Guards LINQ to SQL T4 templates, and modify the template to include the attributes you need. Then the attributes will be generated when you regenerate the classes.
You cannot add additional attributes to the properties in another partial class file because you would be defining the property more than once. This is one reason, among others, that we created our own code generator that generates L2S entity classes the way we want them.
Our code generator also generates a second set of 'application' entities that are much more lightweight than the L2S entities and used at the application level. They contain no L2S plumbing, but do contain other characteristics that the application level finds useful.

Generation custom files from dbml file?

I've been having a look at making changes to the partial classes generated from a DBML file. I was reading into using the sqlmetal.exe tool but it appears that you can't do much customisation of what it actually spits out.
I'm wanting to make changes to the file for serialization purposes, I'd like to add the Data Member Attribute to specified properties in the generated partial classes.
Is this possible to do using the sqlmetal.exe tool or would I need to write my own tool for the file generation?
You could check out T4 templates or CodeSmith for file generation.
No it is not. You can accomplish this with Entity Framework.
http://blogs.msdn.com/jkowalski/archive/2008/05/12/transparent-lazy-loading-for-entity-framework-part-1.aspx
Code written by Jaroslaw Kowalski works much the same way that Linq to SQL does.
It has some issues, but you can do everything with it, because you have the source. I'm going to publish my version soon(support for stored procedures, improved databinding experience and many other useful features)
If you want the datacontract and datamember attributes to be added, simply change the "Serialization Mode" property in the L2S designer's datacontext properties from "None" to "Unidirectional". All entity classes will then be datacontracts, and their members will be datamembers...
The upcoming Beta version of Entity Developer will contain highly customizable T4-like templates for code generation.
Also we have added functionality to divide the generated code into separate files.

Linq-To-SQL Code Generator - How to Modify the Template?

We're using Linq-To-SQL in one of our projects, and I like to modify the template the code generate uses to add some Code Analysis suppression attributes. Anyone know how I can do this?
Have a look here (by Damien Guard), it provides a good rundown on using T4 Templates for this purpose.
Also, looks like he's since posted the whole thing on CodePlex: LINQ to SQL templates for T4
Many of the Linq-To-SQL classes are implemented as partial classes. If you implement your own partial class (http://msdn.microsoft.com/en-us/library/wa80x488%28VS.80%29.aspx) you could added the attributes to it. By using the partial class you avoid the risk of your attributes being removed the next time the Ling-To-SQL classes are generated.