开发者

Odd bug in C#4 compiler? Alias namespaces getting mixed up

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.

Odd bug in C#4 compiler? Alias namespaces getting mixed up

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜