我可以用泛型类型解决这个问题吗? [英] Can I solve this with generics types?

查看:117
本文介绍了我可以用泛型类型解决这个问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我想要的类结构:

  public class AllocationNEW< TActivity,TResource> 
其中TActivity:AllocatableActivity< TActivity>
其中TResource:AllocatableResource< TResource>
{

public TResource Resource {get;私人设置; }
public TActivity Activity {get;私人设置; }
public TimeQuantity Period {get;私人设置; }
$ b $ public AllocationNEW(TResource resource,TActivity activity,DateTime eventDate,TimeQuantity timeSpent)
{
Check.RequireNotNull< IResource>(resource);
资源=资源;

Check.RequireNotNull< Activities.Activity>(activity);
Activity = activity;

Check.Require(timeSpent.Amount> = 0,Msgs.Allocation_NegativeTimeSpent);
TimeSpentPeriod = _toTimeSpentPeriod(eventDate,timeSpent);
}
}


公共类AllocatableResource< TResource>
{
public string Description {get;保护组; }
public string BusinessId {get;保护组; }
}

public class AllocatableActivity< TActivity>
{

public string Description {get;保护组; }
public string BusinessId {get;保护组; }

protected readonly IDictionary<日期时间,分配< TActivity,TResource>> _allocations;

public virtual void ClockIn< TResource>(DateTime eventDate,TResource resource,TimeQuantity timeSpent)
{
var entry = new Allocation< TActivity,TResource>(resource,this,eventDate , 所花费的时间);
if(_allocations.ContainsKey(eventDate))
{
Check.Require(_allocations [eventDate] .Resource.Equals(resource),
此方法需要相同的资源是此分配中的资源。);
_allocations [eventDate] = entry;
}
else _allocations.Add(eventDate,entry);


当然,AllocatableActivity就是这个方案崩溃的地方。我需要在这个类中使用Allocations集合,但是我不知道TResource会在客户使用ClockIn方法之前做什么。



I'我一直坚持关于如何用Resource和Activity类型解决Allocation类的一些想法,所以我希望我错过了一些明显的东西。它看起来应该可以用泛型解决。



干杯



更多信息



以下是一些活动示例:

  public class ProjectActivity:AllocatableActivity< Project> 
{
public ProjectActivity(Project project){
_project = project;
说明= _project.Description;
BusinessId = _project.Code.ToString();
}
// private readonly Project _project; - >应不再需要
}

公共类AccountingActivity:AllocatableActivity< Account>
{

Public AccountingActivity(帐户帐户)
{
_account =帐户;
说明= account.Description;
BusinessId = account.AccountId;
}
//私人只读帐户_account;
}

以下是一个资源示例:

  public class StaffMemberResource:AllocatableResource< StaffMember> 
{
public StaffMemberResource(StaffMember staffMember){
Description = staffMember.Name.ToString();
BusinessId = staffMember.Number;
}
}

数据输入将由单个资源输入。我还不知道递归问题,但除此之外,没有任何商业原因,为什么除了创建活动的成本之外,TResource无法在数据输入会话中知道(有大约300个基础项目组合,在这段时间内,我们希望将其视为AllocatableActivity。

报告涉及多种资源(即,需要按时间签署的经理需要查看所有花费的时间她分配的资源,项目特设报告等)。

我没有使用泛型(接口和基类),但是有一些涉及打字的尴尬,所以我想看看泛型是否会使接口更简单。

解决方案

为什么不是 AllocatableActivity< TActivity> TResource 中也是通用的?这将使它在语言方面都有效 - 是否存在业务问题?你给exa TActivity TResource 的元素,以及为什么您在创建活动时可能不知道需要哪种资源?

Here is the class structure I'd like to have:

  public class AllocationNEW<TActivity, TResource> 
    where TActivity : AllocatableActivity<TActivity>
    where TResource : AllocatableResource<TResource>
  {

   public TResource Resource { get; private set; }
   public TActivity Activity { get; private set; }
   public TimeQuantity Period { get; private set; }

   public AllocationNEW(TResource resource, TActivity activity, DateTime eventDate, TimeQuantity timeSpent)
    {
        Check.RequireNotNull<IResource>(resource);
        Resource = resource;

        Check.RequireNotNull<Activities.Activity>(activity);
        Activity = activity;

        Check.Require(timeSpent.Amount >= 0, Msgs.Allocation_NegativeTimeSpent);
        TimeSpentPeriod = _toTimeSpentPeriod(eventDate, timeSpent);
    }
   }


public class AllocatableResource<TResource>
{
    public string Description { get; protected set; }
    public string BusinessId { get; protected set; }     
}

public class AllocatableActivity<TActivity>
{

    public string Description { get; protected set; }
    public string BusinessId { get; protected set; }

    protected readonly IDictionary<DateTime, Allocation<TActivity, TResource>> _allocations;

    public virtual void ClockIn<TResource>(DateTime eventDate, TResource resource, TimeQuantity timeSpent)
    {
        var entry = new Allocation<TActivity, TResource>(resource, this, eventDate, timeSpent);
        if (_allocations.ContainsKey(eventDate))
        {
            Check.Require(_allocations[eventDate].Resource.Equals(resource),
                          "This method requires that the same resource is the resource in this allocation.");
            _allocations[eventDate] = entry;
        }
        else _allocations.Add(eventDate, entry);
    }
}

AllocatableActivity is where this scheme breaks down, of course. I need a collection of Allocations to work with in this class, but I don't know what TResource is going to be until a client uses the ClockIn method.

I've been stuck on various ideas on how to solve the Allocation class with Resource and Activity types for awhile, so I'm hoping that I'm missing something obvious. It seems like it should be solvable with Generics.

Cheers

MORE INFO

Here's some examples of activities:

public class ProjectActivity : AllocatableActivity<Project>
{
    public ProjectActivity(Project project) {
        _project = project;
        Description = _project.Description;
        BusinessId = _project.Code.ToString();
    }
    // private readonly Project _project; --> shouldn't need anymore
}

public class AccountingActivity : AllocatableActivity<Account>
{

    public AccountingActivity(Account account)
    {
        _account = account;
        Description = account.Description;
        BusinessId = account.AccountId;
    }
    // private readonly Account _account;
}

Here's an example of a resource:

public class StaffMemberResource : AllocatableResource<StaffMember>
{
    public StaffMemberResource(StaffMember staffMember) {
        Description = staffMember.Name.ToString();
        BusinessId = staffMember.Number;
    }
}

Data entry would be by a single Resource. I don't know about the recursion issue yet, but otherwise there is no business reason why TResource can't be known in a data entry session other than the cost of creating the activities (there's about 300 combinations of underlying projects, accounts that I would want to treat as an AllocatableActivity at this time.

Reporting would involve multiple resources (ie, a Manager needing to sign off on time spent needs to see all time spent by her assigned resources, ad hoc reports by project, etc.)

I was laying this out without generics (interfaces and base classes) but there was some awkwardness with involving the typing, so I wanted to see if generics would make for a simpler interface.

解决方案

Why isn't AllocatableActivity<TActivity> also generic in TResource? That would make it all work in terms of the language - is there a business problem with that? Could you give examples of TActivity and TResource, and why you might not know what kind of resource you need when you create the activity?

这篇关于我可以用泛型类型解决这个问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆