views:

71

answers:

3

I tend to make a lot of folders and when doing OO I put the Models in a folder called Models then under that I have BLL, DAL, OBJ

Instead of writing at the top of all of my files using MyApp.Models; I would like to put this in the Web.Config if possible.

I am new to .net so I am not sure if you can or even if this is a good idea.

+2  A: 

It is possible to register assemblies and namespaces for use in your markup, but not for use in your code behind of code files. VB.NET does support this on a project level, but I never liked this feature, because it makes it less explicit what you're using.

Steven
+2  A: 

It is not possible.

Class names have to be resolved at compile time and web.config can change at any time after deployment.

gustavogb
+2  A: 

I don't believe there is any way to do that in the config file for C# files.

This isn't exactly what you're asking for, but the closest thing to it is that you can register namespaces in the web.config that are then usable within ascx/aspx markup files, as Steven mentioned. Below is an example:

<configuration>
    <system.web>
        <pages>
            <namespaces>
                <add namespace="System.Web.Mvc"/>
                <add namespace="System.Web.Mvc.Ajax"/>
                <add namespace="System.Web.Mvc.Html"/>
                <add namespace="System.Web.Routing"/>
                <add namespace="System.Linq"/>
                <add namespace="System.Collections.Generic"/>
                <!-- Add your own namespaces here -->
            </namespaces>
        </pages>
    </system.web>
</configuration>

EDIT: added another suggestion below:

If you had several using statements that had to be included within most of your .cs files, then the best thing I could suggest is to either A) keep those using statements handy where you can just copy/paste them as needed, or B) create a C# code snippet, such as the example below:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"&gt;
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>using MyApp</Title>
            <Shortcut>usingmyapp</Shortcut>
            <Description>Code snippet for common using statements with MyApp</Description>
            <Author></Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Code Language="csharp">
                <![CDATA[using MyApp.Common;
using MyApp.Common.Extensions;
using MyApp.Domain;
using MyApp.Models;
// *** add whatever using statements you want here ***
$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

After you add that file into the appropriate location (...\My Documents\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets), then you should then just be able to start typing "usingmyapp" (or whatever you put inside the <Shortcut> element) at the location of your .cs file where you want the using statements to appear; intellisense should recognize it and autocomplete it for you by inserting the <Code> content of the code snippet.

Personally, I don't bother with doing something like this; I don't like to add a bunch of using statements to my file if some of them are not actually being used in that file. Additionally, I think adding several using statements in bulk could potentially lead to confusing ambiguous name conflicts if you start using a class name that appears in multiple namespaces. But I think that's just a personal preference; if you want to do this, go ahead.

Dr. Wily's Apprentice
Yes, for The CodeBehind and/or Class files. Also is this a bad idea @Steven? I guess I understand it makes it harder just looking to know what is coming from where. However in Debug mode you can see exactly what comes from where.The namespace I was considering was for a folder like using MyApp.Models;then in the code I can do something like BLL.Category myCategory = BLL.Category;instead of MyApp.Models.BLL.Category myCategory = MyApp.Models.BLL.Category;
Nathan Stanford
@Nathan, it is not an evil thing to do so. If it makes it easier for you to write code, you will finish the job sooner, which is a good thing. You can always clean up the afterwards with the Visual Studio 'Organize usings / Remove and sort' feature.
Steven
Currently since I am wanting to learn how to do it correctly. I am in training mode and learning as quickly and as best as I can. I have so much to learn however I feel I am learning something new each day.
Nathan Stanford