views:

28

answers:

1

Here's my issue,

I'm using a namespace to remove ambiguity from a factory class which is creating domain objects from entity framework entity objects (a POCO factory,.. if that makes sense!). Call me old fashioned but I like things this way :-)

The namespaces I'm working with are aliased as such -

using Entities = DataAccess.Models.AccessControl;
using Cases = DomainModel.BusinessObjects.Implimentations.Cases;

Now, the DomainModel.BusinessObjects.Implimentations.Cases namespace only has one type so far called CaseType. However whilst I was working on another type which consumes the CaseType class I noticed that when I 'dot' into the Cases alias, it points to a totally different namespace in my DataAccess assembly and gives me the CaseTypeFactory in intellisense.

alt text

So I checked the CaseType and CaseTypeFactory classes and they are namespaced correctly. What in god's name is going on? I really can't work this one out.

Here's the code for the CaseType and CaseTypeFactory if it helps.

CaseTypeFactory

using Domain = DomainModel.BusinessObjects.Implimentations.Cases;
using Entities = DataAccess.Models.AccessControl;
using Interfaces.DataAccess;

    namespace DataAccess.Factories.Cases
    {
        public class CaseTypeFactory :
            IEntityPOCOFactory<Domain.CaseType, Entities.CaseType>
        {
            #region IEntityPOCOFactory<CaseType,CaseType> Members

            public Domain.CaseType CreatePOCO(Entities.CaseType entity)
            {
                return new Domain.CaseType(entity.Id, entity.Name, entity.LastChanged);
            }

            public IList<Domain.CaseType> CreatePOCOs(
                IEnumerable<Entities.CaseType> entities)
            {
                var toReturn = new List<Domain.CaseType>();

                foreach (var entity in entities)
                {
                    toReturn.Add(CreatePOCO(entity));
                }
                return toReturn;
            }

            #endregion
        }
    }

CaseType

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainModel.BusinessObjects.Base;

namespace DomainModel.BusinessObjects.Implimentations.Cases
{
    public class CaseType : NamedNumberedAndVersioned
    {
        public CaseType(string name, DateTime lastChanged)
            : this(0, name, lastChanged) { }

        public CaseType(int id, string name, DateTime lastChanged)
            : base(id, name, lastChanged) { }

        public void Update(string name)
        {
            this.Name = name;
        }
    }
}

It's probably worth mentioning I'm working with .Net 4.0 / VS2010

Any help would be massively appreciated. Thanks in advance.

+1  A: 

Is the code you're writing in the DataAccess.Factories namespace? If so, then Cases will indeed resolve to DataAccess.Factories.Cases first.

Two options:

  • Use an alias which isn't also the name of another namespace, e.g.

    using DomainCases = ...
    
  • Use :: instead of . to force it to use the alias:

    IEntityPOCOFactory<Cases::CaseType, Whatever>
    

I'd personally go for the first option to make things clearer... or try to avoid requiring namespace aliases to start with.

Jon Skeet
How very strange. You are, as always, right on the money. Why when I've qualified the namespace the alias should use does it have to 'resolve' at all? Peculiar but thanks very much Jon.
Stimul8d
@Stimul8d: Well, because you can *also* use the dot notation with non-aliases (whereas :: will *only* work with aliases). When in doubt, try to give a name only one meaning :)
Jon Skeet
Agreed, thanks for the advice.
Stimul8d