如何在DLL中编写更多常规方法 [英] How to write more general methods in a dll

查看:62
本文介绍了如何在DLL中编写更多常规方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的
我在一个C#应用程序中编写了一个Class,在其他程序中经常使用它
它是一个类,其中包含一些用于读取或写入Access数据库中的数据的方法.
我正在寻找一种解决方案,以免在我的每个程序中添加此类并将其写入
我在网上搜索,发现最好编写一个dll文件,只需添加一个引用,便可以每次在程序中使用它,
但现在我的问题是:

例如假设我有这样的方法

Hi dears
I wrote a Class in one of my C# application that I use it a lot in my other programs
it''s a class that contains some methods for Reading or Writing Data in an Access database.
I was seeking for a solution to not add and write this class to my every program
I searched the net, and found that it''s better to write a dll file that I can use every time in my programs just by adding a reference,
but now my question is:

for e.g. suppose that I have a method like this

public void Write_Account(Account account)   //Write_Data
        {
            if (Validate_Account(account.UserName) == false)
            {
                try
                {
                    if (con.State != ConnectionState.Open)
                    {
                        con.ConnectionString = con_string;
                        con.Open();
                    }

                    StringBuilder st = new StringBuilder();
                    
                    st.Append("Insert into Accounts Values ('");
                    st.Append(account.UserName);
                    st.Append("','");
                    st.Append(account.PassWord);
                    st.Append("','");
                    st.Append(account.Email);
                    st.Append("','");
                    st.Append(account.VerificationCode);
                    st.Append("','");
                    st.Append(account.ActivationStatus);
                    st.Append("')");
                    
                    data_cmd.Connection = con;
                    data_cmd.CommandText = st.ToString();

                    data_cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("an error occured while connecting database\n" + ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    if (con.State != ConnectionState.Closed)
                        con.Close();
                }
            }
            else
                MessageBox.Show("This user already exists!", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }  //end of Write_Data*************************************



在这里,我知道我要写多少个条目(例如,我的Account类有5个参数)

但是如果我不知道我有多少条目怎么办?



here I know that how many entry I want to write (for e.g my Account class has 5 arguments)

but what can I do if I don''t know how many entries I have?
how can I write this part of code more general?

推荐答案

问题尚不清楚.您没有抽象目标时正在寻找某种抽象技术.我的意思是,在您的确切情况下,您实际上不需要进一步的抽象.原因如下:您传递类或结构Account的参数.它具有五个要附加到某个字符串的属性,仅此而已.如果这是一个类,则可以对其进行扩展并传递派生类的实例,但是当您的方法被写入基础抽象或伪抽象类时,它只能与基础类的属性一起使用.

经典的OOP方法是,您需要添加一些虚拟方法,例如AppendPropertyValuesToStringBuilder,并在每个派生类中重写它.在这种情况下,您将把NamePasswordEmailVerificationCodeActivationStatus的片段从Write_Account方法移动到它真正所属的每个派生类中,因为只有这些类才知道它们的特性是什么?请记住,在每个派生类中,您始终可以调用base.AppendPropertyValuesToStringBuilder来附加已在基类中定义的数据部分(此方法是非抽象的).由于利用了 late绑定的机制,Write_Account在基类上作为 compile-time type 调用的方法AppendPropertyValuesToStringBuilder实际上将被分派到调用运行时类型之一的重写方法,将其作为参数传递给该方法;并且这些运行时类型中的每一个都是派生类之一.这是经典OOP的核心.请参阅:
http://en.wikipedia.org/wiki/Dynamic_dispatch [ http://en.wikipedia.org/wiki/Late_binding [http://en.wikipedia.org/wiki/面向对象的程序 [ http://en.wikipedia.org/wiki/Exception_handling [我如何制作滚动条到达底部时将停止的循环 [当我运行应用程序时,例外是捕获了如何处理此问题? [扔. .then ... rethrowing [ ^ ],
错误记录和屏幕截图. [捕获异常 [在类库(dll)中处理异常 [ ^ ].

—SA
The problem is not clear. You are looking for some abstraction technique while you don''t have abstract target. I mean that in your exact case you don''t really need further abstraction. Here is why: you pass a parameter of the class or structure Account. It has five properties you want to append to some string, no more. If this is a class, you can extend it and pass an instance of a derived class, but as your method is written to the base abstract or pseudo abstract class, it can only work with the property of a base class.

The classic OOP approach is that you need to add some virtual method like AppendPropertyValuesToStringBuilder and override it in each of the derived classes. In this case, you will move the fragment appending Name, Password, Email, VerificationCode and ActivationStatus from your Write_Account method to each of derived classes where it really belongs, because only the classes know what are their properties. Remember, in each of derived classes you can always call base.AppendPropertyValuesToStringBuilder to append the part of data already defined in the base class (where this method is non-abstract). As it leverages the mechanism of late binding, the method AppendPropertyValuesToStringBuilder called by Write_Account on a base class as a compile-time type, will actually be dispatched to call the overridden method of one of the run-time types to be passed to this method as its argument; and each of those run-time types will be one of the derived classes. This is a heart of classical OOP. Please see:
http://en.wikipedia.org/wiki/Dynamic_dispatch[^],
http://en.wikipedia.org/wiki/Late_binding[^],
http://en.wikipedia.org/wiki/Object-oriented_programming[^].

You really need to fully understand OOP to do any .NET development.

Now, you have some solution, but you need to look at this problem from a different point of view. You don''t use appropriate abstraction mechanism not just because you don''t know them well, but because your approach to data is questionable. Your property appending code tells me that you are trying to work with string presentation of data instead of data itself. Yes, there are places where you need to show some data in the string form; on the screen, for example. But this should be totally isolated from data itself. Just think about it.

And finally, your code has one critical problem. In your exception catch handler, you totally block exception propagation. As a rule of thumb, you should not do it. Let exception go. You can finally catch all exceptions only on the top of the stack of each thread. By blocking exception propagation, you effectively defeat the purpose of structural exception handling. You really need to learn the basics of exception handling. The pain purpose of it is to avoid dealing with exception in almost all of your code. The mechanism isolates exceptional situation from the regular operation, so you can forget about them in most of your work. Please see:
http://en.wikipedia.org/wiki/Exception_handling[^].

Please also see my recommendations in my past answers:
How do i make a loop that will stop when a scrollbar reaches the bottom[^],
When i run an application an exception is caught how to handle this?[^],
throw . .then ... rethrowing[^],
Error Logging and Screen Shot.[^],
Catching an Exception[^],
Handling exceptions in class library (dll)[^].

—SA


我同意Sergey Alexandrovich的观点,问题不在那段代码中.用简单的话讲,问题在于您的函数依赖于"Account"类,因此,如果要使函数更抽象,则需要通过重载函数来使类更抽象.

现在,我很好奇,为什么要向此函数传递Accaunt参数?它不是帐户类吗?

我希望将此函数包括在Accaunt类中,以便在创建它时可以采用默认的类参数.
I agree with Sergey Alexandrovich, the problem isnt in that piece of code. The problem in simple words is that your function depends on a class "Account", so if you want to make the function more abstract you need to do the class more abstract by overloading functions.

Now Im curious, why do you pass an Accaunt parameter to this function? it isnt on the account class?

I would prefer to include this function in the Accaunt class so I could take the default class parameters when you create it.


这篇关于如何在DLL中编写更多常规方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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