首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在C# .NET中使用MyGeneration涂鸦

在C# .NET中使用MyGeneration涂鸦
EN

Stack Overflow用户
提问于 2013-09-12 08:54:18
回答 2查看 2.3K关注 0票数 1

我已经在.NET表单中添加了一个新控件,并希望将其值保存在表中。我已经在table.How中添加了一个新列。您是否使用MyGeneration Doodads为该表创建数据访问对象?我已经查看过http://www.mygenerationsoftware.com/portal/doodads/cusage/tabid/53/default.aspx,但我不明白“模板”是什么意思.What是为表重新生成doodads的过程?

EN

回答 2

Stack Overflow用户

发布于 2013-11-05 16:09:02

在这个问题上你不会得到太多回应。dOOdads多年来一直不受支持。无论如何,我们也使用dOOdads,我只是为我的WPF项目使用我自己的存储库(我知道它不是ASP,但我不认为你可以只是“即插即用”)。下面是我的基查找类的一个示例:

代码语言:javascript
复制
public abstract class BaseLookup<TEntity>
{
    // Constructor

    protected BaseLookup()
    {
        this.SubsetIdentifier = null;
    }

    // Properties

    public virtual object SubsetIdentifier { get; set; }

    // Public Methods

    public abstract IEnumerable<TEntity> Read();

    public virtual TEntity ReadSingle()
    {
        return default(TEntity);
    }

    // Protected Methods

    /// <summary>
    /// Retrieve translated entities from the database. The methods used to do this
    /// are specified from the child class as parameters (i.e. Action or Func delegates).
    /// </summary>
    /// <param name="loadSubsetFunc">Specify how to load a set of database records.
    /// Return boolean confirmation that records were found.</param>
    /// <param name="orderByAction">Specify what should happen to sort the results.</param>
    /// <param name="translateRowFunc">Specify how a database record should translate to
    /// a model entity. Return the new entity.</param>
    /// <param name="moveNextFunc">Specify how the database row pointer should move on.
    /// Return FALSE on a call to the final row.</param>
    /// <returns>A set of translated entities from the database.</returns>
    /// <example><code>
    ///
    /// return base.ReloadRecords(
    ///     _dOOdad.LoadAll,
    ///     () =>
    ///     {
    ///         _dOOdad.Sort = _dOOdad.GetAutoKeyColumn();
    ///     },
    ///     () =>
    ///     {
    ///         var entity = new LookupEntity();
    ///         return entity.PopulateLookupEntity(_dOOdad.CurrentRow.ItemArray);
    ///     },
    ///     _dOOdad.MoveNext);
    ///     
    /// </code></example>
    protected virtual IEnumerable<TEntity> ReloadRecords(Func<bool> loadSubsetFunc,
        Action orderByAction, Func<TEntity> translateRowFunc, Func<bool> moveNextFunc)
    {
        // If records are found, sort them and return set of entities
        if (loadSubsetFunc())
        {
            orderByAction();

            do
            {
                var entity = translateRowFunc();
                yield return entity;
            }
            while (moveNextFunc());
        }
        else
        {
            Debug.WriteLine(
                string.Format(
                    "# ZERO records found: Returning empty set of {0}.",
                    typeof(TEntity).Name));
        }
    }
}

下面是基本查找的具体实现:

代码语言:javascript
复制
public class CyclesLookup : BaseLookup<BaseLookupEntity>
{
    // Fields & Constructor

    private readonly CYCLES _dOOdad;

    public CyclesLookup(IDbConnection connection, string library)
    {
        _dOOdad = OpenConnection(connection, library);
    }

    // Base Override Methods

    public override IEnumerable<BaseLookupEntity> Read()
    {
        // Clear old result set and settings
        _dOOdad.FlushData();

        // Reload the records and return them sorted and translated
        return base.ReloadRecords(
            _dOOdad.LoadAll,
            () =>
            {
                _dOOdad.Sort = _dOOdad.GetAutoKeyColumn();
            },
            () =>
            {
                var entity = new LookupEntity();
                entity.Description = string.Format("Cycles for {0}", _dOOdad.YEAR);
                return entity.PopulateLookupEntity(_dOOdad.CurrentRow.ItemArray, true);
            },
            _dOOdad.MoveNext);
    }

    // Private Helper Methods

    private static CYCLES OpenConnection(IDbConnection connection, string library)
    {
        var dOOdad = new CYCLES(connection);
        dOOdad.SchemaGlobal = library + ".";
        return dOOdad;
    }
}

该PopulateLookupEntity方法只是一个扩展方法:

代码语言:javascript
复制
public static T PopulateLookupEntity<T>(this T entity, object[] rowItems,
    bool noDescription = false) where T : BaseLookupEntity
{
    int id = 0;
    int.TryParse(rowItems[0].ToString(), out id);
    entity.RecordID = id;

    var attributesFirstIndex = 1;
    if (!noDescription)
    {
        entity.Description = rowItems[1].ToString();
        attributesFirstIndex = 2;
    }

    entity.Attributes = new object[rowItems.Length - attributesFirstIndex];
    for (int index = attributesFirstIndex; index < rowItems.Length; index++)
    {
        entity.Attributes[index - attributesFirstIndex] = rowItems[index];
    }

    return (T)entity;
}

对于非查找,我有一个继承自BaseLookup的更复杂的BaseRepository类。对于这个方法,我没有使用PopulateLookupEntity,而是使用了一个私有的助手方法,如下所示:

代码语言:javascript
复制
private IPlanningGridHeader TranslateCurrentDoodadRow()
{
    return new PlanningGridHeader()
    {
        PlanNumber = Convert.ToInt32(_dOOdad.PLANNUMBER),
        Active = _dOOdad.ACTIVE == 1M,
        Capturer = _dOOdad.CAPTURER,
        Region = _dOOdad.REGION,
        CorporateID = Convert.ToInt32(_dOOdad.CORPORATEID),
        StartDate = _dOOdad.STARTDATE.ConvertDb2Date(),
        EndDate = _dOOdad.ENDDATE.ConvertDb2Date(),
        AdvertStartDate = _dOOdad.ADVERTSTARTDATE.ConvertDb2Date(),
        AdvertEndDate = _dOOdad.ADVERTENDDATE.ConvertDb2Date(),
        BpcsDealNumber = Convert.ToInt32(_dOOdad.BPCSDEALNUMBER),
        Description = _dOOdad.DESCRIPTION,
        DeactivationReason = _dOOdad.DEACTIVATIONREASON,
        LastSavedUsername = _dOOdad.LASTUSER,
        LastSavedDateTime = _dOOdad.LASTDATE.ConvertDb2Date().AddDb2Time(_dOOdad.LASTTIME)
    };
}

希望这能有所帮助:-)

票数 3
EN

Stack Overflow用户

发布于 2020-02-11 04:10:01

虽然它很旧,但我还是建议你远离这个库。它有一个严重的bug,导致连接泄漏,我敢打赌,创建者在编码时没有检查ADO.NET实践。此外,他不知道如何处理DBNull,因此他在普通属性的旁边发明了“字符串属性”来处理空问题,将生成的代码和编程模型弄得一团糟,在开发人员访问值为空的普通属性时创建了更多的NullReferenceException。

我使用MyGeneration Doodads从一个有10+年历史的遗留系统中收集了所有这些问题。我很高兴我终于可以摆脱它了。

如果您正在为企业应用程序查找DAC库,请不要选择此库。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18753666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档