FluentNHibernate映射到现有表而不进行规范化

时间:2012-10-25 19:31:20

标签: nhibernate fluent-nhibernate fluent-nhibernate-mapping

我的数据库中有现有表,需要为它创建一个域来处理现有数据。

我有下表结构:

Application
'ORDERSCANNER'

ApplicationFunction
'ORDERSCANNER','SCAN'
'ORDERSCANNER','READ'
'ORDERSCANNER','PRINT'

FunctionalGroup
'ADMIN','RUS','ORDERSCANNER','SCAN'
'ADMIN','RUS','ORDERSCANNER','PRINT'
'ADMIN','JAP','ORDERSCANNER','SCAN'
'ADMIN','JAP','ORDERSCANNER','PRINT'
'OPERATOR','RUS','ORDERSCANNER','SCAN'
'OPERATOR','JAP','ORDERSCANNER','SCAN'

User
'IVANOV','RUS','OPERATOR'
'PETROV','RUS','OPERATOR'
'PETROV','JAP','OPERATOR'
'SIDOROV','RUS','ADMIN - he is an ADMIN group but only for RUS. He is not able to scan for JAP

以下是我查看域名的方式

public class Application // root
{
    private readonly List<ApplicationFunction> _functions;
    public string ApplicationId { get; set; }

    public Application()
    {
        _functions = new List<ApplicationFunction>();
    }
    public void AddFunction(string newFunction)
    {
        var function = new ApplicationFunction(this, newFunction);
        _functions.Add(function);
    }
}
public class ApplicationFunction // value object
{
    public Application Application { get; private set; }
    public string FunctionId { get; private set; }

    public ApplicationFunction(Application application, string functionId)
    {
        if (application == null) throw new ArgumentNullException("application");
        if (functionId == null) throw new ArgumentNullException("functionId");
        Application = application;
        FunctionId = functionId;
    }
}
public class FunctionalGroup // root
{
    private readonly List<ApplicationFunction> _functions;
    public string GroupId { get; set; }
    public string MarketingPlanCode { get; set; }
    public IEnumerable<ApplicationFunction> Functions { get { return _functions; } }

    public FunctionalGroup()
    {
        _functions = new List<ApplicationFunction>();
    }
    public void AddFunction(ApplicationFunction applicationFunction)
    {
        _functions.Add(applicationFunction);
    }
}
public class User // root
{
    private readonly List<FunctionalGroup> _groups;
    public string UserId { get; set; }
    public IEnumerable<FunctionalGroup> Groups { get { return _groups; } }

    public User()
    {
        _groups = new List<FunctionalGroup>();
    }
    public void AddToGroup(FunctionalGroup functionalGroup)
    {
        _groups.Add(functionalGroup);
    }
}

让我们来看看用户。

public class UserMap : ClassMap<User>
{
     public UserMap()
     {
         Id(x => x.UserId).Column("USER_ID");
         HasMany(x => x.Groups).Cascade.All();
     }
}

看起来我需要额外的数据来映射我的FunctionalGroup集合 - 它是MarketingPlanCode。如果我将MarketingPlanCode属性添加到我的User类中,那么每个集合只能获得一个属性。但是我需要在该集合中为每件物品提供属性。

好吧如果我无法解释我需要的东西请看看我的表中的示例数据。有没有办法映射它?

1 个答案:

答案 0 :(得分:1)

我认为该模型可以进一步简化,但首先完全抛弃FunctionalGroup类。

public ApplicationFunctionMap()
{
    CompositeId()
        .KeyReference(x => x.Application, "application_id")
        .KeyProperty(x => x.FunctionId, "FunctionId");
}

public UserMap()
{
    Component(x => x.Group, c => 
    {
        c.Map(g => g.Name, "GroupId");
        c.Map(g => g.MarketingPlanCode, "MarketingPlanCode");
    })

    HasManyToMany(x => x.AllowedFunctions)
        .Table("FunctionalGroup")
        .ParentKeyColumns.Add("GroupId", "MarketingPlanCode") // in FunctionalGroup table
        .ChildKeyColumns.Add("application_id", "FunctionId") // in FunctionalGroup table
        .Propertyref(x => x.Group);   // Component defined above
}
相关问题