ADO EF 数据操作方法

发布于 2024-12-10 23:48:49 字数 927 浏览 0 评论 0原文

我希望有人能指出我重构一些方法以接受泛型类型的方向。

我有一个服务与我的数据库中的许多不同的表进行交互。大多数表格的格式相同。该服务对所有表执行相同的基本操作:GetAll、Save、Delete。由于操作是相同的,我一直在寻找一种创建核心操作库的方法。

这是我的保存功能的示例,它与其他表的唯一区别是数据。所以这里是 ClientData,下一个是 GroupData 等...

internal static void SaveClient(ClientData clientData)
        {
            using (CFOEntityModelContainer database = new CFOEntityModelContainer())
            {
                if (!IsClientValid(clientData.Id))
                {
                    database.Clients.AddObject(ConvertClientDataToClient(clientData));
                    database.SaveChanges();
                }
                else
                {
                    Client client = database.Clients.First(
                        c => c.Id == clientData.Id);

                    client.Name = clientData.Name;

                    database.SaveChanges();
                }
            }
        }

建议?

I was hoping someone could point me in the direction of refactoring some of my methods to accept generic types.

I have a Service that speaks with many different tables in my database. The majority of the tables are of the same format. The service performs the same basic operations on all of the tables, GetAll, Save, Delete. Since the operation is the same I was looking for a way to create a core library of actions.

This is an example of my save function, the only difference between this and that of the other tables is the data. So here its ClientData, on the next one its GroupData ect...

internal static void SaveClient(ClientData clientData)
        {
            using (CFOEntityModelContainer database = new CFOEntityModelContainer())
            {
                if (!IsClientValid(clientData.Id))
                {
                    database.Clients.AddObject(ConvertClientDataToClient(clientData));
                    database.SaveChanges();
                }
                else
                {
                    Client client = database.Clients.First(
                        c => c.Id == clientData.Id);

                    client.Name = clientData.Name;

                    database.SaveChanges();
                }
            }
        }

Suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七月上 2024-12-17 23:48:49

如果您的方法包含特定于类型的操作,您希望如何使其通用?您是否总是有 XXX 实体和 XXXData 类,其中 XXX 实体包含 IdName,XXXData 类包含 NameId >?如果不是,您的通用保存将只是模板接受特定方法的委托=没有简化。如果是,您可以执行以下操作:

首先准备通用方法中使用的接口:

public interface IData
{
    int Id { get; }
    string Name { get; set; }
}

现在在实体和数据类上实现该接口并创建通用方法:

internal static void Save<TEntity, TData>(TData data)
    where TEntity : class, IData
    where TData : class, IData
{
    using (CFOEntityModelContainer database = new CFOEntityModelContainer())
    {
        ObjectSet<TEntity> set = database.CreateObjectSet<TEntity>();

        // Here you have specific method - can you make it generic?
        // If not it must be another parameter (delegate) passed to Save method
        if (!IsValid(data.Id))  
        {
            // Convert is another specific method which must be generalize
            // This time it can be probably solved by overriding conversion
            // operator on each data implementation
            set.AddObject(Convert(data));
        }
        else
        {
            // Another specific logic - can you make it generic?
            // If not it must be passed as delegate
            TEntity entity = set.First(e => e.Id == data.Id);
            entity.Name = data.Name;
        }

        database.SaveChanges();
    }
}

您可以像这样调用此方法:

Save<Client, ClientData>(data);

How do you expect to make it generic if your method contains type specific operations? Do you always have XXX entity and XXXData class where XXX entity contains Id and Name and XXXData class contains Name and Id? If not your generic save would be just template accepting delegates to specific method = no simplification. If yes you can do something like:

First prepare interface used in generic methods:

public interface IData
{
    int Id { get; }
    string Name { get; set; }
}

Now implement that interface on your entity and data classes and create generic method:

internal static void Save<TEntity, TData>(TData data)
    where TEntity : class, IData
    where TData : class, IData
{
    using (CFOEntityModelContainer database = new CFOEntityModelContainer())
    {
        ObjectSet<TEntity> set = database.CreateObjectSet<TEntity>();

        // Here you have specific method - can you make it generic?
        // If not it must be another parameter (delegate) passed to Save method
        if (!IsValid(data.Id))  
        {
            // Convert is another specific method which must be generalize
            // This time it can be probably solved by overriding conversion
            // operator on each data implementation
            set.AddObject(Convert(data));
        }
        else
        {
            // Another specific logic - can you make it generic?
            // If not it must be passed as delegate
            TEntity entity = set.First(e => e.Id == data.Id);
            entity.Name = data.Name;
        }

        database.SaveChanges();
    }
}

You can call this method like:

Save<Client, ClientData>(data);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文